使用C ++映射对单词频率进行计数.我究竟做错了什么? [英] Using a C++ map to count word frequency. What am i doing wrong?
问题描述
那么一点背景.我应该编写一个程序,逐段浏览段落,并在每一行中挑选出最常用的单词.我已经有效地编写了将行的内容存储在向量中的代码,现在我需要将频率分配给这些单词.我正在使用地图,但这并没有给我想要的结果.我是C ++数据结构的新手,所以请教一些建议.
So a little background. I am supposed to make a program that goes through a paragraph, line by line and picks out the most frequently used word on each line. I have effectively written code that stores the contents of a line in a vector, now i need to assign the frequncey to these words. I am using a map, but it isnt giving me the desired outcome. I am new to C++ data structures, so some advice would be appreciated.
map<string,int> freq; //map for getting frequency on line
for(int i=0;i<currLine.size();i++){ //loops through the words on the line
string currName=currLine.at(i); //current word
//cout<<currName<<endl;
if(freq.find(currName)!=freq.end()){ //if already present
int update=freq.at(currName)+1; //this value takes the value and adds one
freq.insert(pair<string,int>(currName,update)); //insert back into the string with updated value
//cout<<freq.at(currName);
}
else{
freq.insert(pair<string,int>(currName,0)); //if not present, then insert with value 0
}
}
例如:
-与您不同的是,我对gnu表示不满.不介意我随身带食物吗?乔治·奥威尔(George Orwell)大约在1984年错了.您的猫码可以吗?如果没有,可以输入狗码吗?我喜欢玩辐射游戏.建议您也玩.足球是美国以外最受欢迎的运动.
-Unlike you I take umbrage with gnu. Don’t mind me taking the food with me? George Orwell was wrong about 1984. Can your cat code? If not, can your dog code? I like to play the fallout games. Recommend you play it as well. Soccer is the most popular sport outside America.
会回来的:与我不同,乔治可以踢足球"
Would return: "unlike me george can play soccer"
int main()
{
string line;
vector<string> result;
while(getline(cin,line)){ //on each line
vector<string> currLine; //stores words on line
string curr=""; //temp string for grasping words
for(int i=0;i<line.length();i++){ //loops through line
if(isalpha(line.at(i))){ //if it is a letter, add it to the temp string
curr+=tolower(line.at(i));
}
else if(line.at(i)=='\''){
//do nothing
}
else{ //if not, then add the previous word to vector and reset temp string
currLine.push_back(curr);
curr="";
}
}
vector< pair<string, int> > freq(10);
//freq.push_back(make_pair("asd",2));
for(int i=0;i<currLine.size();i++){
string curr1=currLine.at(i);
cout<<curr1<<" ";
for(int j=0;j<freq.size();j++){
if(freq.at(j).first==curr1){ //if present in the list
//cout<<"Duplicate found";
freq.at(j).second++; //increment second value
}
else{
//cout<<"Pair Made";
freq.push_back(make_pair(curr1,1));
break;
}
}
}
int max=0;
string currMost;
for(int i =0;i<freq.size();i++){
if(freq.at(i).second>max){
max=freq.at(i).second;;
currMost=freq.at(i).first;
}
}
//cout<<currMost;
cout<<endl;
// result.push_back(currMost);
}
for(int i=0;i<result.size();i++){
cout<<result.at(i);
}
}
推荐答案
我首先将句子拆分为单词,然后将其存储到map中.插入地图最简单,最明显的方法是使用 map [key] = value
表示法.我先使用 count()
函数检查该值是否已经存在并插入该值,否则我增加了已经存在的键的值.
I first split the sentence into words and then store them into map. The easiest and more obvious way for map insertion is to use map[key] = value
notation. I checked first whether the value is already present by using count()
function and insert the value, otherwise I incremented the already present key's value.
map <string, int> word_frequency;
string word="";
for (int i=0; i<=current_line.length();i++) {
if (current_line[i]!=' ' && i< current_line.length()) {
word = word + current_line[i];
} else if(word_frequency.count(word) == 0) {
word_frequency[word] = 1;
word = "";
} else {
word_frequency[word]++;
word = "";
}
}
这篇关于使用C ++映射对单词频率进行计数.我究竟做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!