-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActiveChecker.java
More file actions
196 lines (179 loc) · 5.2 KB
/
Copy pathActiveChecker.java
File metadata and controls
196 lines (179 loc) · 5.2 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import java.util.*;
import java.io.*;
import java.util.stream.Collectors;
public class ActiveChecker
{
private TrieNode trieN;
private TreeNode treeN;
public String enteredWord;
public Trie trie;
public Tree tree;
public String dataStructure;
public ArrayList<String> dictionary, suggestions;
public ActiveChecker() {
trie = new Trie();
tree = new Tree();
dataStructure = "trie";
dictionary = new ArrayList<String>();
suggestions = new ArrayList<String>();
}
public void readDictionary() {
try {
Scanner scan = new Scanner(new File("english.0"));
while (scan.hasNextLine()) {
String line = scan.nextLine();
if (!line.isEmpty()) {
dictionary.add(line);
}
}
}
catch (FileNotFoundException e) {
System.out.println("english.0 file not found.");
return;
}
if (dataStructure.equals("trie")) {
trieDictionary();
}
else {
treeDictionary(0, dictionary.size()-1);
}
}
public void trieDictionary() {
for (int i=0; i<dictionary.size(); i++) {
trie.insert(dictionary.get(i).toLowerCase());
}
}
public void treeDictionary(int start, int end) {
for (int i=0; i<dictionary.size(); i++) {
tree.insert(dictionary.get(i).toLowerCase());
}
tree.root = tree.build(tree.root);
}
public void readInput() {
if (dataStructure.equals("trie")) {
trieInput();
}
else {
treeInput();
}
}
public void trieInput() {
HashMap<String, Integer> distanceMap = new HashMap<String, Integer>();
if (trie.search(enteredWord.toLowerCase())) {
suggestions.add(enteredWord);
}
else {
for (int j=0; j<dictionary.size(); j++) {
String word = dictionary.get(j);
distanceMap.put(word, levenshtein(enteredWord, word, enteredWord.length(), word.length()));
}
distanceMap = distanceMap.entrySet().stream().sorted(Map.Entry.comparingByValue()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
Iterator<String> iterator = distanceMap.keySet().iterator();
for (int j=0; j<3; j++) {
suggestions.add(iterator.next());
}
}
}
public void treeInput() {
HashMap<String, Integer> distanceMap = new HashMap<String, Integer>();
if (tree.search(enteredWord.toLowerCase())) {
suggestions.add(enteredWord);
}
else {
for (int j=0; j<dictionary.size(); j++) {
String word = dictionary.get(j);
distanceMap.put(word, levenshtein(enteredWord, word, enteredWord.length(), word.length()));
}
distanceMap = distanceMap.entrySet().stream().sorted(Map.Entry.comparingByValue()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
Iterator<String> iterator = distanceMap.keySet().iterator();
for (int j=0; j<3; j++) {
suggestions.add(iterator.next());
System.out.println("size: " + suggestions.size());
}
}
}
public LinkedHashMap<String,Integer> sortMapByValue(HashMap<String, Integer> map) {
List<String> keys = new ArrayList<>(map.keySet());
List<Integer> values = new ArrayList<>(map.values());
Collections.sort(values);
Collections.sort(keys);
LinkedHashMap<String,Integer> newMap = new LinkedHashMap<String,Integer>();
Iterator<Integer> valueIterator = values.iterator();
while (valueIterator.hasNext()) {
Integer value = valueIterator.next();
Iterator<String> keyIterator = keys.iterator();
while (keyIterator.hasNext()) {
String key = keyIterator.next();
Integer v1 = map.get(key);
Integer v2 = value;
if (v1.equals(v2)) {
keyIterator.remove();
newMap.put(key, value);
break;
}
}
}
return newMap;
}
public int levenshtein(String str1, String str2, int lth1, int lth2) {
int[][] length = new int[lth1+1][lth2+1];
for (int i=0; i<=lth1; i++) {
length[i][0] = i;
}
for (int i=0; i<=lth2; i++) {
length[0][i] = i;
}
for (int i=1; i<=lth1; i++) {
for (int j=1; j<=lth2; j++) {
if (str1.charAt(i-1) == str2.charAt(j-1)) {
length[i][j] = length[i-1][j-1];
}
else {
length[i][j] = Math.min(length[i-1][j]+1, Math.min(length[i][j-1]+1, length[i-1][j-1]+1));
}
}
}
return length[lth1][lth2];
}
public void configuration() {
try {
Scanner scan = new Scanner(new File("a1properties.txt"));
while (scan.hasNextLine()) {
String line = scan.nextLine();
if (line.toLowerCase().contains("storage=")) {
if (line.toLowerCase().contains("=tree")) {
dataStructure = "tree";
}
else if (line.toLowerCase().contains("=trie")) {
dataStructure = "trie";
}
else {
dataStructure = "trie";
}
}
}
}
catch (FileNotFoundException e) {
dataStructure = "trie";
}
}
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Invalid arguments.");
return;
}
for (int i=1; i<=args[0].length(); i++) {
ActiveChecker assignment1 = new ActiveChecker();
assignment1.enteredWord = args[0].substring(0,i);
assignment1.configuration();
assignment1.readDictionary();
assignment1.readInput();
ArrayList<String> output = assignment1.suggestions;
System.out.print("Suggestions for \"" + assignment1.enteredWord + "\": ");
for (int j=0; j<output.size(); j++) {
System.out.print(output.get(j) + " ");
}
System.out.print("\n");
}
}
}