-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
277 lines (238 loc) · 7.6 KB
/
index.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
function Vertex(word) {
this.word = word;
this.adjacent = new Set();
}
function WordGraph(validWords, ops) {
this.validWords = validWords;
this.ops = ops;
this.map = new Map();
};
function WordTrace(graph, startWord, endWord, ops) {
this.graph = graph;
this.startWord = startWord;
this.endWord = endWord;
this.ops = ops;
this.parents = new Map();
}
async function loadWords(filename) {
const res = await fetch(filename);
const text = await res.text();
return new Set(text.split("\n"));
}
async function submitClick(g) {
// Read in words from input fields
const startWord = document.getElementById("start").value.trim().toLowerCase();
const endWord = document.getElementById("end").value.trim().toLowerCase();
// Hide output and clear trace
const outputbox = document.getElementById("outputbox");
outputbox.hidden = true;
const tracebox = document.getElementById("tracebox");
tracebox.innerHTML = "";
// Display goal path
const goal = document.getElementById("goal");
goal.innerText = `${startWord} \u2192 ${endWord}`;
// Check for new operations
const newops = [false, false, false];
if (document.getElementById("set").checked) {
newops[0] = true;
}
if (document.getElementById("add").checked) {
newops[1] = true;
}
if (document.getElementById("remove").checked) {
newops[2] = true;
}
// If nothing in map, or operations have changed
let needsRebuild = false;
if (g.map.size == 0 || g.ops.toString() != newops.toString()) {
needsRebuild = true;
g.ops = newops;
}
// Check for input errors (see docs)
if (!g.validWords.has(startWord)) {
logResult(2, null);
return g;
}
if (!g.validWords.has(endWord)) {
logResult(3, null);
return g;
}
startLen = startWord.length;
endLen = endWord.length;
if (!((startWord == endWord) ||
((startLen == endLen) && (g.ops[0] || (g.ops[1] && g.ops[2]))) ||
((startLen < endLen) && g.ops[1]) ||
((startLen > endLen) && g.ops[2]))) {
logResult(4, null);
return g;
}
// Build graph if needed, then log search path
if (needsRebuild) {
const overlay = document.getElementById("overlay");
overlay.hidden = false;
g = await buildGraph(g.validWords, g.ops);
overlay.hidden = true;
}
const t = new WordTrace(g, startWord, endWord, g.ops);
logResult(BFS(t), t);
return g;
}
async function buildGraph(validWords, ops) {
const g = new WordGraph(validWords, ops);
for (const word of validWords) {
const v = new Vertex(word);
g.map.set(word, v);
}
return await buildLoop(g);
}
async function buildLoop(g) {
const progressBar = document.getElementById("progressbar");
let wordi = 0;
let interval = Math.floor(g.map.size / 100);
let percentage = 0;
for (const v of g.map.values()) {
v.adjacent = getAdjacent(v.word, g);
// If reached interval to update
if (wordi % interval == 0) {
progressBar.style.width = percentage + "%";
percentage++;
// Force style attribute to update
await new Promise(r => setTimeout(r, 1));
}
wordi++;
}
return g;
}
function getAdjacent(word, g) {
const alphabet = "abcdefghijklmnopqrstuvwxyz";
const adjacent = new Set();
for (let ichar = 0; ichar < word.length; ichar++) {
if (g.ops[2]) {
const rmAdjacent = word.slice(0, ichar) + word.slice(ichar + 1);
adjacent.add(rmAdjacent);
}
if (g.ops[0]) {
for (const letter of alphabet) {
const setAdjacent = word.slice(0, ichar) + letter + word.slice(ichar + 1);
adjacent.add(setAdjacent);
}
}
}
if (g.ops[1]) {
for (let ipos = 0; ipos < word.length + 1; ipos++) {
for (const letter of alphabet) {
const addAdjacent = word.slice(0, ipos) + letter + word.slice(ipos);
adjacent.add(addAdjacent);
}
}
}
const validAdjacent = new Set();
for (const adj of adjacent) {
if (g.validWords.has(adj)) {
validAdjacent.add(g.map.get(adj));
}
}
validAdjacent.delete(g.map.get(word));
return validAdjacent;
}
function BFS(t) {
const queue = [];
const visited = new Set();
const start = t.graph.map.get(t.startWord);
queue.push(start);
visited.add(start);
if (t.startWord == t.endWord) {
return 0;
}
while (true) {
const current = queue.shift();
if (current == undefined) {
return 1;
}
for (const adj of current.adjacent) {
if (visited.has(adj)) {
continue;
}
if (adj.word == t.endWord) {
t.parents.set(adj, current);
return 0;
}
t.parents.set(adj, current);
queue.push(adj);
visited.add(adj);
}
}
}
async function logResult(result, t) {
const goal = document.getElementById("goal");
const status = document.getElementById("status");
const outputbox = document.getElementById("outputbox");
switch (result) {
case 0:
status.innerText = "\u{2705}";
goal.style.color = "green";
break;
case 1:
status.innerText = "\u{274C}";
goal.style.color = "red";
outputbox.hidden = false;
return;
case 2:
status.innerText = "\u{26A0} Word 1"
goal.style.color = "orange";
outputbox.hidden = false;
return;
case 3:
status.innerText = "\u{26A0} Word 2"
goal.style.color = "orange";
outputbox.hidden = false;
return;
case 4:
status.innerText = "\u{26A0}"
goal.style.color = "orange";
outputbox.hidden = false;
return;
}
// Get list of word strings
const trace = [];
let current = t.graph.map.get(t.endWord);
while (current != null) {
trace.push(current.word);
current = t.parents.get(current);
}
const nSteps = trace.length;
const distance = nSteps - 1;
goal.innerText += ` (${distance})`;
// Put words in trace box with definition alt text
const tracebox = document.getElementById("tracebox");
for (let i = 0; i < nSteps; i++) {
const step = document.createElement("div");
step.className = "step";
const word = trace.pop()
// API Call
const response = await fetch(`https://api.dictionaryapi.dev/api/v2/entries/en/${word}`);
const data = await response.json();
if (data.title == "No Definitions Found") {
step.setAttribute("title", "No definition found.")
} else {
const partOfSpeech = data[0].meanings[0].partOfSpeech;
const definition = data[0].meanings[0].definitions[0].definition;
step.setAttribute("title", partOfSpeech + "\n" + definition);
}
step.innerText = word;
tracebox.appendChild(step);
}
// Only show output box when all definitions fetched
outputbox.hidden = false;
}
async function main() {
const filename = "wordsets/words-58k.txt";
const validWords = await loadWords(filename);
let g = new WordGraph(validWords, null);
const submit = document.getElementById("submit");
submit.addEventListener("click", async function () {
g = await submitClick(g);
}
);
}
main();