String Similarity
A simple, lightweight (~700 bytes minified) string similarity function based on comparing the number of bigrams in common between any two strings. Returns a score between 0 and 1 indicating the strength of the match.
Based on the Sørensen–Dice coefficient, this algorithm is most effective at detecting rearranged words or misspellings. It tends to be less effective with very short strings, unless perhaps you switch to comparing individual characters in common instead of bigrams.
It is case insensitive unless you specify otherwise. Does not ignore punctuation or spaces. In some cases, removing punctuation beforehand may improve accuracy.
Update
Version 2.0 optimizes the algorithm from O(n2) time complexity to O(n), and switches from using an array for bigrams to a Map, which was found to be substantially faster in performance tests.
Usage
Requirements
This library uses built-in Map data structure for optimal performance. Therefore, it requires at least IE11 or a polyfill for Map.
Examples
import { stringSimilarity } from "string-similarity-js";
stringSimilarity("Lorem ipsum", "Ipsum lorem")
stringSimilarity("The quick brown fox jumps over the lazy dog", "The quck brown fx jumps over the lazy dog")
stringSimilarity("The quick brown fox jumps over the lazy dog", "The quack brain fax jomps odor the lady frog")
stringSimilarity("The quick brown fox jumps over the lazy dog", "Lorem ipsum")
stringSimilarity("DMV", "DNV")
stringSimilarity("DMV", "DNV", 1)
License
This project is licensed under the MIT License - see the LICENSE.md file for details