similarity.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. function compareTwoStrings(first, second) {
  2. if ((first = first.replace(/\s+/g, '')) === (second = second.replace(/\s+/g, ''))) return 1;
  3. if (first.length < 2 || second.length < 2) return 0;
  4. const firstBigrams = new Map();
  5. for (let i = 0; i < first.length - 1; i++) {
  6. const bigram = first.substring(i, i + 2),
  7. count = firstBigrams.has(bigram) ? firstBigrams.get(bigram) + 1 : 1;
  8. firstBigrams.set(bigram, count);
  9. }
  10. let intersectionSize = 0;
  11. for (let i = 0; i < second.length - 1; i++) {
  12. const bigram = second.substring(i, i + 2),
  13. count = firstBigrams.has(bigram) ? firstBigrams.get(bigram) : 0;
  14. 0 < count && (firstBigrams.set(bigram, count - 1), intersectionSize++);
  15. }
  16. return (2 * intersectionSize) / (first.length + second.length - 2);
  17. }
  18. function findBestMatch(mainString, targetStrings) {
  19. const ratings = [];
  20. let bestMatchIndex = 0;
  21. for (let i = 0; i < targetStrings.length; i++) {
  22. const currentTargetString = targetStrings[i],
  23. currentRating = compareTwoStrings(mainString, currentTargetString);
  24. ratings.push({ target: currentTargetString, rating: currentRating }), currentRating > ratings[bestMatchIndex].rating && (bestMatchIndex = i);
  25. }
  26. return { ratings: ratings, bestMatch: ratings[bestMatchIndex], bestMatchIndex: bestMatchIndex };
  27. }
  28. function lcs(str1, str2) {
  29. let j;
  30. let sequence;
  31. let str1Length = str1.length;
  32. let str2Length = str2.length;
  33. let num = new Array(str1Length);
  34. let maxlen;
  35. let lastSubsBegin;
  36. let i;
  37. if (!str1 || !str2) return { length: 0, sequence: '', offset: 0 };
  38. for (; i < str1Length; i++) {
  39. let subArray = new Array(str2Length);
  40. for (; j < str2Length; j++) subArray[j] = 0;
  41. num[i] = subArray;
  42. }
  43. let thisSubsBegin = null;
  44. i = 0;
  45. for (; i < str1Length; i++) for (j = 0; j < str2Length; j++) str1[i] !== str2[j] ? (num[i][j] = 0) : ((num[i][j] = 0 === i || 0 === j ? 1 : 1 + num[i - 1][j - 1]), num[i][j] > maxlen && ((maxlen = num[i][j]), lastSubsBegin === (thisSubsBegin = i - num[i][j] + 1) ? (sequence += str1[i]) : ((lastSubsBegin = thisSubsBegin), (sequence = ''), (sequence += str1.substr(lastSubsBegin, i + 1 - lastSubsBegin)))));
  46. return { length: maxlen, sequence: sequence, offset: thisSubsBegin };
  47. }
  48. function findBestLCS(mainString, targetStrings) {
  49. const results = [];
  50. let bestMatchIndex = 0;
  51. for (let i = 0; i < targetStrings.length; i++) {
  52. const currentTargetString = targetStrings[i],
  53. currentLCS = lcs(mainString, currentTargetString);
  54. results.push({ target: currentTargetString, lcs: currentLCS }), currentLCS.length > results[bestMatchIndex].lcs.length && (bestMatchIndex = i);
  55. }
  56. return { allLCS: results, bestMatch: results[bestMatchIndex], bestMatchIndex: bestMatchIndex };
  57. }
  58. export { compareTwoStrings, findBestMatch, findBestLCS };