| | |
Display only the 20 most used words[Word Frequency]
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 10
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
ok ok i got it now right i copy it to vector and sort it..... here is the code #include <iostream> #include <fstream> #include <algorithm> #include <string> #include <map> #include <vector> using namespace std; typedef map<string,int> word_count_list; struct val_lessthan : binary_function < pair<string,int>, pair<string,int>, bool > { bool operator() (const pair<string,int>& x, const pair<string,int>& y) const {return x.second<y.second;} }val_lt; int main() { word_count_list word_count; string filename; // Get the filename. cout << "Enter the file you wish to have searched:\n"; cin >> filename; // Open file. ifstream file(filename.c_str()); // Read in all the words. string word; while (file >> word){ // Remove punctuation. int index; while ((index = word.find_first_of(".,!?\\;-*+")) != string::npos) word.erase(index, 1); ++word_count[word]; } //copy pairs to vector vector<pair<string,int> > wordvector; copy(word_count.begin(), word_count.end(), back_inserter(wordvector)); //sort the vector by second (value) instead of key sort(wordvector.begin(), wordvector.end(), val_lt); for(int i=0; i<wordvector.size(); ++i) cout << wordvector[i].first << " = " << wordvector[i].second << endl; return 0; } to finish the porgram. i need to output the 20 most common or used words.... i try changing for(int i=0; i<wordvector.size(); ++i) to for9int i=0,i<=20;i++) but nothing happens
This should do it
C++ Syntax (Toggle Plain Text)
vector<pair<string,int> >::iterator it; for(it = wordvector.begin(); it != wordvector.end(); it++) cout << (*it).first << " = " << (*it).second << "\n";
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Sep 2009
Posts: 10
Reputation:
Solved Threads: 0
i try using
vector<pair<string,int> >::iterator it;
for(it = wordvector.begin(); it != wordvector.end(); it++)
cout << (*it).first << " = " << (*it).second << "\n";
this does not limit the output to 20
am i missing something?
please teach me. this is the last problem to complete the program
just display the 20 most common words.
damn why cant i use the CODE button
vector<pair<string,int> >::iterator it;
for(it = wordvector.begin(); it != wordvector.end(); it++)
cout << (*it).first << " = " << (*it).second << "\n";
this does not limit the output to 20
am i missing something?
please teach me. this is the last problem to complete the program
just display the 20 most common words.
damn why cant i use the CODE button
Last edited by markrezak; Sep 24th, 2009 at 10:29 am.
>>am i missing something?
Just add a counter, when it reaches 20 then stop the loop. Also, pay attention to the quantities that are displayed because the smallest ones are sorted to the top, not to the bottom. So the first 20 items in that array will be the smallest, not the largest. To fix that you will need to change the sort callback function on line 18 of your original post to use > operator instead of <.
>>damn why cant i use the CODE button
I don't use a button, just add code tags manually
[code]
// put your code here
[/code]
NEW DANIWEB FEATURE Look at the top of the Quick Edit window, next to the other buttons, and you will see [code] button. Just click it and paste your code between the two tags as I showed above.
Just add a counter, when it reaches 20 then stop the loop. Also, pay attention to the quantities that are displayed because the smallest ones are sorted to the top, not to the bottom. So the first 20 items in that array will be the smallest, not the largest. To fix that you will need to change the sort callback function on line 18 of your original post to use > operator instead of <.
>>damn why cant i use the CODE button
I don't use a button, just add code tags manually
[code]
// put your code here
[/code]
NEW DANIWEB FEATURE Look at the top of the Quick Edit window, next to the other buttons, and you will see [code] button. Just click it and paste your code between the two tags as I showed above.
Last edited by Ancient Dragon; Sep 24th, 2009 at 11:01 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Sep 2009
Posts: 10
Reputation:
Solved Threads: 0
sir ancient dragon
i come up with this
i come up with this
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <algorithm> #include <string> #include <map> #include <vector> using namespace std; typedef map<string,int> word_count_list; struct val_lessthan : binary_function < pair<string,int>, pair<string,int>, bool > { bool operator() (const pair<string,int>& x, const pair<string,int>& y) const {return y.second<x.second;} }val_lt; int main() { word_count_list word_count; string filename; // Get the filename. cout << "Enter the file you wish to have searched:\n"; cin >> filename; // Open file. ifstream file(filename.c_str()); // Read in all the words. string word; while (file >> word){ // Remove punctuation. int index; while ((index = word.find_first_of(".,!?\\;-*+")) != string::npos) word.erase(index, 1); ++word_count[word]; } //copy pairs to vector vector<pair<string,int> > wordvector; vector<pair<string,int> >::iterator it; copy(word_count.begin(), word_count.end(), back_inserter(wordvector)); //sort the vector by second (value) instead of key sort(wordvector.begin(), wordvector.end(), val_lt); int i ; { for(it = wordvector.begin(),i=1; it!=wordvector.end(),i<21; ++it,++i) cout<<i<<(*it).first << " = " << (*it).second << "\n"; if(i=20) { system("pause"); } } }
Last edited by markrezak; Sep 24th, 2009 at 1:56 pm.
![]() |
Similar Threads
- how can i display the ms word document in asp page? (ASP)
- Code Snippet: Word Frequency using Python (Python)
- word count (C++)
- program that will input a number and display it in words (C)
- need help on my code about word frequency counter (C++)
- Word Frequency Counter Help (Java)
- display punctuations in word count hash map (Java)
- Word Frequency Count (Java)
- Display Longest and Shortest Word in a String (C++)
Other Threads in the C++ Forum
- Previous Thread: [HELP]File I/O in managed C++
- Next Thread: What does C++ const do?
| Thread Tools | Search this Thread |
api atlantis broadband broadbandprovision c++ character cloud conversion count crack database duplication encryption equipt excel exploit flash gis google googleearth googleocean ide imagery infinite java looping map menu microsoft myth news number numbertoword objects parsing password powerpoint project repetition reversing security sort statistics stickiness struct subroutine subscription text-file vb.net vba vector word wordfrequency







