Add a bit of documentation
This commit is contained in:
parent
86ce93d067
commit
796d4c7573
File diff suppressed because one or more lines are too long
|
@ -24,39 +24,28 @@
|
|||
|
||||
"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);
|
||||
|
||||
/**
|
||||
* Returns the similarity of the given strings.
|
||||
*
|
||||
* @param a {string} a string
|
||||
* @param b {string} a string
|
||||
* @param caseSensitive {boolean} whether the calculation should be case-sensitive
|
||||
* @returns {number} the similarity of the given strings
|
||||
*/
|
||||
export const jaroWinkler = function (a, b, caseSensitive = true) {
|
||||
// Convert to lowercase if not case-sensitive
|
||||
if (!options.caseSensitive) {
|
||||
if (!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;
|
||||
if (a.length === 0 || b.length === 0) return 0;
|
||||
if (a === b) return 1;
|
||||
|
||||
|
||||
// Calculate difference
|
||||
const range = (Math.floor(Math.max(aLen, bLen) / 2)) - 1;
|
||||
const range = Math.floor(Math.max(a.length, b.length) / 2) - 1;
|
||||
const aMatches = [];
|
||||
const bMatches = [];
|
||||
let low;
|
||||
|
@ -64,9 +53,9 @@ export const jaro_winkler = function (a, b, options) {
|
|||
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 i = 0; i < a.length; i++) {
|
||||
low = (i >= range) ? i - range : 0;
|
||||
high = (i + range <= b.length) ? (i + range) : (b.length - 1);
|
||||
|
||||
for (let j = low; j <= high; j++) {
|
||||
if (aMatches[i] !== true && bMatches[j] !== true && a[i] === b[j]) {
|
||||
|
@ -79,30 +68,27 @@ export const jaro_winkler = function (a, b, options) {
|
|||
}
|
||||
|
||||
// Short-circuit if not matches found
|
||||
if (m === 0)
|
||||
return 0;
|
||||
if (m === 0) return 0;
|
||||
|
||||
// Count transpositions
|
||||
let k = 0;
|
||||
let numTrans = 0;
|
||||
|
||||
for (let i = 0; i < aLen; i++) {
|
||||
for (let i = 0; i < a.length; i++) {
|
||||
if (aMatches[i] === true) {
|
||||
let j;
|
||||
for (j = k; j < bLen; j++) {
|
||||
for (j = k; j < b.length; j++) {
|
||||
if (bMatches[j] === true) {
|
||||
k = j + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (a[i] !== b[j]) {
|
||||
numTrans++;
|
||||
}
|
||||
if (a[i] !== b[j]) numTrans++;
|
||||
}
|
||||
}
|
||||
|
||||
let weight = (((m / aLen) + (m / bLen) + (m - (numTrans / 2)) / m) / 3);
|
||||
let weight = (m / a.length + m / b.length + (m - numTrans / 2) / m) / 3;
|
||||
let l = 0;
|
||||
const p = 0.1;
|
||||
|
Loading…
Reference in New Issue