A library implementing different string similarity, distance and sortMatch measures. A dozen of algorithms (including Levenshtein edit distance and sibblings, Longest Common Subsequence, cosine similarity etc.) are currently implemented. Check the summary table below for the complete list...
- Download & Usage
- Normalized, metric, similarity and distance
- Longest Common Subsequence
- Metric Longest Common Subsequence
- Cosine similarity
- Sorensen-Dice coefficient
- Release Notes
Download & Usage
npm install string-comparision --saveyarn add string-comparision
let stringComparision =const Thanos = 'healed'const Rival = 'sealed'const Avengers = 'edward' 'sealed' 'theatre'use by Consinelet cos = stringComparisionconsineconsoleconsoleconsole
The main characteristics of each implemented algorithm are presented below. The "cost" column gives an estimation of the computational cost to compute the similarity between two strings of length m and n respectively.
Normalized, metric, similarity and distance
Although the topic might seem simple, a lot of different algorithms exist to measure text similarity or distance. Therefore the library defines some interfaces to categorize them.
(Normalized) similarity and distance
- StringSimilarity : Implementing algorithms define a similarity between strings (0 means strings are completely different).
- NormalizedStringSimilarity : Implementing algorithms define a similarity between 0.0 and 1.0, like Jaro-Winkler for example.
- StringDistance : Implementing algorithms define a distance between strings (0 means strings are identical), like Levenshtein for example. The maximum distance value depends on the algorithm.
- NormalizedStringDistance : This interface extends StringDistance. For implementing classes, the computed distance value is between 0.0 and 1.0. NormalizedLevenshtein is an example of NormalizedStringDistance.
The Levenshtein distance between two words is the minimum number of single-character edits (insertions, deletions or substitutions) required to change one word into the other.
It is a metric string distance. This implementation uses dynamic programming (Wagner–Fischer algorithm), with only 2 rows of data. The space requirement is thus O(m) and the algorithm runs in O(m.n).
const Thanos = 'healed'const Rival = 'sealed'const Avengers = 'edward' 'sealed' 'theatre'let ls = Similaritylevenshteinconsoleconsoleconsole// output083333333333333341member: 'edward' index: 0 rating: 016666666666666663member: 'theatre' index: 2 rating: 04285714285714286member: 'sealed' index: 1 rating: 08333333333333334
Longest Common Subsequence
The longest common subsequence (LCS) problem consists in finding the longest subsequence common to two (or more) sequences. It differs from problems of finding common substrings: unlike substrings, subsequences are not required to occupy consecutive positions within the original sequences.
It is used by the diff utility, by Git for reconciling multiple changes, etc.
The LCS distance between strings X (of length n) and Y (of length m) is n + m - 2 |LCS(X, Y)| min = 0 max = n + m
LCS distance is equivalent to Levenshtein distance when only insertion and deletion is allowed (no substitution), or when the cost of the substitution is the double of the cost of an insertion or deletion.
This class implements the dynamic programming approach, which has a space requirement O(m.n), and computation cost O(m.n).
In "Length of Maximal Common Subsequences", K.S. Larsen proposed an algorithm that computes the length of LCS in time O(log(m).log(n)). But the algorithm has a memory requirement O(m.n²) and was thus not implemented here.
const Thanos = 'healed'const Rival = 'sealed'const Avengers = 'edward' 'sealed' 'theatre'let lcs = Similaritylcsconsoleconsoleconsole// output083333333333333342member: 'edward' index: 0 rating: 05member: 'theatre' index: 2 rating: 06153846153846154member: 'sealed' index: 1 rating: 08333333333333334
Metric Longest Common Subsequence
Distance metric based on Longest Common Subsequence, from the notes "An LCS-based string metric" by Daniel Bakkelund. http://heim.ifi.uio.no/~danielry/StringMetric.pdf
The distance is computed as 1 - |LCS(s1, s2)| / max(|s1|, |s2|)
const Thanos = 'healed'const Rival = 'sealed'const Avengers = 'edward' 'sealed' 'theatre'let mlcs = Similaritymlcsconsoleconsoleconsole// output08333333333333334016666666666666663member: 'edward' index: 0 rating: 05member: 'theatre' index: 2 rating: 05714285714285714member: 'sealed' index: 1 rating: 08333333333333334
Like Q-Gram distance, the input strings are first converted into sets of n-grams (sequences of n characters, also called k-shingles), but this time the cardinality of each n-gram is not taken into account. Each input string is simply a set of n-grams. The Jaccard index is then computed as |V1 inter V2| / |V1 union V2|.
Distance is computed as 1 - similarity. Jaccard index is a metric distance.
Similar to Jaccard index, but this time the similarity is computed as 2 * |V1 inter V2| / (|V1| + |V2|).
Distance is computed as 1 - similarity.
Implementing algorithms define a similarity between strings
- thanos [String]
- rival [String]
Return a similarity between 0.0 and 1.0
Implementing algorithms define a distance between strings (0 means strings are identical)
- thanos [String]
- rival [String]
Return a number
- thanos [String]
- avengers [...String]
Return an array of objects. ex:
member: 'edward' rating: 016666666666666663member: 'theatre' rating: 04285714285714286member: 'mailed' rating: 05member: 'sealed' rating: 08333333333333334
- Basic building
- Add function sortMatch()