-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionary.java
More file actions
117 lines (100 loc) · 3.43 KB
/
Copy pathDictionary.java
File metadata and controls
117 lines (100 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.*;
/*
* This class creates a dictionary by reading in a file and storing each word in a HashTable.
* It can return all the words that are at the same index position as the searched for word,
* or find all the permutations that the hashTable contains of that specified word.
* @Author: Sönke Schaarschmidt & Jonas Kallwies
*/
public class Dictionary {
private FileReader input;
private File original;
private Scanner text;
private HashTable hashTable;
/*
* Creating and setting up a dictionary. Printing out the lookup results
* for the searched String word and all of its permutations.
*/
public static void main(String[] args) {
Dictionary d = new Dictionary();
d.setup();
String word = "ealdngi";
//d.searchForPermutations();
System.out.println("Entries: " + d.hashTable.entries + " | " + "Collisions: " + d.hashTable.collisions);
System.out.println();
LinkedList<String> all = d.lookup(word);
System.out.println("Permutations of (" + word + ") could be in here:");
System.out.println(all);
System.out.println();
System.out.println("All permutations of (" + word + "):");
LinkedList<String> permutations = d.lookupPermutations(word);
System.out.println(permutations);
}
/*
* Setup method creates a hashTable and reads all the words from a file using a Scanner.
* Each word is then stored in the hashTable.
*/
public void setup() {
hashTable = new HashTable(1000);
original = new File("/users/sonkeschaarschmidt/downloads/7letters.txt");
try {
input = new FileReader(original);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
text = new Scanner(input);
while(text.hasNext()) {
hashTable.add(text.next(), false);
}
}
/*
* Returns a LinkedList<String> containing all words at the index position where
* the word searched for would be situated according to its index calculation.
* Its possible permutations are in this LinkedList as well if available.
*/
public LinkedList<String> lookup(String word){
int index = hashTable.calculateIndex(word);
LinkedList<String> list = hashTable.array[index];
return list;
}
/*
* Returnes a LinkedList<String> containing all permutations of the String word.
* If the word searched for is in the dictionary, it is not returned, as this
* order of the letters is already known to the user.
*/
public LinkedList<String> lookupPermutations(String word){
int index = hashTable.calculateIndex(word);
LinkedList<String> list = hashTable.array[index];
LinkedList<String> permutations = new LinkedList<String>();
for(int i = 0; i < list.size(); i++) {
if(hashTable.isPermutation(word, list.get(i))) {
if(!word.equals(list.get(i))) {
permutations.add(list.get(i));
}
}
}
return permutations;
}
/*
* Returns all Permutations that are available in the Dictionary.
* This was only done to find out which ones there are, without
* reading the file ourselves
*/
public void searchForPermutations(){
for(int i = 0; i < hashTable.array.length; i++) {
LinkedList<String> list = hashTable.array[i];
for(int j = 0; j < list.size(); j++) {
String word = list.get(j);
for(int k = 0; k < list.size(); k++) {
if(hashTable.isPermutation(word, list.get(k))) {
if(!word.equals(list.get(k))) {
System.out.println(word + " - " + list.get(k));
}
}
}
}
}
}
}