Add SimpleMarkov class

This commit is contained in:
Joscha 2016-11-03 23:03:36 +00:00
parent b1048a5abc
commit 7eae3eea0d
3 changed files with 127 additions and 1 deletions

95
src/SimpleMarkov.cpp Normal file
View file

@ -0,0 +1,95 @@
#include "SimpleMarkov.hpp"
#include <chrono>
/*
class SimpleMarkov : public Markov
{
public:
SimpleMarkov(int length=10);
~SimpleMarkov();
virtual ~SimpleMarkov();
// loading and saving of internal state to files
virtual void load(std::string filename);
virtual void save(std::string filename);
virtual void throwUp(); // empty the belly of previous text
private:
virtual void swallowLine(std::string line);
virtual std::string regurgitateLine(std::string start="");
};
*/
SimpleMarkov::SimpleMarkov(int length) :
matrix(length),
generator(std::chrono::system_clock::now().time_since_epoch().count())
{
// do nothing
}
SimpleMarkov::~SimpleMarkov()
{
// do nothing
}
void SimpleMarkov::load(std::string filename)
{
// do nothing
}
void SimpleMarkov::save(std::string filename)
{
// do nothing
}
void SimpleMarkov::throwUp()
{
this->matrix.reset();
}
void SimpleMarkov::swallowLine(std::string line)
{
this->matrix.feed(line);
}
std::string SimpleMarkov::regurgitateLine(std::string start)
{
std::string line = "";
std::string next = "";
do {
// get the possible next characters from the matrix
std::map<std::string, int> possibles = this->matrix.hiccup(line);
// calculate sum of all possibilities
int sum = 0;
for (auto it : possibles) {
sum += it.second;
}
// generate random number in that range
int rand = std::uniform_int_distribution<>(1, sum)(this->generator);
// iterate over possibilities again, this time picking the character chosen by rand
sum = 0;
for (auto it : possibles) {
sum += it.second;
if (rand <= sum) {
next = it.first;
break;
}
}
// append the chosen character to the line
line += next;
} while (next != "");
return line;
}

31
src/SimpleMarkov.hpp Normal file
View file

@ -0,0 +1,31 @@
#ifndef SIMPLEMARKOV_HPP
#define SIMPLEMARKOV_HPP
#include "Markov.hpp"
#include "MarkovMatrix.hpp"
#include <random>
#include <string>
class SimpleMarkov : public Markov
{
public:
SimpleMarkov(int length=10);
virtual ~SimpleMarkov();
// loading and saving of internal state to files
virtual void load(std::string filename);
virtual void save(std::string filename);
virtual void throwUp(); // empty the belly of previous text
private:
virtual void swallowLine(std::string line);
virtual std::string regurgitateLine(std::string start="");
MarkovMatrix matrix;
std::default_random_engine generator;
};
#endif