【问题标题】:Searching for words in richTextBox (windows forms, c#)在richTextBox中搜索单词(windows forms, c#)
【发布时间】:2018-04-14 08:26:03
【问题描述】:

我需要帮助来计算richTextBox 中的相同单词,并且还想为它们分配数字/单词。一个盒子里有 500,000+ 个单词,而且所有单词都只写了 3 或 6 次。 一种解决方案是提前写下我要查找的单词,在框中搜索并为其分配一个数字/单词。但是这样做的人太多了,如果有更快的方法,它将对我有很大帮助。 提前致谢!

这是我现在正在做的代码,它不代表我在问什么。

        for (int i = 0; i < Url.Length; i++)
        {
            doc = web.Load(Url[i]);

            int x=0;
            int y=0;

            //here was a long list of whiles for *fors* down there, 
            //removed to be cleaner for you to see 
            //one example:                 
            //while (i == 0)
            //{
            //    x = 18;
            //    y = 19;
            //    break;
            //}

            for (int w = 0; w < x; w++)
            {
                string metascore = doc.DocumentNode.SelectNodes("//*[@class=\"trow8\"]")[w].InnerText;
                richTextBox1.AppendText("-----------");
                richTextBox1.AppendText(metascore);
                richTextBox1.AppendText("\r\n");
            }
            for (int z = 0; z < y; z++)
            {
                string metascore1 = doc.DocumentNode.SelectNodes("//*[@class=\"trow2\"]")[z].InnerText;
                richTextBox1.AppendText("-----------");
                richTextBox1.AppendText(metascore1);
                richTextBox1.AppendText("\r\n");
            }
        }

【问题讨论】:

  • 您的代码与搜索 RichTextBox 无关。这听起来也不是一个好主意——你为什么要使用文本框?似乎内存中的列表或字典会更好地解决这个问题。
  • 我只是想展示一下我现在在做什么。内存列表或字典是什么意思?

标签: c# winforms visual-studio richtextbox string-matching


【解决方案1】:

假设您有文本、从文件中读取、从某个网页下载或 RTB...使用 LINQ 将满足您的所有需求。

string textToCount = "...";
// second step, split text by delimiters of your own
List<string> words = textToCount.Split(' ', ',', '!', '?', '.', ';', '\r', '\n') // split by whatever characters
            .Where(s => !string.IsNullOrWhiteSpace(s))                           // eliminate all whitespaces
            .Select(w => w.ToLower())                                            // transform to lower for easier comparison/count
            .ToList();
// use LINQ helper method to group words and project them into dictionary
// with word as a key and value as total number of word appearances
// this is the actual magic. With this in place, it's easy to get statistics.
var groupedWords = words.GroupBy(w => w)
   .ToDictionary(k => k.Key, v => v.Count());`

// to get various statistics
// total different words - count your dictionary entries
groupedWords.Count();
// the most often word - looking for the word having the max number in the list of values
groupedWords.First(kvp => kvp.Value == groupedWords.Values.Max()).Key
// mentioned this many times
groupedWords.Values.Max()
// similar for min, just replace call to Max() with call to Min()
// print out your dictionary to see for every word how often it's metnioned
foreach (var wordStats in groupedWords)
{
    Console.WriteLine($"{wordStats.Key} - {wordStats.Value}");
}

此解决方案与之前的帖子类似。主要区别在于该解决方案使用按单词分组,这很简单并将其放入字典中。到了那里,很容易找到很多东西。

【讨论】:

    【解决方案2】:

    假设你有一个 RichTextBox 在某处跑来跑去,我们称他为 Bob:

            //Grab all the text from the RTF
            TextRange BobRange = new TextRange(
                    // TextPointer to the start of content in the RichTextBox.
                    Bob.Document.ContentStart,
                    // TextPointer to the end of content in the RichTextBox.
                    Bob.Document.ContentEnd
                );
    
            //Assume words are sperated by space, commas or periods, split on that and thorw the words in a List
            List<string> BobsWords = BobRange.Text.Split(new char[] { ' ', ',', '.' }, StringSplitOptions.RemoveEmptyEntries).ToList<string>();
    
            //Now use Linq for .net to grab what you want
            int CountOfTheWordThe = BobsWords.Where(w => w == "The").Count();
    
            //Now that we have the list of words we can create a MetaDictionary
            Dictionary<string, int> BobsWordsAndCounts = new Dictionary<string, int>();
            foreach( string word in BobsWords)
            {
                if (!BobsWordsAndCounts.Keys.Contains(word))
                    BobsWordsAndCounts.Add( word, BobsWords.Where(w => w == word).Count(); )
            }
    

    这就是你给一个词分配一个数字的意思吗?您想知道 RichTextBox 中每个单词的计数吗?

    也许添加一些上下文?

    【讨论】:

    • 是的,我想知道richTextBox 中每个单词的计数,并将该数字写在该单词之前。例如,这将是编辑后该框中的句子:“(4)Comments (1)are (45)used (34)to...” 数字表示该词被使用了多少次。下一次任何句子中都会有“评论”,它应该在它之前有(5)。我知道你在这里写的内容以及我如何计算准确的单词,但一般不知道如何计算。
    • 查看我的帖子。只需替换 Console.WriteLine($"{wordStats.Key} - {wordStats.Value}");在 foreach 循环中使用 Console.WriteLine($"({wordStats.Value}){wordStats.Key}");你会得到你所需要的。
    • @NickWhite 是 RTB 会不断更新,因此您的计数会发生变化吗?所以计数将是所有先前的单词?即:“(1)计数(2)计数,(3)计数(4)计数”
    • 我想在 for 循环 结束后实现该功能,所以每次我启动我的软件时,它都会记下 RTB 中的所有单词,完成后,它应该数一数其中的每一个。
    猜你喜欢
    • 1970-01-01
    • 2019-07-19
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多