suggestion.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // @see: https://github.com/microsoft/TypeScript/blob/master/src/compiler/checker.ts
  2. /**
  3. * Given a name and a list of names that are not equal to the name, return a
  4. * spelling suggestion if there is one that is close enough. Names less than
  5. * length 3 only check for case-insensitive equality, not Levenshtein distance.
  6. *
  7. * - If there is a candidate that's the same except for case, return that.
  8. * - If there is a candidate that's within one edit of the name, return that.
  9. * - Otherwise, return the candidate with the smallest Levenshtein distance,
  10. * except for candidates:
  11. * * With no name
  12. * * Whose length differs from the target name by more than 0.34 of the
  13. * length of the name.
  14. * * Whose levenshtein distance is more than 0.4 of the length of the
  15. * name (0.4 allows 1 substitution/transposition for every 5 characters,
  16. * and 1 insertion/deletion at 3 characters)
  17. */
  18. export function getSpellingSuggestion(name, candidates, getName) {
  19. const maximumLengthDifference = Math.min(2, Math.floor(name.length * 0.34));
  20. // If the best result isn't better than this, don't bother.
  21. let bestDistance = Math.floor(name.length * 0.4) + 1;
  22. let bestCandidate;
  23. let justCheckExactMatches = false;
  24. const nameLowerCase = name.toLowerCase();
  25. // eslint-disable-next-line
  26. for (const candidate of candidates) {
  27. const candidateName = getName(candidate);
  28. if (candidateName !== undefined &&
  29. Math.abs(candidateName.length - nameLowerCase.length) <=
  30. maximumLengthDifference) {
  31. const candidateNameLowerCase = candidateName.toLowerCase();
  32. if (candidateNameLowerCase === nameLowerCase) {
  33. if (candidateName === name) {
  34. continue;
  35. }
  36. return candidate;
  37. }
  38. if (justCheckExactMatches) {
  39. continue;
  40. }
  41. if (candidateName.length < 3) {
  42. // Don't bother, user would have noticed a
  43. // 2-character name having an extra character.
  44. continue;
  45. }
  46. // Only care about a result better than the best so far.
  47. const distance = levenshteinWithMax(nameLowerCase, candidateNameLowerCase, bestDistance - 1);
  48. if (distance === undefined) {
  49. continue;
  50. }
  51. if (distance < 3) {
  52. justCheckExactMatches = true;
  53. bestCandidate = candidate;
  54. }
  55. else {
  56. // Debug.assert(distance < bestDistance)
  57. bestDistance = distance;
  58. bestCandidate = candidate;
  59. }
  60. }
  61. }
  62. return bestCandidate;
  63. }
  64. function levenshteinWithMax(s1, s2, max) {
  65. let previous = new Array(s2.length + 1); // eslint-disable-line
  66. let current = new Array(s2.length + 1); // eslint-disable-line
  67. /** Represents any value > max. We don't care about the particular value. */
  68. const big = max + 1;
  69. for (let i = 0; i <= s2.length; i += 1) {
  70. previous[i] = i;
  71. }
  72. for (let i = 1; i <= s1.length; i += 1) {
  73. const c1 = s1.charCodeAt(i - 1);
  74. const minJ = i > max ? i - max : 1;
  75. const maxJ = s2.length > max + i ? max + i : s2.length;
  76. current[0] = i;
  77. /** Smallest value of the matrix in the ith column. */
  78. let colMin = i;
  79. for (let j = 1; j < minJ; j += 1) {
  80. current[j] = big;
  81. }
  82. for (let j = minJ; j <= maxJ; j += 1) {
  83. const dist = c1 === s2.charCodeAt(j - 1)
  84. ? previous[j - 1]
  85. : Math.min(
  86. /* delete */ previous[j] + 1,
  87. /* insert */ current[j - 1] + 1,
  88. /* substitute */ previous[j - 1] + 2);
  89. current[j] = dist;
  90. colMin = Math.min(colMin, dist);
  91. }
  92. for (let j = maxJ + 1; j <= s2.length; j += 1) {
  93. current[j] = big;
  94. }
  95. if (colMin > max) {
  96. // Give up -- everything in this column is > max
  97. // and it can't get better in future columns.
  98. return undefined;
  99. }
  100. const temp = previous;
  101. previous = current;
  102. current = temp;
  103. }
  104. const res = previous[s2.length];
  105. return res > max ? undefined : res;
  106. }
  107. //# sourceMappingURL=suggestion.js.map