5

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

这是一个字数统计程序的测试。这只是一个测试。如果你的程序运行成功,你应该计算出这个文件有 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);
    }
} 
4

6 回答 6

3

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

int count = Regex.Matches(input, @"\b\w+\b").Count;
于 2012-11-05T23:57:39.290 回答
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);
}

}

于 2012-11-05T23:55:55.713 回答
1

几个提示。

  1. 如果你只有句子“hi”,你的输出会是什么?
  2. 您的计数器计算是:从 0 到fields.Length,递增计数器。和你fields.Length的柜台有什么关系?
于 2012-11-05T23:55:40.750 回答
0

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

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

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

于 2012-11-05T23:57:30.513 回答
0
//Easy method using Linq to Count number of words in a text file
/// www.techhowdy.com
// Lyoid Lopes Centennial College 2018
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FP_WK13
{
    static class Util
    {

        public static IEnumerable<string> GetLines(string yourtextfile)
        {
            TextReader reader = new StreamReader(yourtextfile);
            string result = string.Empty;
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                yield return line;
            }
            reader.Close();
        }



        // Word Count 

        public static int GetWordCount(string str)
        {         
            int words = 0;
            string s = string.Empty;
            var lines = GetLines(str);

            foreach (var item in lines)
            {
                s = item.ToString();
                words = words +  s.Split(' ').Length;

            }

            return words;

        }


    }
}
于 2018-01-12T03:22:08.073 回答
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);
        }
    }
}
于 2019-08-24T11:01:08.050 回答