Skip to content

Commit

Permalink
Adds most common word functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ParkerBeck committed May 1, 2015
1 parent 9e1f7ec commit 39705ad
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
6 changes: 3 additions & 3 deletions FinalProject.depend
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# depslib dependency file v1.0
1430214344 source:c:\users\other people\beck_csci2270_finalproject\main.cpp
1430469489 source:c:\users\other people\beck_csci2270_finalproject\main.cpp
<iostream>
<string>
"MarkovChain.h"

1430216868 source:c:\users\other people\beck_csci2270_finalproject\markovchain.cpp
1430469505 source:c:\users\other people\beck_csci2270_finalproject\markovchain.cpp
"MarkovChain.h"
<map>
<vector>
Expand All @@ -16,7 +16,7 @@
<time.h>
<algorithm>

1430222373 c:\users\other people\beck_csci2270_finalproject\markovchain.h
1430468079 c:\users\other people\beck_csci2270_finalproject\markovchain.h
<map>
<vector>
<string>
Expand Down
37 changes: 37 additions & 0 deletions MarkovChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,43 @@ MarkovChain::~MarkovChain()
//dtor
}



void MarkovChain::printMostCommon(int number)
{
std::vector<string> addedWords;

for(int i = 0; i < number && i < wordList.size(); i++)
{
std::string currentMaxWord = "";
int currentMaxCount = 0;
for (auto const& w : wordList)
{
int currentCount = nextAppearanceSum(w.second.nextWords);
if(currentCount > currentMaxCount)
{
if(std::find(addedWords.begin(), addedWords.end(), w.first) == addedWords.end())
{
currentMaxWord = w.first;
currentMaxCount = currentCount;
}
}
}
addedWords.push_back(currentMaxWord);
}
for(int i = 0; i < addedWords.size(); i++)
{
cout << addedWords[i] << "; ";
if(i % 4 == 3)
{
cout << endl;
}
}
cout << endl;

}


void MarkovChain::addWord(std::string wordString)
{
wordList[wordString] = Word();
Expand Down
3 changes: 3 additions & 0 deletions MarkovChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ class MarkovChain
*/
std::string mostUsedNextWord(std::vector<NextWord> nextwords);

void printMostCommon(int number);

protected:
private:
std::map<std::string, Word> wordList;
Expand Down
30 changes: 18 additions & 12 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using namespace std;

int main()
{
string ans;
std::string ans = "Y";
MarkovChain *mc = new MarkovChain();
std::string userInput;
bool program = true;
Expand Down Expand Up @@ -54,27 +54,33 @@ int main()
{
do
{
cout << "What word do you want to start the generation?"<<endl;
cout << "What word do you want to start the generation? Enter '?' for suggestions. "<<endl;
getline(cin, userInput);
if(mc->generateText(userInput))
if(userInput == "?")
{
cout << "File generated under: " << userInput << ".txt" << endl;
program = false;
cout << "Some suggested starting words are: " << endl;
mc->printMostCommon(10);
}
else
{
cout << "Sorry that word does not appear in the sample file! ";
cout << "Please choose a different starting word" << endl;
}
if(mc->generateText(userInput))
{
cout << "File generated under: " << userInput << ".txt" << endl;
program = false;
}
else
{
cout << "Sorry that word does not appear in the sample file! ";
cout << "Please choose a different starting word" << endl;
}

cout << "Enter 'y' or 'Y' to generate another file." << endl;
getline(cin, ans);
cout << "Enter 'y' or 'Y' to generate another file." << endl;
getline(cin, ans);
}
}
while ( "y" == ans || "Y" == ans );
cout << "All done!" << endl;
break;


}
}

Expand Down

0 comments on commit 39705ad

Please sign in to comment.