Add Markov abstract base class
This commit is contained in:
parent
3e9eb17523
commit
c0e84ee194
3 changed files with 74 additions and 1 deletions
2
Makefile
2
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))
|
||||
|
|
|
|||
45
src/Markov.cpp
Normal file
45
src/Markov.cpp
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#include "Markov.hpp"
|
||||
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
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; i<lines; ++i) {
|
||||
answer += this->regurgitateLine(start) + "\n";
|
||||
}
|
||||
|
||||
return answer;
|
||||
}
|
||||
28
src/Markov.hpp
Normal file
28
src/Markov.hpp
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#ifndef MARKOV_HPP
|
||||
#define MARKOV_HPP
|
||||
|
||||
#include <string>
|
||||
|
||||
|
||||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue