-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdictionary_worker.js
67 lines (54 loc) · 1.5 KB
/
dictionary_worker.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
"use strict";
const dictionaries = new Map();
const distance = (a, b) => {
if (a.length == 0) {
return b.length;
}
if (b.length == 0) {
return a.length;
}
const matrix = [];
// increment along the first column of each row
let i;
for (i = 0; i <= b.length; i++){
matrix[i] = [i];
}
// increment each column in the first row
let j;
for (j = 0; j <= a.length; j++){
matrix[0][j] = j;
}
// fill in the rest of the matrix
for (i = 1; i <= b.length; i++){
for (j = 1; j <= a.length; j++){
if (b.charAt(i-1) == a.charAt(j-1)){
matrix[i][j] = matrix[i-1][j-1];
} else {
matrix[i][j] = Math.min(matrix[i-1][j-1] + 1, // substitution
Math.min(matrix[i][j-1] + 1, // insertion
matrix[i-1][j] + 1)); // deletion
}
}
}
return matrix[b.length][a.length];
}
self.onmessage = async event => {
const [dictionaryName, requestType, str, numToReturn] = event.data;
if (!dictionaries.has(dictionaryName)) {
const data = await fetch("/dictionaries/" + dictionaryName + ".json");
const dictionary = await data.json();
dictionaries.set(dictionaryName, dictionary);
}
const dictionary = dictionaries.get(dictionaryName);
if (requestType == "lookup") {
postMessage(dictionary[str] || null);
} else if (requestType == "nearest") {
const pairs = [];
for (let word in dictionary) {
pairs.push([distance(str, word), word]);
}
const sorted = pairs.sort((a, b) => a[0] - b[0]);
const rtn = sorted.slice(0, numToReturn).map(pair => pair[1]);
postMessage(rtn);
}
};