-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrieDictionary.java
More file actions
137 lines (122 loc) · 3.73 KB
/
TrieDictionary.java
File metadata and controls
137 lines (122 loc) · 3.73 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
package DictionaryTypes;
import java.util.*;
import DictionaryTypes.abstractClasses.Dictionary;
import DictionaryTypes.TrieStructureComponents.*;
public class TrieDictionary extends Dictionary implements DictionaryInterface {
public String Array[];
public int counter;
private State root;
public TrieDictionary() {
root = new State();
Array = new String[30000];
this.counter = 0;
}
/* SPECIFIC METHODS - Hand in 2 */
public ArrayList<Character> BFS() {
// todo
return null;
}
public ArrayList<Character> DFS() {
// todo
return null;
}
/* GENERAL METHODS - Hand in 1 */
public void removeWord(String word1) {
// todo
String[] newArr = new String[counter - 1];
int index = 0;
for (int i = 0; i < counter; i++) {
if (Array[i].equals(word1)) {
index = i;
counter--;
}
}
String[] anotherArray = new String[Array.length - 1];
// Copy the elements except the index
// from original array to the other array
for (int i = 0, k = 0; i < Array.length; i++) {
// if the index is
// the removal element index
if (i == index) {
continue;
}
// if the index is not
// the removal element index
anotherArray[k++] = Array[i];
}
Array = Arrays.copyOf(anotherArray, Array.length);
}
public void CreateDictionary(String filePath) {
// todo
In file = new In(filePath);
while (file.hasNextLine()) {
String word = file.readLine();
if (!(isWord(word))) {
Array[counter++] = word;
}
}
}
public String findCommonPrefix(String word) {
// todo
String commonPrefix = "";
for (int i = 0; i < counter; i++) {
String temp = "";
int sizeSmall = Math.min(word.length(), Array[i].length());
for (int j = 0; j < sizeSmall; j++) {
if (Array[i].charAt(j) == word.charAt(j)) {
temp += word.charAt(j);
if (temp.length() > commonPrefix.length()) {
commonPrefix = temp;
}
}
}
}
return commonPrefix;
}
public boolean isWord(String word) {
// todo
for (int i = 0; i < counter; i++) {
if (Array[i].equals(word)) {
return true;
}
}
return false;
}
public void addNewWord(String w) {
// todo
if (!isWord(w)) {
if (counter != Array.length) {
Array[counter++] = w;
}
}
}
public ArrayList<String> getDictionaryWords() {
// todo
ArrayList<String> text = new ArrayList<String>();
for (int i = 0; i < counter; i++) {
text.add(Array[i]);
}
return text;
}
public int getNumberOfElements() {
// todo
return counter;
}
public String[] getTopNSuggestions(ArrayList<String> dictionaryWords, String word, int N) {
// todo
String[] Arr = dictionaryWords.toArray(new String[dictionaryWords.size()]);
String[] A = {};
int count = 0, diff = 0;
while (count <= word.length()) {
for (int i = 0; i < Arr.length; i++) {
if ((this.getLevenshteinDistance(Arr[i], word) == diff && A.length < N)) {
A = Arrays.copyOf(A, A.length + 1);
A[A.length - 1] = Arr[i];
}
}
diff++;
count++;
}
return A;
}
}