From c0e84ee19454cb1ebfc3849f362d3557fc28d35a Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 3 Nov 2016 18:46:38 +0000 Subject: [PATCH] Add Markov abstract base class --- Makefile | 2 +- src/Markov.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/Markov.hpp | 28 ++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/Markov.cpp create mode 100644 src/Markov.hpp diff --git a/Makefile b/Makefile index ae6b5a1..f37cc6c 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ RM = rm -f SRCDIR = src TMPDIR = build TARGET = trumpotron -FILES = main +FILES = main Markov #SOURCES = $(patsubst %,$(SRCDIR)/%.cpp,$(FILES)) OBJECTS = $(patsubst %,$(TMPDIR)/%.o,$(FILES)) diff --git a/src/Markov.cpp b/src/Markov.cpp new file mode 100644 index 0000000..89871f5 --- /dev/null +++ b/src/Markov.cpp @@ -0,0 +1,45 @@ +#include "Markov.hpp" + +#include +#include + + +Markov::~Markov() +{ + // does nothing +} + + +void Markov::eat(std::string filename) // gobble up a file +{ + std::ifstream ifs(filename); + for (std::string line; std::getline(ifs, line); ) + { + this->swallowLine(line); + } +} + + +void Markov::swallow(std::string& text) // ingest text to later regurgitate +{ + std::istringstream iss(text); + for (std::string line; std::getline(iss, line); ) + { + this->swallowLine(line); + } +} + + +std::string Markov::regurgitate(int lines, std::string start) +{ + // Not sure if it should return or just correct invalid input. + // For now, just correct it. + if (lines<1) lines = 1; + + std::string answer = ""; + for (int i=0; iregurgitateLine(start) + "\n"; + } + + return answer; +} diff --git a/src/Markov.hpp b/src/Markov.hpp new file mode 100644 index 0000000..30c3b07 --- /dev/null +++ b/src/Markov.hpp @@ -0,0 +1,28 @@ +#ifndef MARKOV_HPP +#define MARKOV_HPP + +#include + + +class Markov +{ +public: + virtual ~Markov(); + + // loading and saving of internal state to files + virtual void load(std::string filename) =0; + virtual void save(std::string filename) =0; + + void eat(std::string filename); // gobble up a file + void swallow(std::string& text); // ingest text to later regurgitate + virtual void throwUp() =0; // empty the belly of previous text + + // generate new text from previously eaten text + std::string regurgitate(int lines=1, std::string start=""); + +protected: + virtual void swallowLine(std::string line) =0; + virtual std::string regurgitateLine(std::string start="") =0; +}; + +#endif