<i id='lRRna'><tr id='lRRna'><dt id='lRRna'><q id='lRRna'><span id='lRRna'><b id='lRRna'><form id='lRRna'><ins id='lRRna'></ins><ul id='lRRna'></ul><sub id='lRRna'></sub></form><legend id='lRRna'></legend><bdo id='lRRna'><pre id='lRRna'><center id='lRRna'></center></pre></bdo></b><th id='lRRna'></th></span></q></dt></tr></i><div id='lRRna'><tfoot id='lRRna'></tfoot><dl id='lRRna'><fieldset id='lRRna'></fieldset></dl></div>
      <bdo id='lRRna'></bdo><ul id='lRRna'></ul>

    <small id='lRRna'></small><noframes id='lRRna'>

    <legend id='lRRna'><style id='lRRna'><dir id='lRRna'><q id='lRRna'></q></dir></style></legend>

      <tfoot id='lRRna'></tfoot>

    1. 计算文件中单词出现频率的优雅方法

      时间:2023-08-25
      <legend id='JxwM3'><style id='JxwM3'><dir id='JxwM3'><q id='JxwM3'></q></dir></style></legend>
          • <tfoot id='JxwM3'></tfoot>
            <i id='JxwM3'><tr id='JxwM3'><dt id='JxwM3'><q id='JxwM3'><span id='JxwM3'><b id='JxwM3'><form id='JxwM3'><ins id='JxwM3'></ins><ul id='JxwM3'></ul><sub id='JxwM3'></sub></form><legend id='JxwM3'></legend><bdo id='JxwM3'><pre id='JxwM3'><center id='JxwM3'></center></pre></bdo></b><th id='JxwM3'></th></span></q></dt></tr></i><div id='JxwM3'><tfoot id='JxwM3'></tfoot><dl id='JxwM3'><fieldset id='JxwM3'></fieldset></dl></div>
                <tbody id='JxwM3'></tbody>
                <bdo id='JxwM3'></bdo><ul id='JxwM3'></ul>

                <small id='JxwM3'></small><noframes id='JxwM3'>

                本文介绍了计算文件中单词出现频率的优雅方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                统计文件中每个英文"词出现频率的优雅有效方法有哪些?

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

                推荐答案

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

                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

                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++ 中的文件修改? 下一篇:将文件内容读入 C++ 中的字符串

                相关文章

                1. <tfoot id='WtRrs'></tfoot>
                  <legend id='WtRrs'><style id='WtRrs'><dir id='WtRrs'><q id='WtRrs'></q></dir></style></legend>

                    <bdo id='WtRrs'></bdo><ul id='WtRrs'></ul>

                  <small id='WtRrs'></small><noframes id='WtRrs'>

                  <i id='WtRrs'><tr id='WtRrs'><dt id='WtRrs'><q id='WtRrs'><span id='WtRrs'><b id='WtRrs'><form id='WtRrs'><ins id='WtRrs'></ins><ul id='WtRrs'></ul><sub id='WtRrs'></sub></form><legend id='WtRrs'></legend><bdo id='WtRrs'><pre id='WtRrs'><center id='WtRrs'></center></pre></bdo></b><th id='WtRrs'></th></span></q></dt></tr></i><div id='WtRrs'><tfoot id='WtRrs'></tfoot><dl id='WtRrs'><fieldset id='WtRrs'></fieldset></dl></div>