Given a string, s, and a set of strings, S, find a string, k that closely resembles s. For this problem, some possible answers would be Jaro Distance, Cosine Similarity, TF-IDF, Levenshtein distance, fuzzy logic, etc., which produce a score between two strings to reliably approximate their atomic similarity.
In this blog post, I’d like to explore one of my favorites, Sørensen–Dice coefficient, to achieve even better similarity comparisons for some cases like FQDNs. Incidentally, this method is used widely in the industry - including New Relic!
Sørensen–Dice Coefficient is “a statistic used to gauge the similarity of two samples.” (Wikipedia) A sample can be a sequence of types such as a string or an integer. It can also be described mathematically as below, where the cardinality of intersection (of X and Y) is multiplied by a constant 2 (2 because the elements common in both samples were reduced through intersection). The outcome in the numerator portion is then divided by the individual cardinality of X and Y.
I wrote a quick implementation below for reference:
|Metric Longest Common Subsequence||prod.us-east01.verizon.com||prod.us-east02.verizon.com||0.038|
|Metric Longest Common Subsequence||apple||apple||0.0|
|Metric Longest Common Subsequence||apple||apples||0.16|
|Metric Longest Common Subsequence||apple||orange||0.66|
As you can see, using the Sørensen–Dice coefficient yields a much better accuracy especially in a case where the sample strings are composed of complex symbols. Now this doesn’t mean that using other algorithms are fundamentally weaker - as even without using SDC, you can still work around with limited impact by, for example, pre-processing the sample strings (e.g. split by symbols and comparing each element separately).