簡體   English   中英

計算文本文件中的單詞數

[英]Counting number of words in a text file

我正在嘗試計算文本文件中的單詞數,即這個,開始。

這是對字數統計程序的測試。 這只是一個測試。 如果你的程序運行成功,你應該計算出這個文件中有 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);
    }
} 

嘗試使用正則表達式,例如:

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

這應該適合你:

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);
}

}

一些提示。

  1. 如果你只有句子“hi”,你的輸出會是什么?
  2. 您的計數器計算是:從 0 到fields.Length ,遞增計數器。 fields.Length和你的計數器有什么關系?

您可能會遇到一次性錯誤,請嘗試這樣的操作

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

當您可以直接獲取數字時,無需遍歷數組來計算元素

//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;

        }


    }
}
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);
        }
    }
}

暫無
暫無

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

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