使用地图计算单词频率

Count word frequency using map

本文关键字:单词 频率 计算 地图      更新时间:2023-10-16

这是我第一次在C++中实现映射。因此,给定一个包含文本的字符数组,我想计算每个单词在文本中出现的频率。我决定实现map来存储单词,比较下面的单词并增加一个计数器。以下是我迄今为止编写的代码。

const char *kInputText = "
So given a character array with text, I want to count the frequency of
each word occurring in the text.n
I decided to implement map to store then
words and compare following words and increment a counter.n";      
typedef struct WordCounts
{
int wordcount;
}WordCounts;
typedef map<string, int> StoreMap;
//countWord function is to count the total number of words in the text.
void countWord( const char * text, WordCounts & outWordCounts )
{
outWordCounts.wordcount = 0;
size_t i;
if(isalpha(text[0]))
outWordCounts.wordcount++;
for(i=0;i<strlen(text);i++)
{
if((isalpha(text[i])) && (!isalpha(text[i-1])))
outWordCounts.wordcount++;
}
cout<<outWordCounts.wordcount;
}
//count_for_map() is to count the word frequency using map.
void count_for_map(const char *text, StoreMap & words)
{
string st;
while(text >> st)
words[st]++;
}
int main()
{
WordCounts wordCounts;
StoreMap w;
countWord( kInputText, wordCounts );
count_for_map(kInputText, w);
for(StoreMap::iterator p = w.begin();p != w.end();++p)
{
std::cout<<p->first<<"occurred" <<p->second<<"times. n";
}
return 0;
}

Error: No match for 'operator >>' in 'text >> st'
I understand this is an operator overloading error, so I went ahead and
wrote the following lines of code.
//In the count_for_map()
/*istream & operator >> (istream & input,const char *text)
{
int i;
for(i=0;i<strlen(text);i++)
input >> text[i];
return input;
}*/
Am I implementing map in the wrong way?

左侧有const char*>>没有过载。

textconst char*,而不是istream,因此您的重载不适用(重载1:错误,2:已存在于标准库中)。

你想使用更合适的std::istringstream,比如:

std::istringstream textstream(text);
while(textstream >> st)
words[st]++;

如果你使用现代C++语言,那么生活会变得容易得多。

首先。使用std::map是正确的方法。

这或多或少是一种对容器中的某些东西进行计数的标准方法。

我们可以使用像std::mapstd::unordered_map这样的关联容器。在这里,我们将一个";键";,在这种情况下;单词";用一个值来计数,在这种情况下是特定单词的计数。

幸运的是,这些地图有一个非常好的索引运算符[]。这将查找给定的键,如果找到,则返回对该值的引用。如果找不到,它将使用键创建一个新条目,并返回对该新条目的引用。因此,在机器人程序的情况下,我们将获得用于计数的值的参考。然后我们可以简单地写:

std::unordered_map<std::string, unsigned int> counter{};
counter[word]++;

但是如何从字符串中获取单词。字符串就像一个包含元素的容器。在C++中,许多容器都有迭代器。特别是对于字符串,有一个专用的迭代器允许对std::string中的模式进行迭代。它被称为std::sregex_token_iterator,在这里进行描述。。该模式以std::regex的形式提供,这将给您很大的灵活性。

而且,因为我们有一个如此出色和专用的迭代器,我们应该使用它!

将所有东西粘在一起将提供一个非常紧凑的解决方案,只需最少的代码行数。

请参阅:

#include <iostream>
#include <string>
#include <regex>
#include <map>
#include <iomanip>
const std::regex re{ "\w+" };
const std::string text{ R"(So given a character array with text, I want to count the frequency of 
each word occurring in the text. 
I decided to implement map to store the
words and compare following words and increment a counter.")" };

int main() {
std::map<std::string, unsigned int> counter{};
for (auto word{ std::sregex_token_iterator(text.begin(),text.end(),re) }; word != std::sregex_token_iterator(); ++word)
counter[*word]++;
for (const auto& [word, count] : counter) 
std::cout << std::setw(20) << word << "toccurredt" << count << " timesn";
}