计算文件中单词频率的优雅方式

编程入门 行业动态 更新时间:2024-10-28 09:13:35
本文介绍了计算文件中单词频率的优雅方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

What are the elegant and effective ways to count the frequency of each "english" word in a file?

推荐答案

,我定义 letter_only std :: locale ,以便忽略来自流的标点符号,并从输入流中只读取有效的英语字母。这样,流将处理方式,方式。和 ways!作为同一个词ways,因为流将忽略像 / code>和!。

First of all, I define letter_only std::locale so as to ignore punctuations coming from the stream, and to read only valid "english" letters from the input stream. That way, the stream will treat the words "ways", "ways." and "ways!" as just the same word "ways", because the stream will ignore punctuations like "." and "!".

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 ​​

Solution 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

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

更多推荐

计算文件中单词频率的优雅方式

本文发布于:2023-11-12 02:51:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580295.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单词   频率   优雅   方式   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!