[英]How to display all mistaken words
我在richTextBox1中有一些文本。
我必须按单词的频率对其排序,并在richTextBox2
显示它们。 似乎有效。
必须找到所有错误的单词,并将其显示在richTextBox4
。 我正在使用Hunspell。 显然我缺少了一些东西。 几乎所有单词都显示在richTextBox4
而不仅仅是错误的单词。
码:
foreach (Match match in wordPattern.Matches(str))
{
if (!words.ContainsKey(match.Value))
words.Add(match.Value, 1);
else
words[match.Value]++;
}
string[] words2 = new string[words.Keys.Count];
words.Keys.CopyTo(words2, 0);
int[] freqs = new int[words.Values.Count];
words.Values.CopyTo(freqs, 0);
Array.Sort(freqs, words2);
Array.Reverse(freqs);
Array.Reverse(words2);
Dictionary<string, int> dictByFreq = new Dictionary<string, int>();
for (int i = 0; i < freqs.Length; i++)
{
dictByFreq.Add(words2[i], freqs[i]);
}
Hunspell hunspell = new Hunspell("en_US.aff", "en_US.dic");
StringBuilder resultSb = new StringBuilder(dictByFreq.Count);
foreach (KeyValuePair<string, int> entry in dictByFreq)
{
resultSb.AppendLine(string.Format("{0} [{1}]", entry.Key, entry.Value));
richTextBox2.Text = resultSb.ToString();
bool correct = hunspell.Spell(entry.Key);
if (correct == false)
{
richTextBox4.Text = resultSb.ToString();
}
}
您在richtextbox4上显示的内容与在richtextbox2中显示的内容相同:)
我认为这应该工作:
foreach (KeyValuePair<string, int> entry in dictByFreq)
{
resultSb.AppendLine(string.Format("{0} [{1}]", entry.Key, entry.Value));
richTextBox2.Text = resultSb.ToString();
bool correct = hunspell.Spell(entry.Key);
if (correct == false)
{
richTextBox4.Text += entry.Key;
}
}
除了以上答案(如果您的Hunspell.Spell方法正确运行,该方法应该可以工作),我还有一些建议可以缩短您的代码。 您正在将Matches添加到字典中,并计算每个匹配项的出现次数。 然后,您似乎是按照频率的降序对其进行排序(因此,最高匹配项的结果中的索引为0)。 以下是一些代码段,这些代码段应使您的函数更短:
IOrderedEnumerable<KeyValuePair<string, int>> dictByFreq = words.OrderBy<KeyValuePair<string, int>, int>((KeyValuePair<string, int> kvp) => -kvp.Value);
这使用.NET框架为您完成所有工作。 words.OrderBy使用Func参数,该参数提供要排序的值。 使用此函数的默认值的问题是它希望对键进行排序,而您希望对值进行排序。 此函数调用将完全做到这一点。 它还将基于这些值以降序对它们进行排序,这是发生特定匹配的频率。 它返回一个IOrderedEnumerable对象,该对象必须存储。 而且由于这是无法计数的,因此您甚至不必将其放回字典中! 如果以后确实需要执行其他操作,则可以调用dictByFreq.ToList()函数,该函数返回类型为List>的对象。
因此,您的整个功能将变为:
foreach (Match match in wordPattern.Matches(str))
{
if (!words.ContainsKey(match.Value))
words.Add(match.Value, 1);
else
words[match.Value]++;
}
IOrderedEnumerable<KeyValuePair<string, int>> dictByFreq = words.OrderBy<KeyValuePair<string, int>, int>((KeyValuePair<string, int> kvp) => -kvp.Value);
Hunspell hunspell = new Hunspell("en_US.aff", "en_US.dic");
StringBuilder resultSb = new StringBuilder(dictByFreq.Count);
foreach (KeyValuePair<string, int> entry in dictByFreq)
{
resultSb.AppendLine(string.Format("{0} [{1}]", entry.Key, entry.Value));
richTextBox2.Text = resultSb.ToString();
bool correct = hunspell.Spell(entry.Key);
if (correct == false)
{
richTextBox4.Text = entry.Key;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.