Is there any class (function) in .Net that can do this:
if
s1 = " I have a black car" and s2 = "I have a car that is small";
int matchingProcentage = matchingFunction(s1,s2);
matchingProcentage == 70% <-- just as an example value :)
Here's a good way of going about it!
Levenshtein Distance
A function like the following should work, it was hastily written so feel free to change things up:
Usage:
GetStringPercentage("I have a black car", "I have a car that is small");
Method:
public static decimal GetStringPercentage(string s1, string s2)
{
decimal matches = 0.0m;
List<string> s1Split = s1.Split(' ').ToList();
List<string> s2Split = s2.Split(' ').ToList();
if (s1Split.Count() > s2Split.Count())
{
foreach (string s in s1Split)
if (s2Split.Any(st => st == s))
matches++;
return (matches / s1Split.Count());
}
else
{
foreach (string s in s2Split)
if (s1Split.Any(st => st == s))
matches++;
return (matches / s2Split.Count());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With