123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.getSpellingSuggestion = void 0;
- function getSpellingSuggestion(name, candidates, getName) {
- const maximumLengthDifference = Math.min(2, Math.floor(name.length * 0.34));
-
- let bestDistance = Math.floor(name.length * 0.4) + 1;
- let bestCandidate;
- let justCheckExactMatches = false;
- const nameLowerCase = name.toLowerCase();
-
- for (const candidate of candidates) {
- const candidateName = getName(candidate);
- if (candidateName !== undefined &&
- Math.abs(candidateName.length - nameLowerCase.length) <=
- maximumLengthDifference) {
- const candidateNameLowerCase = candidateName.toLowerCase();
- if (candidateNameLowerCase === nameLowerCase) {
- if (candidateName === name) {
- continue;
- }
- return candidate;
- }
- if (justCheckExactMatches) {
- continue;
- }
- if (candidateName.length < 3) {
-
-
- continue;
- }
-
- const distance = levenshteinWithMax(nameLowerCase, candidateNameLowerCase, bestDistance - 1);
- if (distance === undefined) {
- continue;
- }
- if (distance < 3) {
- justCheckExactMatches = true;
- bestCandidate = candidate;
- }
- else {
-
- bestDistance = distance;
- bestCandidate = candidate;
- }
- }
- }
- return bestCandidate;
- }
- exports.getSpellingSuggestion = getSpellingSuggestion;
- function levenshteinWithMax(s1, s2, max) {
- let previous = new Array(s2.length + 1);
- let current = new Array(s2.length + 1);
-
- const big = max + 1;
- for (let i = 0; i <= s2.length; i += 1) {
- previous[i] = i;
- }
- for (let i = 1; i <= s1.length; i += 1) {
- const c1 = s1.charCodeAt(i - 1);
- const minJ = i > max ? i - max : 1;
- const maxJ = s2.length > max + i ? max + i : s2.length;
- current[0] = i;
-
- let colMin = i;
- for (let j = 1; j < minJ; j += 1) {
- current[j] = big;
- }
- for (let j = minJ; j <= maxJ; j += 1) {
- const dist = c1 === s2.charCodeAt(j - 1)
- ? previous[j - 1]
- : Math.min(
- previous[j] + 1,
- current[j - 1] + 1,
- previous[j - 1] + 2);
- current[j] = dist;
- colMin = Math.min(colMin, dist);
- }
- for (let j = maxJ + 1; j <= s2.length; j += 1) {
- current[j] = big;
- }
- if (colMin > max) {
-
-
- return undefined;
- }
- const temp = previous;
- previous = current;
- current = temp;
- }
- const res = previous[s2.length];
- return res > max ? undefined : res;
- }
|