简体   繁体   English

如何显示所有错误的单词

[英]How to display all mistaken words

I have some text in richTextBox1. 我在richTextBox1中有一些文本。

  1. I have to sort the words by their frequency and display them in richTextBox2 . 我必须按单词的频率对其排序,并在richTextBox2显示它们。 It seems to work. 似乎有效。

  2. Have to find all mistaken words and display them in richTextBox4 . 必须找到所有错误的单词,并将其显示在richTextBox4 I'm using Hunspell. 我正在使用Hunspell。 Apparently I'm missing something. 显然我缺少了一些东西。 Almost all words are displayed in richTextBox4 not only the wrong ones. 几乎所有单词都显示在richTextBox4而不仅仅是错误的单词。

Code: 码:

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();
    }    
}

Your are displaying on richtextbox4 the same as in richtextbox2 :) 您在richtextbox4上显示的内容与在richtextbox2中显示的内容相同:)

I think this should work: 我认为这应该工作:

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;
    }    
}

In addition to the above answer (which should work if your Hunspell.Spell method works correctly), I have a few suggestions to shorten your code. 除了以上答案(如果您的Hunspell.Spell方法正确运行,该方法应该可以工作),我还有一些建议可以缩短您的代码。 You are adding Matches to your dictionary, and counting the number of occurrences of each match. 您正在将Matches添加到字典中,并计算每个匹配项的出现次数。 Then you appear to be sorting them in descending value of the frequency (so the highest occurrence match will have index 0 in the result). 然后,您似乎是按照频率的降序对其进行排序(因此,最高匹配项的结果中的索引为0)。 Here are a few code snippets which should make your function a lot shorter: 以下是一些代码段,这些代码段应使您的函数更短:

IOrderedEnumerable<KeyValuePair<string, int>> dictByFreq = words.OrderBy<KeyValuePair<string, int>, int>((KeyValuePair<string, int> kvp) =>  -kvp.Value);

This uses the .NET framework to do all your work for you. 这使用.NET框架为您完成所有工作。 words.OrderBy takes a Func argument which provides the value to sort on. words.OrderBy使用Func参数,该参数提供要排序的值。 The problem with using the default values for this function is it wants to sort on the keys and you want to sort on the values . 使用此函数的默认值的问题是它希望对进行排序,而您希望对进行排序。 This function call will do exactly that. 此函数调用将完全做到这一点。 It will also sort them in descending order based on the values, which is the frequency that a particular match occurred. 它还将基于这些值以降序对它们进行排序,这是发生特定匹配的频率。 It returns an IOrderedEnumerable object, which has to be stored. 它返回一个IOrderedEnumerable对象,该对象必须存储。 And since that is enumerable, you don't even have to put it back into a dictionary! 而且由于这是无法计数的,因此您甚至不必将其放回字典中! If you really need to do other operations on it later, you can call the dictByFreq.ToList() function, which returns an object of type: List>. 如果以后确实需要执行其他操作,则可以调用dictByFreq.ToList()函数,该函数返回类型为List>的对象。

So your whole function then becomes this: 因此,您的整个功能将变为:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM