SELECT `justfunctions.eu.fuzzy_distance_dam`("pyhtno", "python")
/*--Output--
2
*/
CREATE OR REPLACE FUNCTION `your_project_id.your_dataset_id.fuzzy_distance_dam`(`string_1` STRING, `string_2` STRING) RETURNS INT64
LANGUAGE js AS r'''const initMatrix = (string_1, string_2) => {
if (undefined == string_1 || undefined == string_2) {
return null;
}
let d = [];
for (let i = 0; i <= string_1.length; i++) {
d[i] = [];
d[i][0] = i;
}
for (let j = 0; j <= string_2.length; j++) {
d[0][j] = j;
}
return d;
};
const damerau = (i, j, string_1, string_2, d, cost) => {
if (i > 1 && j > 1 && string_1[i - 1] === string_2[j - 2] && string_1[i - 2] === string_2[j - 1]) {
d[i][j] = Math.min.apply(null, [d[i][j], d[i - 2][j - 2] + cost]);
}
};
if (
undefined == string_1 ||
undefined == string_2 ||
"string" !== typeof string_1 ||
"string" !== typeof string_2
) {
return -1;
}
let d = initMatrix(string_1, string_2);
if (null === d) {
return -1;
}
for (var i = 1; i <= string_1.length; i++) {
let cost;
for (let j = 1; j <= string_2.length; j++) {
if (string_1.charAt(i - 1) === string_2.charAt(j - 1)) {
cost = 0;
} else {
cost = 1;
}
d[i][j] = Math.min.apply(null, [
d[i - 1][j] + 1,
d[i][j - 1] + 1,
d[i - 1][j - 1] + cost
]);
damerau(i, j, string_1, string_2, d, cost);
}
}
return d[string_1.length][string_2.length];''' OPTIONS ( description = '''Calculates Damerau-Levenshtein distance between <string_1> and <string_2>.''' )
JavaScript User Defined Function (JavaScript UDF)
See something wrong? Contact us or report an issue on Github.