UNPKG

@technobuddha/library

Version:
59 lines (58 loc) 2.24 kB
"use strict"; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.diceCoefficient = void 0; var compareStrings_1 = require("../compareStrings"); /** * Compute the dice coefficient measure of similarity between two strings * * @param input The first string * @param compareTo The second string * @param __nameParameters see {@link Options} * @return a number from 0 (not similar) to 1 (equal) measuring the similarity */ function diceCoefficient(input, compareTo, _a) { var e_1, _b; var _c = _a === void 0 ? {} : _a, _d = _c.caseInsensitive, caseInsensitive = _d === void 0 ? false : _d; if (input.length <= 1 || compareTo.length <= 1) return compareStrings_1.compareStrings(input, compareTo, { caseInsensitive: caseInsensitive }) === 0 ? 1.0 : 0.0; var bg0 = biGrams(caseInsensitive ? input.toLowerCase() : input); var bg1 = biGrams(caseInsensitive ? compareTo.toLowerCase() : compareTo); var count = 0; try { for (var bg0_1 = __values(bg0), bg0_1_1 = bg0_1.next(); !bg0_1_1.done; bg0_1_1 = bg0_1.next()) { var bg = bg0_1_1.value; var pos = bg1.indexOf(bg); if (pos >= 0) { count += 1; bg1[pos] = null; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (bg0_1_1 && !bg0_1_1.done && (_b = bg0_1.return)) _b.call(bg0_1); } finally { if (e_1) throw e_1.error; } } return count * 2 / (bg0.length + bg1.length); } exports.diceCoefficient = diceCoefficient; function biGrams(input) { var biGram = []; for (var i = 0; i < input.length - 1; ++i) biGram.push(input.slice(i, i + 2)); return biGram; } exports.default = diceCoefficient;