feat(image-import): add inline levenshtein distance helper
This commit is contained in:
parent
93e6b32fe9
commit
773c286236
2 changed files with 59 additions and 0 deletions
|
|
@ -175,6 +175,34 @@ class ImageImporter {
|
|||
|
||||
return { month, year, entries: validEntries, notes };
|
||||
}
|
||||
|
||||
/**
|
||||
* Levenshtein distance (O(m*n) DP, inline).
|
||||
* Inputs are expected to already be normalized.
|
||||
* @param {string} a
|
||||
* @param {string} b
|
||||
* @returns {number}
|
||||
*/
|
||||
levenshtein(a, b) {
|
||||
if (a === b) return 0;
|
||||
if (!a.length) return b.length;
|
||||
if (!b.length) return a.length;
|
||||
const m = a.length, n = b.length;
|
||||
const dp = Array.from({ length: m + 1 }, () => new Array(n + 1).fill(0));
|
||||
for (let i = 0; i <= m; i++) dp[i][0] = i;
|
||||
for (let j = 0; j <= n; j++) dp[0][j] = j;
|
||||
for (let i = 1; i <= m; i++) {
|
||||
for (let j = 1; j <= n; j++) {
|
||||
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
||||
dp[i][j] = Math.min(
|
||||
dp[i - 1][j] + 1,
|
||||
dp[i][j - 1] + 1,
|
||||
dp[i - 1][j - 1] + cost
|
||||
);
|
||||
}
|
||||
}
|
||||
return dp[m][n];
|
||||
}
|
||||
}
|
||||
|
||||
// Verbatim system prompt — German with Umlaute (per spec §7.3).
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue