diff --git a/Makefile b/Makefile index c464341..b712ee1 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ RM = rm -f SRCDIR = src TMPDIR = build TARGET = trumpotron -FILES = main Markov MarkovMatrix +FILES = main Markov MarkovMatrix SimpleMarkov #SOURCES = $(patsubst %,$(SRCDIR)/%.cpp,$(FILES)) OBJECTS = $(patsubst %,$(TMPDIR)/%.o,$(FILES)) diff --git a/src/SimpleMarkov.cpp b/src/SimpleMarkov.cpp new file mode 100644 index 0000000..4428d3e --- /dev/null +++ b/src/SimpleMarkov.cpp @@ -0,0 +1,95 @@ +#include "SimpleMarkov.hpp" + +#include + +/* +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 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; +} diff --git a/src/SimpleMarkov.hpp b/src/SimpleMarkov.hpp new file mode 100644 index 0000000..09d2998 --- /dev/null +++ b/src/SimpleMarkov.hpp @@ -0,0 +1,31 @@ +#ifndef SIMPLEMARKOV_HPP +#define SIMPLEMARKOV_HPP + +#include "Markov.hpp" +#include "MarkovMatrix.hpp" + +#include +#include + + +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