-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
198 lines (185 loc) · 4.47 KB
/
app.js
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
197
198
class Node {
constructor(value) {
this.data = value;
this.children = {};
this.isWord = false;
this.prefixes = 0;
}
}
class Trie {
constructor(value) {
this.root = new Node("");
}
add(value) {
if (!this.root) {
return null;
} else {
this._addValue(this.root, value);
}
}
_addValue(node, value) {
node.prefixes++;
let char = value[0];
let child = node.children[char];
if (!child) {
child = new Node(char);
node.children[char] = child;
}
let remainder = value.substring(1);
if (remainder) {
this._addValue(child, remainder);
} else {
child.isWord = true;
}
}
isEmpty(obj) {
if (Object.keys(obj).length === 0) {
return true;
} else {
return false;
}
}
predict(value) {
if (!this.root) {
return null;
} else {
return this._predict(this.root, value);
}
}
getValue(node, value) {
let arr = [];
let str = value;
let _getValue = function (newNode, value) {
for (let key in newNode.children) {
let child = newNode.children[key];
str = value + key;
if (child.isWord) {
arr.push(str);
}
_getValue(child, str);
}
};
_getValue(node, value);
return arr;
}
_predict(node, value) {
//debugger;
let count = 0;
let char = value[count];
let child = node.children[char];
while (child) {
count++;
node = node.children[char];
char = value[count];
if (!char) {
return this.getValue(node, value);
}
child = node.children[char];
}
}
}
word = document.getElementById("word");
answer = document.getElementById("answer");
addbtn = document.getElementById("add");
predictbtn = document.getElementById("predict");
var mydata = JSON.parse(data);
var dictionary = [];
mydata.forEach((data) => {
dictionary.push(data.word);
entry = dictionary[dictionary.length - 1];
var element = document.createElement("li");
element.appendChild(document.createTextNode(entry));
document.getElementById("dictitems").appendChild(element);
});
console.log(dictionary);
function heard() {
let trie = new Trie();
trie.add("America");
trie.add("Australia");
trie.add("Austria");
trie.add("Butterfly");
trie.add("Baseball");
trie.add("Box");
trie.add("Car");
trie.add("Cat");
trie.add("Chocolate");
trie.add("Coin");
trie.add("Deer");
trie.add("Dog");
trie.add("Door");
trie.add("Drum");
trie.add("Elephant");
trie.add("Egg");
trie.add("Flower");
trie.add("Fan");
trie.add("Goat");
trie.add("Garlic");
trie.add("Horse");
trie.add("Hat");
trie.add("Iron");
trie.add("Igloo");
trie.add("Jam");
trie.add("Jellyfish");
trie.add("Kettle");
trie.add("Kite");
trie.add("Lion");
trie.add("Lamp");
trie.add("Moon");
trie.add("Man");
trie.add("Needle");
trie.add("Night");
trie.add("Oven");
trie.add("Ocean");
trie.add("Pan");
trie.add("Paper");
trie.add("Pine");
trie.add("Queen");
trie.add("Rat");
trie.add("Right");
trie.add("Sand");
trie.add("Sea");
trie.add("Tea");
trie.add("Time");
trie.add("Umbrella");
trie.add("Utensils");
trie.add("Van");
trie.add("Void");
trie.add("Wind");
trie.add("Work");
trie.add("Year");
trie.add("Yeast");
trie.add("Zebra");
if (trie.predict(word.value)) {
x = trie.predict(word.value);
word.value = x;
dictionary = [...dictionary, ...x];
console.log(dictionary);
}
}
function addtodict() {
if (word.value != "undefined" || word.value != "") {
var regex = /^[A-Za-z0-9 ]+$/;
var isValid = regex.test(word.value);
if (!isValid) {
entry1 = dictionary[dictionary.length - 1];
entry2 = dictionary[dictionary.length - 2];
var element1 = document.createElement("li");
element1.appendChild(document.createTextNode(entry1));
var element2 = document.createElement("li");
element2.appendChild(document.createTextNode(entry2));
document.getElementById("dictitems").appendChild(element1);
document.getElementById("dictitems").appendChild(element2);
answer.style.display = "none";
alert("Successfully Added");
}
else {
entry = dictionary[dictionary.length - 1];
var element = document.createElement("li");
element.appendChild(document.createTextNode(entry));
document.getElementById("dictitems").appendChild(element);
answer.style.display = "none";
alert("Successfully Added");
}
}
word.value = "";
}