-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdist.js
executable file
·122 lines (107 loc) · 2.85 KB
/
dist.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
console.log("once");
importScripts('fastlev.js');
var dicts = {
raw: 0,
susp: 0
};
var loaded = 0
Promise.all([get("dict.json"), get("suspect.json")]).then(function (result) {
//console.dir(dicts);
dicts.raw = JSON.parse(result[0]);
dicts.susp = JSON.parse(result[1]);
console.log("dicts loaded!");
loaded = 1;
postMessage("ready");
});
var compare = async function (ocr, dict) {
//console.log(ocr);
/* if (!ocr) {
console.log("nocr");
return false;
}
if (hasNumber(ocr) && ocr.length < 3) {
console.log("numbery");
return alse;
}*/
var words = {};
var ranked = [];
var compdist;
var compdist = Math.floor(ocr.length / 4) + 1;
var cnt = 0;
for (var word of dict) {
cnt++;
var lowcr = ocr.toLowerCase();
var low = word.toLowerCase();
var dist = FastLev.get(lowcr, low);
if (dist === 0) {
console.log("perfect match");
return ({
'original': ocr,
'low': 0,
'words': [{
'word': word,
'distance': 0
}]
});
} else if (dist < compdist) {
ranked.push({
'word': word,
'distance': dist
});
}
}
//console.log(ranked);
var ranked = ranked.sort(acompare);
if (ranked.length) {
words.original = ocr;
var low = ranked[0].dist;
var high = ranked[ranked.length - 1];
words.low = low;
if (words.high > words.low) {
words.high = high;
}
words.words = ranked;
return words;
}
}
function acompare(a, b) {
if (a.distance < b.distance)
return -1;
else if (a.distance > b.distance)
return 1;
else
return 0;
}
onmessage = async function (input) {
var a = input.data.word;
var b = input.data.dict;
var res = await compare(a, dicts[b]);
//console.log(res);
postMessage(res);
}
function get(url) {
// Return a new promise.
return new Promise(function (resolve, reject) {
// Do the usual XHR stuff
var req = new XMLHttpRequest();
req.open("GET", url);
req.onload = function () {
// This is called even on 404 etc
// so check the status
if (req.status === 200) {
// Resolve the promise with the response text
resolve(req.response);
} else {
// Otherwise reject with the status text
// which will hopefully be a meaningful error
reject(Error(req.statusText));
}
};
// Handle network errors
req.onerror = function () {
reject(Error("Network Error"));
};
// Make the request
req.send();
});
}