使用地图计算单词频率
Count word frequency using map
这是我第一次在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*
的>>
没有过载。
text
是const char*
,而不是istream
,因此您的重载不适用(重载1:错误,2:已存在于标准库中)。
你想使用更合适的std::istringstream
,比如:
std::istringstream textstream(text);
while(textstream >> st)
words[st]++;
如果你使用现代C++语言,那么生活会变得容易得多。
首先。使用std::map
是正确的方法。
这或多或少是一种对容器中的某些东西进行计数的标准方法。
我们可以使用像std::map
或std::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";
}
- 文本文件中的单词链表
- 芬威克树(BIT).找到具有给定累积频率的最小索引,单位为 O(logN)
- 在指针的帮助下,文本文件中单词的频率
- 如何根据单词在文本中出现的概率输出单词
- 从字符串变量中逐字符读取单词
- 使用std::mt19937从字符串中返回一个随机单词
- 函数计算用户按下按钮的频率
- 如何用for循环在c++中生成单词三角形
- 将数字转换为单词
- 使用if-else将数字转换为单词
- C++-字符串是否包含一个带有简单循环的单词
- 使用单词"not"作为C ++类的名称会导致VS2019错误
- 在条件变量中触发错误信号的频率是多少
- 为什么这个程序返回最后一个单词而不是最长的单词?
- 3-3. 编写一个程序来计算每个不同单词在其输入中出现的次数
- 创建一个函数,返回给定单词在C++中的频率
- 有效地使用具有数组的KMP来计数单词频率计数
- 使用地图计算单词频率
- 算法:计算单词列表频率的更好方法
- 统计一个单词在文本中出现的频率