我有一个字符串“The White Horse is hungry
”
现在,我需要将其与可能的发音相匹配。以下是示例。 (把这些视为音素,好吧我的意思是用户可以发音的方式)
The White Horse is hungary
The White Horse is not hungry
The White Horse is very hungry
The Horse is hungry
The Horse is hungries
White Horse is hungry
star wars..clone wars
现在你可以看到发音的相似程度以及它们之间的差异。我可以申请Levenshtein distance
来找出差异。它给了我非常准确的结果。然而,我还发现如果我能找到一种方法来比较两个音素的相似度,例如,当用户说错音素,而不是添加或删除音素时,我可以得到更好的结果。
任何人都知道一个很好的算法吗?以及c#实现的示例/链接?
答案 0 :(得分:1)
您可以在此处尝试此算法:http://www.catalysoft.com/articles/StrikeAMatch.html
它的示例实现。
string input = "The White Horse is hungry";
string[] toTest = new string[]{
"The White Horse is hungary",
"The White Horse is not hungry",
"The White Horse is very hungry",
"The Horse is hungry",
"The Horse is hungries",
"White Horse is hungry",
"star wars..clone wars",
};
string closest = toTest
.Select(s => new
{
Str = s,
Distance = s.Distance(input)
})
.OrderByDescending(x => x.Distance)
.First().Str;
public static class StringSimilarity
{
public static float Distance(this string s1, string s2)
{
var p1 = GetPairs(s1);
var p2 = GetPairs(s2);
return (2f * p1.Intersect(p2).Count()) / (p1.Count + p2.Count);
}
static List<string> GetPairs(string s)
{
if (s == null) return new List<string>();
if (s.Length < 3) return new List<string>() { s };
List<string> result = new List<string>();
for (int i = 0; i < s.Length - 1; i++)
{
result.Add(s.Substring(i, 2).ToLower(CultureInfo.InvariantCulture));
}
return result;
}
}
答案 1 :(得分:0)