Skip to content

Instantly share code, notes, and snippets.

@brandedoutcast
Last active December 9, 2019 13:00
Show Gist options
  • Save brandedoutcast/6b2e42ae2700aefb147d1a73291a23cc to your computer and use it in GitHub Desktop.
Save brandedoutcast/6b2e42ae2700aefb147d1a73291a23cc to your computer and use it in GitHub Desktop.
Compare 2 strings & determine the similarity between them in percentage
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