943,557 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2908
  • C++ RSS
Sep 23rd, 2009
0

Display only the 20 most used words[Word Frequency]

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. ok ok i got it now right i copy it to vector and sort it..... here is the code
  2.  
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. #include <algorithm>
  7. #include <string>
  8. #include <map>
  9. #include <vector>
  10.  
  11. using namespace std;
  12.  
  13. typedef map<string,int> word_count_list;
  14.  
  15. struct val_lessthan : binary_function < pair<string,int>, pair<string,int>, bool >
  16. {
  17. bool operator() (const pair<string,int>& x, const pair<string,int>& y) const
  18. {return x.second<y.second;}
  19. }val_lt;
  20.  
  21. int main()
  22. {
  23. word_count_list word_count;
  24. string filename;
  25.  
  26. // Get the filename.
  27. cout << "Enter the file you wish to have searched:\n";
  28. cin >> filename;
  29.  
  30. // Open file.
  31. ifstream file(filename.c_str());
  32.  
  33. // Read in all the words.
  34. string word;
  35.  
  36. while (file >> word){
  37. // Remove punctuation.
  38. int index;
  39. while ((index = word.find_first_of(".,!?\\;-*+")) != string::npos)
  40. word.erase(index, 1);
  41.  
  42. ++word_count[word];
  43. }
  44.  
  45. //copy pairs to vector
  46. vector<pair<string,int> > wordvector;
  47. copy(word_count.begin(), word_count.end(), back_inserter(wordvector));
  48.  
  49. //sort the vector by second (value) instead of key
  50. sort(wordvector.begin(), wordvector.end(), val_lt);
  51.  
  52. for(int i=0; i<wordvector.size(); ++i)
  53. cout << wordvector[i].first << " = " << wordvector[i].second << endl;
  54.  
  55. return 0;
  56. }
  57.  
  58. to finish the porgram. i need to output the 20 most common or used words....
  59.  
  60. i try changing
  61. for(int i=0; i<wordvector.size(); ++i)
  62. to for9int i=0,i<=20;i++)
  63.  
  64. but nothing happens
Similar Threads
Reputation Points: 2
Solved Threads: 0
Newbie Poster
markrezak is offline Offline
10 posts
since Sep 2009
Sep 24th, 2009
0

Re: Display only the 20 most used words[Word Frequency]

cant get it right hhmmp
Reputation Points: 2
Solved Threads: 0
Newbie Poster
markrezak is offline Offline
10 posts
since Sep 2009
Sep 24th, 2009
-7

Re: Display only the 20 most used words[Word Frequency]

This should do it
C++ Syntax (Toggle Plain Text)
  1. vector<pair<string,int> >::iterator it;
  2.  
  3. for(it = wordvector.begin(); it != wordvector.end(); it++)
  4. cout << (*it).first << " = " << (*it).second << "\n";
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Sep 24th, 2009
0

Re: Display only the 20 most used words[Word Frequency]

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
Last edited by markrezak; Sep 24th, 2009 at 10:29 am.
Reputation Points: 2
Solved Threads: 0
Newbie Poster
markrezak is offline Offline
10 posts
since Sep 2009
Sep 24th, 2009
-7

Re: Display only the 20 most used words[Word Frequency]

>>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.
Last edited by Ancient Dragon; Sep 24th, 2009 at 11:01 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Sep 24th, 2009
0

Re: Display only the 20 most used words[Word Frequency]

sir ancient dragon

i come up with this
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <algorithm>
  4. #include <string>
  5. #include <map>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. typedef map<string,int> word_count_list;
  11.  
  12. struct val_lessthan : binary_function < pair<string,int>, pair<string,int>, bool >
  13. {
  14. bool operator() (const pair<string,int>& x, const pair<string,int>& y) const
  15. {return y.second<x.second;}
  16. }val_lt;
  17.  
  18. int main()
  19. {
  20.  
  21. word_count_list word_count;
  22. string filename;
  23.  
  24. // Get the filename.
  25. cout << "Enter the file you wish to have searched:\n";
  26. cin >> filename;
  27.  
  28. // Open file.
  29. ifstream file(filename.c_str());
  30.  
  31. // Read in all the words.
  32. string word;
  33.  
  34. while (file >> word){
  35. // Remove punctuation.
  36. int index;
  37. while ((index = word.find_first_of(".,!?\\;-*+")) != string::npos)
  38. word.erase(index, 1);
  39.  
  40. ++word_count[word];
  41. }
  42.  
  43. //copy pairs to vector
  44. vector<pair<string,int> > wordvector;
  45. vector<pair<string,int> >::iterator it;
  46. copy(word_count.begin(), word_count.end(), back_inserter(wordvector));
  47.  
  48. //sort the vector by second (value) instead of key
  49. sort(wordvector.begin(), wordvector.end(), val_lt);
  50. int i ;
  51. {
  52. for(it = wordvector.begin(),i=1; it!=wordvector.end(),i<21; ++it,++i)
  53. cout<<i<<(*it).first << " = " << (*it).second << "\n";
  54. if(i=20)
  55. {
  56.  
  57. system("pause");
  58. }
  59. }
  60. }
Last edited by markrezak; Sep 24th, 2009 at 1:56 pm.
Reputation Points: 2
Solved Threads: 0
Newbie Poster
markrezak is offline Offline
10 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: [HELP]File I/O in managed C++
Next Thread in C++ Forum Timeline: What does C++ const do?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC