Last active
December 9, 2019 13:00
-
-
Save brandedoutcast/6b2e42ae2700aefb147d1a73291a23cc to your computer and use it in GitHub Desktop.
Compare 2 strings & determine the similarity between them in percentage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public float SimilarityPercentage(string a, string b) | |
{ | |
var SpecialCharacters = new Regex("[^\\w\\d\\s]"); | |
a = SpecialCharacters.Replace(a, string.Empty); | |
b = SpecialCharacters.Replace(b, string.Empty); | |
var PairsInA = a.Split(' ').SelectMany(s => s.Zip(s.Substring(1), (fc, sc) => string.Concat(fc, sc))); | |
var PairsInB = b.Split(' ').SelectMany(s => s.Zip(s.Substring(1), (fc, sc) => string.Concat(fc, sc))); | |
return 2 * PairsInA.Intersect(PairsInB).Count() / PairsInA.Union(PairsInB).Count(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment