【发布时间】:2012-07-28 08:06:10
【问题描述】:
我正试图解决一个问题,即从字典文件中识别英语单词与给定字符串的最佳匹配。
例如(“lines”是字典单词的列表):
string testStr = "cakeday";
for (int x= 0; x<= testStr.Length; x++)
{
string test = testStr.Substring(x);
if (test.Length > 0)
{
string test2 = testStr.Remove(counter);
int count = (from w in lines where w.Equals(test) || w.Equals(test2) select w).Count();
Console.WriteLine("Test: {0} / {1} : {2}", test, test2, count);
}
}
给出输出:
Test: cakeday / : 0
Test: akeday / c : 1
Test: keday / ca : 0
Test: eday / cak : 0
Test: day / cake : 2
Test: ay / caked : 1
Test: y / cakeda : 1
显然“day / cake”最适合字符串,但是如果我要在字符串中引入第三个单词,例如“cakedaynow”,它就不会那么好用了。
我知道这个例子很原始,它更像是一个概念证明,想知道是否有人对这种类型的字符串分析有任何经验?
谢谢!
【问题讨论】:
-
“cakeday”不是,也永远不会是英语词典的一部分。拿那个,reddit!
-
我猜
counter和x是一样的 -
显然@KonradRudolph 认为蛋糕是谎言。
-
是的,抱歉,计数器应该是 x