-
Notifications
You must be signed in to change notification settings - Fork 0
/
levenshtein.js
52 lines (40 loc) · 1.17 KB
/
levenshtein.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
// levensthein distance
const math = require('mathjs')
levenstheinDistance = function (x, y) {
n = x.length;
m = y.length;
if (n == 0) {
return m;
}
if (n == 0) {
return n;
}
//construct matrix 0 to A rows, 0 to B columns
matrix = math.zeros(n, m);
// add 1-num into matrix
for (i = 0; i <= n; i++) {
matrix.set([i, 0], i);
}
for (i = 0; i <= m; i++) {
matrix.set([0, i], i);
}
cost = 0;
// iterate and check
let si;
let tj;
for (i = 1; i <= n; i++) {
si = x[i - 1];
for (j = 1; j <= m; j++) {
tj = y[j - 1];
if (si == tj) {
cost = 0;
} else {
cost = 1;
}
matrix.set([i, j], Math.min(matrix.get([i - 1, j]) + 1, matrix.get([i, (j - 1)]) + 1, matrix.get([i - 1, j - 1]) + cost));
}
}
// console.log("return distance of" + matrix.get([n,m]));
return matrix.get([n,m]);
}
module.exports = levenstheinDistance;