So I have a file(story.dat) with a randomly generated story like: "Sleeping in his car was never the plan but sometimes things don't work out as planned. This..."
It will print out each word and the corresponding word to it like so:
0."It: was,
1."Pepsi-Cola.": He,
2."This: is,
3./: 50.,
4.50: /,
5.50.: I,
6.A: long,
7.All: that,
8.He: didn't,knew,had,
9.I: think,had,
10.I'd: love,
11.I'm: certain,
12.It: was,
13.Or: at,
14.She: knew,
15.Sleeping: in, ...
And then it prints the story like so:
have a cup of everything. The inside was ten, and a little strange, but he was either exhausted of days ago. It was supposed to hear from you today and fondling over it. He knew what made the last option is to make a year ago. It was supposed to make this case, was in months. The third is the sun. He knew...
Here's the code:
// #include files
namespace Randomiser
{
std::mt19937 generator{ static_cast<std::mt19937::result_type>(std::time(nullptr)) };
}
int getRandomNumber(int min, int max)
{
std::uniform_int_distribution<int> rand{ min, max };
return rand(Randomiser::generator);
}
void markovIt(const std::map<std::string, std::vector<std::string>>& m)
{
std::string temp{};
std::vector<std::string> possibilities{};
std::string next{};
int i{ 0 };
int randomNumber{ getRandomNumber(0, m.size()) };
for (const auto& n : m)
{
if (i == randomNumber)
{
temp = n.first;
}
++i;
}
auto found{ m.find(temp) };
i = 0;
while(found != m.end())
{
if (i == 255)
break;
possibilities = found->second;
next = possibilities.at(getRandomNumber(0, possibilities.size() - 1));
std::cout << temp << " ";
temp = next;
found = m.find(temp);
++i;
}
}
int main()
{
std::ifstream wordStream{ "story.dat" };
std::string fWord;
std::string sWord;
std::vector<std::string> temp;
std::map<std::string, std::vector<std::string>> gram{};
int i{ 0 };
while(wordStream)
{
wordStream >> fWord;
if (i != 0)
gram[sWord].push_back(fWord);
wordStream >> sWord;
gram[fWord].push_back(sWord);
if (i == 0)
i++;
}
i = 0;
for (const auto& n : gram)
{
std::cout << i << "." << n.first << ": ";
for (const auto& key : n.second)
std::cout << key << ",";
std::cout << '\n';
++i;
}
markovIt(gram);
std::cout << '\n';
return 0;
}
The implementation feels awful and I believe it could be optimised alot further. I'm just not sure where to take it.