计算文本文件中的单词数

时间:2012-11-05 23:51:15

标签: c# .net regex

我正在尝试计算文本文件中的单词数,即开始。

  

这是对字数统计程序的测试。这只是一个测试。如果你的   程序运行成功,你应该计算出有30个   这个文件中的文字。

我正在使用StreamReader将文件中的所有内容放入一个字符串中,然后使用.Split方法获取单个单词的数量,但在编译和运行程序时,我一直得到错误的值。

using System;
using System.IO;

class WordCounter
{
    static void Main()
    {
        string inFileName = null;

        Console.WriteLine("Enter the name of the file to process:");
        inFileName = Console.ReadLine();

        StreamReader sr = new StreamReader(inFileName);

        int counter = 0;
        string delim = " ,.";
        string[] fields = null;
        string line = null;

        while(!sr.EndOfStream)
        {
            line = sr.ReadLine();
        }



        fields = line.Split(delim.ToCharArray());
        for(int i = 0; i < fields.Length; i++)
        {
            counter++;
        }
        sr.Close();
        Console.WriteLine("The word count is {0}", counter);
    }
} 

6 个答案:

答案 0 :(得分:3)

尝试使用正则表达式,例如:

var count = Regex.Matches(input, @"\b\w+\b").Count();

答案 1 :(得分:2)

这应该适合你:

using System;
using System.IO;

class WordCounter
{
static void Main()
{
      string inFileName = null;

      Console.WriteLine("Enter the name of the file to process:");
      inFileName = Console.ReadLine();

      StreamReader sr = new StreamReader(inFileName);

      int counter = 0;
      string delim = " ,."; //maybe some more delimiters like ?! and so on
      string[] fields = null;
      string line = null;

      while(!sr.EndOfStream)
      {
         line = sr.ReadLine();//each time you read a line you should split it into the words
         line.Trim();
         fields = line.Split(delim.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
         counter+=fields.Length; //and just add how many of them there is
      }


      sr.Close();
      Console.WriteLine("The word count is {0}", counter);
}

}

答案 2 :(得分:1)

一些提示。

  1. 如果您只有“hi”这句话会是什么输出怎么办?
  2. 您的计数器计算是:从0到fields.Length,递增计数器。 fields.Length和您的计数器有何关联?

答案 3 :(得分:0)

你可能会遇到一次性错误,尝试这样的事情

    counter = 0;
    while(!sr.EndOfStream)
    {
        line = sr.ReadLine();
        fields = line.Split(delim.ToCharArray());
        counter += field.length();
    }

当您可以直接获取数字时,无需迭代数组来计算元素

答案 4 :(得分:0)

strr = [names[i],":",values[i] for i in range(len(metrics)]

答案 5 :(得分:0)

using System.IO;
using System;
namespace solution
{
    class Program
    {
        static void Main(string[] args)
        {
            var readFile = File.ReadAllText(@"C:\test\my.txt");
            var str = readFile.Split(new char[] { ' ', '\n'}, StringSplitOptions.RemoveEmptyEntries);
            System.Console.WriteLine("Number of words: " + str.Length);
        }
    }
}
相关问题