118 lines
3.2 KiB
JavaScript
118 lines
3.2 KiB
JavaScript
/**
|
|
* The MIT License (MIT)
|
|
*
|
|
* Copyright (c) 2015 Jordan Thomas
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
const optionDefault = (template, options) => {
|
|
for (let prop in options)
|
|
if (options.hasOwnProperty(prop))
|
|
template[prop] = options[prop];
|
|
|
|
return template;
|
|
};
|
|
|
|
export const jaro_winkler = function (a, b, options) {
|
|
// Load default options
|
|
options = optionDefault({"caseSensitive": true}, options);
|
|
|
|
// Convert to lowercase if not case-sensitive
|
|
if (!options.caseSensitive) {
|
|
a = a.toLowerCase();
|
|
b = b.toLowerCase();
|
|
}
|
|
|
|
|
|
const aLen = a.length;
|
|
const bLen = b.length;
|
|
|
|
// Short-circuit if either is empty
|
|
if (aLen === 0 || bLen === 0)
|
|
return 0;
|
|
|
|
// Short-circuit if exact match
|
|
if (a === b)
|
|
return 1;
|
|
|
|
|
|
// Calculate difference
|
|
const range = (Math.floor(Math.max(aLen, bLen) / 2)) - 1;
|
|
const aMatches = [];
|
|
const bMatches = [];
|
|
let low;
|
|
let high;
|
|
let m = 0;
|
|
|
|
// Find matches
|
|
for (let i = 0; i < aLen; i++) {
|
|
low = ((i >= range) ? i - range : 0);
|
|
high = ((i + range <= bLen) ? (i + range) : (bLen - 1));
|
|
|
|
for (let j = low; j <= high; j++) {
|
|
if (aMatches[i] !== true && bMatches[j] !== true && a[i] === b[j]) {
|
|
aMatches[i] = true;
|
|
bMatches[j] = true;
|
|
m++;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Short-circuit if not matches found
|
|
if (m === 0)
|
|
return 0;
|
|
|
|
// Count transpositions
|
|
let k = 0;
|
|
let numTrans = 0;
|
|
|
|
for (let i = 0; i < aLen; i++) {
|
|
if (aMatches[i] === true) {
|
|
let j;
|
|
for (j = k; j < bLen; j++) {
|
|
if (bMatches[j] === true) {
|
|
k = j + 1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (a[i] !== b[j]) {
|
|
numTrans++;
|
|
}
|
|
}
|
|
}
|
|
|
|
let weight = (((m / aLen) + (m / bLen) + (m - (numTrans / 2)) / m) / 3);
|
|
let l = 0;
|
|
const p = 0.1;
|
|
|
|
if (weight > 0.7) {
|
|
while (a[l] === b[l] && l < 4)
|
|
l++;
|
|
|
|
weight += l * p * (1 - weight);
|
|
}
|
|
|
|
return weight;
|
|
};
|