-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
46 lines (38 loc) · 1.19 KB
/
worker.js
File metadata and controls
46 lines (38 loc) · 1.19 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
module.exports = (array, maxDistance) => {
const levenshtein = require('./levenshtein');
const totalCount = array.length;
const chunk = Math.ceil(totalCount / 10);
const result = {};
for (let i = 0; i < array.length; i++) {
if (i % chunk === 0) {
console.log("One of the works's progress: " + (i / chunk) * 10 + "%")
}
const element = array[i];
const keys = Object.keys(result);
if (keys.length === 0) {
result[element] = 0;
continue;
}
let added = false;
for (let j = 0; j < keys.length; j++) {
if (added) {
continue;
}
const key = keys[j];
const distance = levenshtein(key, element);
if (distance < maxDistance) {
result[key] += 1;
added = true;
}
}
if (!added) {
result[element] = 1;
}
}
const processedCount = Object.keys(result).reduce((acc, cv) => {
acc += result[cv];
return acc;
}, 0);
console.log(`One of the works is finished, processed ${processedCount} records`);
return result;
}