Skip to content

Commit 0d36372

Browse files
committed
🦦 fix longestUncommonInfixes and jaroSimilarity (fixes #12)
1 parent cfc0f0e commit 0d36372

3 files changed

Lines changed: 67 additions & 23 deletions

File tree

‎deno.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nodef/extra-string",
3-
"version": "2.5.0",
3+
"version": "2.5.1",
44
"license": "MIT",
55
"exports": "./index.ts",
66
"exclude": [

‎index.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ import {
4646
toSlugCase,
4747
toSuperscript,
4848
tverskyDistance,
49+
jaroSimilarity,
50+
jaroWinklerSimilarity,
51+
levenshteinDistance,
52+
damerauLevenshteinDistance,
4953
} from "./index.ts";
5054

5155

@@ -1219,6 +1223,8 @@ Deno.test("longestUncommonInfixes", () => {
12191223
assertEquals(b, ["i", "o"]);
12201224
const c = longestUncommonInfixes("chocolatier", "engineer");
12211225
assertEquals(c, ["chocolati", "engine"]);
1226+
assertEquals(longestUncommonInfixes("hello", "hello"), ['', '']);
1227+
assertEquals(longestUncommonInfixes("on", "onion"), ['', 'ion']);
12221228
});
12231229

12241230

@@ -1310,3 +1316,34 @@ Deno.test("tverskyDistance", () => {
13101316
const c = tverskyDistance("pikachu", "raichu", 3, 0.2, 0.4);
13111317
assertEquals(c, 0.6666666666666667);
13121318
});
1319+
1320+
1321+
1322+
1323+
Deno.test("jaroSimilarity", () => {
1324+
assertEquals(jaroSimilarity("a", "a"), 1);
1325+
assertEquals(jaroSimilarity("sparrow", "arrow"), 0.9047619047619048);
1326+
assertEquals(jaroSimilarity("arrow", "sparrow"), 0.9047619047619048);
1327+
});
1328+
1329+
1330+
1331+
1332+
Deno.test("jaroWinklerSimilarity", () => {
1333+
assertEquals(jaroWinklerSimilarity("a", "a"), 1);
1334+
});
1335+
1336+
1337+
1338+
1339+
Deno.test("levenshteinDistance", () => {
1340+
assertEquals(levenshteinDistance("on", "onion"), 3);
1341+
assertEquals(levenshteinDistance("aa", "abbaa"), 3);
1342+
});
1343+
1344+
1345+
1346+
1347+
Deno.test("damerauLevenshteinDistance", () => {
1348+
assertEquals(damerauLevenshteinDistance("on", "onion"), 3);
1349+
});

‎index.ts‎

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,12 +1176,12 @@ export function longestCommonSuffix(x: string, y: string): string {
11761176
* ```
11771177
*/
11781178
export function longestUncommonInfixes(x: string, y: string): [string, string] {
1179-
const X = x.length, Y = y.length;
11801179
const i = longestCommonPrefix(x, y).length;
1181-
const j = longestCommonSuffix(x, y).length;
1182-
return [x.substring(i, X-j), y.substring(i, Y-j)];
1183-
}
1184-
//#endregion
1180+
const xs = x.substring(i);
1181+
const ys = y.substring(i);
1182+
const j = longestCommonSuffix(xs, ys).length;
1183+
return [x.substring(i, x.length - j), y.substring(i, y.length - j)];
1184+
}//#endregion
11851185

11861186

11871187

@@ -2042,20 +2042,24 @@ export function tverskyDistance(x: string, y: string, n: number, a: number=1, b:
20422042
* @returns [matching characters in x, matching characters in y]
20432043
*/
20442044
function rangedMatches(x: string, y: string, d: number): [string, string] {
2045-
// 1. Prepare strike-off buffer
2046-
let mx = "", my = "";
2045+
let mx = "";
2046+
let my = "";
20472047
const by: (string | null)[] = Array.from(y);
2048-
// 2. Get matches in first string
2049-
for (let i=0, I=x.length; i<I; i++) {
2050-
let j = by.lastIndexOf(x.charAt(i), i);
2051-
if (j<0 || i-j>d) j = by.indexOf(x.charAt(i), i+1);
2052-
if (j<0 || j-i>d) continue;
2053-
mx += x.charAt(i);
2054-
by[j] = null;
2048+
for (let i = 0; i < x.length; i++) {
2049+
const c = x.charAt(i);
2050+
const lo = Math.max(0, i - d);
2051+
const hi = Math.min(y.length - 1, i + d);
2052+
for (let j = lo; j <= hi; j++) {
2053+
if (by[j] === c) {
2054+
mx += c;
2055+
by[j] = null;
2056+
break;
2057+
}
2058+
}
2059+
}
2060+
for (let j = 0; j < y.length; j++) {
2061+
if (by[j] === null) my += y.charAt(j);
20552062
}
2056-
// 3. Get matches in second string
2057-
for (let j=0, J=y.length; j<J; j++)
2058-
if (by[j]==null) my += y.charAt(j);
20592063
return [mx, my];
20602064
}
20612065
// countRangedMatches()?
@@ -2079,11 +2083,14 @@ function rangedMatches(x: string, y: string, d: number): [string, string] {
20792083
* ```
20802084
*/
20812085
export function jaroSimilarity(x: string, y: string): number {
2082-
const X = x.length, Y = y.length;
2083-
const d = Math.floor(Math.max(X, Y)/2) - 1;
2084-
const [mx, my] = rangedMatches(x, y, d), m = mx.length;
2085-
const t = hammingDistance(mx, my)/2;
2086-
return m? (m/X + m/Y + (m-t)/m)/3 : 0;
2086+
const X = x.length;
2087+
const Y = y.length;
2088+
if (X === 0 && Y === 0) return 1;
2089+
const d = Math.max(0, Math.floor(Math.max(X, Y) / 2) - 1);
2090+
const [mx, my] = rangedMatches(x, y, d);
2091+
const m = mx.length;
2092+
const t = hammingDistance(mx, my) / 2;
2093+
return m ? (m / X + m / Y + (m - t) / m) / 3 : 0;
20872094
}
20882095

20892096

0 commit comments

Comments
 (0)