Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- using namespace std;
- void mangle(string& word);
- int main()
- {
- vector <string> words;
- vector <string> allWords;
- vector <int> frequencies;
- string fileLocation;
- cout<<"Enter a file location:"<<endl;
- cin>>fileLocation;
- int freq = 0;
- int currentMax = -10;
- int currentMaxIndex;
- float count = 0;
- int amtOfTopWords;
- cout<<"\nEnter the amount of words you want to see the most used of:"<<endl;
- cin>>amtOfTopWords;
- ifstream file;
- file.open(fileLocation.c_str());
- while(!file.eof())
- {
- bool distinct = true;
- string word;
- file>>word;
- mangle(word);
- if(words.size() > 0)
- {
- for(int i=0; i < words.size(); i++)
- {
- if(word == words[i])
- {
- distinct = false;
- count++;
- allWords.push_back(word);
- break;
- }
- }
- }
- if(distinct == true)
- {
- words.push_back(word);
- allWords.push_back(word);
- count++;
- }
- }
- for(int i = 0; i < words.size(); i++)
- {
- for(int a = 0; a < allWords.size(); a++)
- {
- if(words[i] == allWords[a])
- {
- freq++;
- }
- }
- frequencies.push_back(freq);
- freq = 0;
- }
- cout<<"\n\nThe Top "<<amtOfTopWords<<" Most Used Words and Their Frequencies"<<endl;
- cout<<"-----------------------------------------------"<<endl;
- for(int i = 0; i < amtOfTopWords; i++)
- {
- for(int a = 0; a < frequencies.size(); a++)
- {
- if(frequencies[a] > currentMax)
- {
- currentMax = frequencies[a];
- currentMaxIndex = a;
- }
- }
- cout<<i+1<<") "<<words[currentMaxIndex]<<" -- "<<currentMax<<endl;
- currentMax = -10;
- frequencies[currentMaxIndex] = 0;
- }
- cout<<"\n\nWord Data"<<endl;
- cout<<"---------"<<endl;
- cout<<"There are "<<count<<" words and "<<words.size()<<" distinct words.\n\n\n";
- system("pause");
- }
- void mangle(string& word)
- {
- vector <string> garbage;
- for(int i = 0; i < word.size(); i++)
- {
- word[i] = tolower(word[i]);
- if(word[word.length()-1] == '\'')
- {
- word.erase(word.length()-1,1);
- }
- if(word[0] == '\'')
- {
- word.erase(0,1);
- }
- if(!(word[i] >= '\'' && word[i] <= 'z') || ((word[i] < '0' && word[i] > '\'') || (word[i] < '\'') || (word[i] > '9' && word[i] < '\`')))
- {
- word.erase(i,1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement