在一个文本文件中查找多少个单词? [英] Find How many Words in a text File?

查看:77
本文介绍了在一个文本文件中查找多少个单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想知道一个文本文件中有多少个单词?
请帮帮我吗?

Hi,


I want to know about how many words in a text file?
please help me?

推荐答案

如果使用C#进行此操作,请将文件读入字符串或缓冲区中,然后应用正则表达式在文件中定位单词的内容.最后,只计算返回的匹配项.比遍历所有字符更快.

If you are doing this using C#, read the file into a string or a buffer and then apply a regular expression to locate words within the file''s content. Finally, just count the matches returned. Faster than looping through all the characters.

using System;
using System.Text.RegularExpressions;
public CountWordsProgram
{
  public static int CountWords(string completeText)
  {
    MatchCollection collection = Regex.Matches(completeText, @"[\S]+");
    return collection.Count;
  }
  public static void Main(string[] args)
  {
    Console.WriteLine( CountWordsProgram.CountWords(File.ReadAllText( @"filename.txt" )) );
  }
}


一个朴素的例子.
A naive example.
#include <iostream>
#include <fstream>
using namespace std;

bool in_word_set(char c)
{ // here you should define the set of characters allowed in a word
  return  ( c >= ''A'' && c <= ''Z'' ) || (c>=''a'' && c<=''z'');
}

int main()
{
  ifstream ifs;
  ifs.open("foo.txt");
  char c;
  int words = 0;
  bool inside = false;
  while ( ifs.get(c).good())
  {
    if (inside) 
    {
      if ( ! in_word_set(c) )  inside = false;
    }
    else
    {
      if (  in_word_set(c) )
      {
        inside = true;
        words++;
      }
    }
  }
  ifs.close();
  cout << words << endl;
  return 0;
}


您可以尝试精炼此粗略的快速示例:
You can try to refine this crude, fast example:
private static int CountWords(string textFileName)
{
    string textFileContents = File.ReadAllText(textFileName);
    char[] separators = new char[] { ' ', '\t', '\n' };
    return textFileContents.Split(separators, StringSplitOptions.RemoveEmptyEntries).Length;
}


这篇关于在一个文本文件中查找多少个单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆