我如何遍历字符串并找出有多少单词?

时间:2017-10-18 18:25:55

标签: c#

我的问题需要帮助:

现在我有一个WPF应用程序,其表单包含一个按钮和一个文本框。

另外,我有一个打开的文件目录 - 打开.cs和.txt文件。

我需要遍历这些文件的字符串并显示其中最常见的单词,从最大到最小。

例如,字符串为:

  

"太阳很明亮。太阳是黄色的"。

会回来:

  

= 2;

     

sun = 2;

     

是= 2;

     

bright = 1;

     

黄色= 1;

截至目前我的代码:

private void btn1_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog() { Filter = "Text Documents |*.cs;*.txt", ValidateNames = true, Multiselect = false };
        if (ofd.ShowDialog() == true)
            rtb.Text = File.ReadAllText(ofd.FileName);

        string[] userText = rtb.Text.ToLower().Split(new char[] { ' ', ',', '=', '+', '}', '{', '\r', '\n', '(', ')', ';' }, StringSplitOptions.RemoveEmptyEntries);

        var frequencies = new Dictionary<string, int>();

        foreach (string word in userText) //search words in our array userText that we declared at the beginning.
        {

        }
   }

我不知道如何从这里继续......感谢帮助。

3 个答案:

答案 0 :(得分:2)

使用您为我们提供的示例和预期输出,我能够通过使用.GroupBy以及创建匿名类来实现此目的。

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
        // example string
        var myString = "The sun is bright. The sun is yellow";

        // split the string into an array based on space characters and the period
        var stringArray = myString.Split(new char[] {' ', '.'}, StringSplitOptions.RemoveEmptyEntries);

        // group the items in the array based on the value, then create an anonymous class with the properties `Word` and `Count`
        var groupedWords = stringArray.GroupBy(x => x).Select(x => new { Word = x.Key, Count = x.Count() }).ToList();

        // print the properties based on order of largest to smallest count to screen
        foreach(var item in groupedWords.OrderByDescending(x => x.Count))
        {
            Console.WriteLine(item.Word + " = " + item.Count);
        }


        // Output
        ---------
        // The = 2
        // sun = 2
        // is = 2
        // bright = 1
        // yellow = 1
    }
}

请告诉我这是否有帮助!

答案 1 :(得分:1)

这几乎听起来像是字典的原始定义,所以这可能是一个很好的起点:

IDictionary<string, int> actualDictionary = new Dictionary<string, int>();

您可以在字典中放置单词,并在每次找到它们时递增计数。

    IDictionary<string, int> actualDictionary = new Dictionary<string, int>();

    foreach (string word in userText) //search words in our array userText that we declared at the beginning.
    {
        if (!actualDictionary.ContainsKey(word)) {
            actualDictionary[word] = 0;
        }

        actualDictionary[word]++;
    }

    foreach(var thing in actualDictionary) {
        Console.WriteLine(thing.Key + " " + thing.Value);
    }

.NET Fiddle上查看正在运行的示例。

答案 2 :(得分:1)

我会选择@ GTown-Coder的方法作为最简单的方法。但是如果你真的只是想知道如何使用字典中的字典来实现相同的代码......

 /**
 * Removes and returns the element that is at place x in the queue.
 * Precondition: x must be less than 5, x must be less than size
 * Note: indexing from 0: 0 == front element, 1 == second element, etc.
 * @param x the passed in index of the element to be removed
 * @return the element removed from the queue
 * @throws EmptyCollectionException if the queue is empty
 * @throws InvalidArgumentException if x > 4, or x > size of collection
 * 
 */

public T dequeue(int x) throws EmptyCollectionException {
    if (numNodes == 0) {
        throw new EmptyCollectionException("Work Ahead Queue");
    }
    T element = front.getElement();
    front = front.getNext();
    firstFive.remove(0);
    numNodes--;
    if (numNodes >= 5) {
        firstFive.add(firstFive.get(3).getNext());
    }
    return element;

}