1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 var EXPORTED_SYMBOLS = ["NLP"];
10 * NLP, which stands for Natural Language Processing, is a module that provides
11 * an entry point to various methods to interface with human language.
13 * At least, that's the goal. Eventually. Right now, the find toolbar only really
14 * needs the Levenshtein distance algorithm.
18 * Calculate the Levenshtein distance between two words.
19 * The implementation of this method was heavily inspired by
20 * http://locutus.io/php/strings/levenshtein/index.html
23 * @param {String} word1 Word to compare against
24 * @param {String} word2 Word that may be different
25 * @param {Number} costIns The cost to insert a character
26 * @param {Number} costRep The cost to replace a character
27 * @param {Number} costDel The cost to delete a character
30 levenshtein(word1 = "", word2 = "", costIns = 1, costRep = 1, costDel = 1) {
31 if (word1 === word2) {
35 let l1 = word1.length;
36 let l2 = word2.length;
44 let p1 = new Array(l2 + 1);
45 let p2 = new Array(l2 + 1);
47 let i1, i2, c0, c1, c2, tmp;
49 for (i2 = 0; i2 <= l2; i2++) {
50 p1[i2] = i2 * costIns;
53 for (i1 = 0; i1 < l1; i1++) {
54 p2[0] = p1[0] + costDel;
56 for (i2 = 0; i2 < l2; i2++) {
57 c0 = p1[i2] + (word1[i1] === word2[i2] ? 0 : costRep);
58 c1 = p1[i2 + 1] + costDel;
64 c2 = p2[i2] + costIns;