簡體   English   中英

在richTextBox 中搜索單詞(windows 窗體,c#)

[英]Searching for words in richTextBox (windows forms, c#)

我需要幫助在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");
            }
        }

假設您有文本、從文件中讀取、從某個網頁下載或 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}");
}

此解決方案類似於上一篇文章。 主要區別在於該解決方案使用按單詞分組,這很簡單並將其放入字典。 在那里,可以很容易地找到很多。

假設你有一個 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 中每個單詞的數量嗎?

也許添加一些上下文?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM