Advertisement
Guest User

Word Counter

a guest
Mar 1st, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. using namespace std;
  6. void mangle(string& word);
  7.  
  8. int main()
  9. {
  10.     vector <string> words;
  11.     vector <string> allWords;
  12.     vector <int> frequencies;
  13.  
  14.     string fileLocation;
  15.     cout<<"Enter a file location:"<<endl;
  16.     cin>>fileLocation;
  17.  
  18.     int freq = 0;
  19.     int currentMax = -10;
  20.     int currentMaxIndex;
  21.     float count = 0;
  22.     int amtOfTopWords;
  23.  
  24.     cout<<"\nEnter the amount of words you want to see the most used of:"<<endl;
  25.     cin>>amtOfTopWords;
  26.  
  27.     ifstream file;
  28.  
  29.     file.open(fileLocation.c_str());
  30.     while(!file.eof())
  31.     {
  32.         bool distinct = true;
  33.         string word;
  34.         file>>word;
  35.         mangle(word);
  36.         if(words.size() > 0)
  37.         {
  38.             for(int i=0; i < words.size(); i++)
  39.             {
  40.                 if(word == words[i])
  41.                 {
  42.                     distinct = false;
  43.                     count++;
  44.                     allWords.push_back(word);
  45.                     break;
  46.                 }
  47.             }
  48.         }
  49.         if(distinct == true)
  50.         {
  51.             words.push_back(word);
  52.             allWords.push_back(word);
  53.             count++;
  54.         }
  55.     }
  56.     for(int i = 0; i < words.size(); i++)
  57.     {
  58.         for(int a = 0; a < allWords.size(); a++)
  59.         {
  60.             if(words[i] == allWords[a])
  61.             {
  62.                 freq++;
  63.             }
  64.         }
  65.         frequencies.push_back(freq);
  66.         freq = 0;
  67.     }
  68.     cout<<"\n\nThe Top "<<amtOfTopWords<<" Most Used Words and Their Frequencies"<<endl;
  69.     cout<<"-----------------------------------------------"<<endl;
  70.     for(int i = 0; i < amtOfTopWords; i++)
  71.     {
  72.         for(int a = 0; a < frequencies.size(); a++)
  73.         {
  74.             if(frequencies[a] > currentMax)
  75.             {
  76.                 currentMax = frequencies[a];
  77.                 currentMaxIndex = a;
  78.             }
  79.         }
  80.         cout<<i+1<<") "<<words[currentMaxIndex]<<" -- "<<currentMax<<endl;
  81.         currentMax = -10;
  82.         frequencies[currentMaxIndex] = 0;
  83.     }
  84.     cout<<"\n\nWord Data"<<endl;
  85.     cout<<"---------"<<endl;
  86.     cout<<"There are "<<count<<" words and "<<words.size()<<" distinct words.\n\n\n";
  87.     system("pause");
  88. }
  89. void mangle(string& word)
  90. {
  91.     vector <string> garbage;
  92.     for(int i = 0; i < word.size(); i++)
  93.     {
  94.         word[i] = tolower(word[i]);
  95.         if(word[word.length()-1] == '\'')
  96.         {
  97.             word.erase(word.length()-1,1);
  98.         }
  99.         if(word[0] == '\'')
  100.         {
  101.             word.erase(0,1);
  102.         }
  103.         if(!(word[i] >= '\'' && word[i] <= 'z')  || ((word[i] < '0' && word[i] > '\'') || (word[i] < '\'') || (word[i] > '9' && word[i] < '\`')))
  104.         {
  105.             word.erase(i,1);
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement