c++ - 计算文件中单词频率的优雅方法

标签 c++ file-io

计算文件中每个“英文”单词出现频率的优雅而有效的方法是什么?

最佳答案

首先,我定义letter_only std::locale以便忽略来自流的标点符号,并仅从输入流中读取有效的“英文”字母。这样,流会将词 "ways""ways.""ways!" 视为同一个词 "ways",因为流会忽略像 ".""!" 这样的标点符号。

struct letter_only: std::ctype<char> 
{
    letter_only(): std::ctype<char>(get_table()) {}

    static std::ctype_base::mask const* get_table()
    {
        static std::vector<std::ctype_base::mask> 
            rc(std::ctype<char>::table_size,std::ctype_base::space);

        std::fill(&rc['A'], &rc['z'+1], std::ctype_base::alpha);
        return &rc[0];
    }
};

解决方案1

int main()
{
     std::map<std::string, int> wordCount;
     ifstream input;
     input.imbue(std::locale(std::locale(), new letter_only())); //enable reading only letters!
     input.open("filename.txt");
     std::string word;
     while(input >> word)
     {
         ++wordCount[word];
     }
     for (std::map<std::string, int>::iterator it = wordCount.begin(); it != wordCount.end(); ++it)
     {
           cout << it->first <<" : "<< it->second << endl;
     }
}

解决方案2

struct Counter
{
    std::map<std::string, int> wordCount;
    void operator()(const std::string & item) { ++wordCount[item]; }
    operator std::map<std::string, int>() { return wordCount; }
};

int main()
{
     ifstream input;
     input.imbue(std::locale(std::locale(), new letter_only())); //enable reading only letters!
     input.open("filename.txt");
     istream_iterator<string> start(input);
     istream_iterator<string> end;
     std::map<std::string, int> wordCount = std::for_each(start, end, Counter());
     for (std::map<std::string, int>::iterator it = wordCount.begin(); it != wordCount.end(); ++it)
     {
          cout << it->first <<" : "<< it->second << endl;
     }
 }

关于c++ - 计算文件中单词频率的优雅方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4888879/

相关文章:

c++ - 存储指向类对象的指针地址的最佳方法

java - 使用 Java 在两个或多个套接字之间执行零复制数据传输

c++ - 给定一个 n 位长整数,如何访问它的各个部分?

c++ - 部署简单的 C++ 应用程序

java - Java 中的 "Cannot Find Symbol - method hasNextLine()"错误

c - 返回文件*

c - 从命令行打开文件并显示键入的内容

c++ - 抽象类继承和智能指针容器

c++ - 在按键时创建一个新对象并将其添加到列表中

c++ - C 和 C++ 中的外部变量有什么区别?