similarity.js 2.8 KB

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