Save to file (loading yet to be implemented)

This commit is contained in:
Joscha 2016-11-20 14:22:26 +00:00
parent 35e485cb76
commit a4785dd983
4 changed files with 109 additions and 5 deletions

View file

@ -1,9 +1,17 @@
#include "MarkovMatrix.hpp" #include "MarkovMatrix.hpp"
#include <algorithm> // for std::max #include <algorithm> // for std::max
#include <iostream>
#include <stdexcept>
MarkovMatrix::MarkovMatrix(int length) MarkovMatrix::MarkovMatrix()
{
// do nothing
}
MarkovMatrix::MarkovMatrix(char length)
{ {
this->length = length; this->length = length;
} }
@ -45,3 +53,41 @@ std::unordered_map<std::string, int> MarkovMatrix::hiccup(std::string& previous)
std::string before = previous.substr(pos, len); std::string before = previous.substr(pos, len);
return this->matrix.at(before); return this->matrix.at(before);
} }
void MarkovMatrix::load(std::istream& is)
{
char file_version;
is.read(&file_version, sizeof(char));
}
void MarkovMatrix::save(std::ostream& os)
{
os << this->file_version << std::endl;
os << this->matrix.size() << std::endl;
for (auto map : this->matrix) {
os << map.first << std::endl;
os << map.second.size() << std::endl;
for (auto character : map.second) {
os << character.first << std::endl;
os << character.second << std::endl;
}
}
}
std::istream& operator>>(std::istream& is, MarkovMatrix& matrix)
{
matrix.load(is);
return is;
}
std::ostream& operator<<(std::ostream& os, MarkovMatrix& matrix)
{
matrix.save(os);
return os;
}

View file

@ -8,16 +8,27 @@
class MarkovMatrix class MarkovMatrix
{ {
public: public:
MarkovMatrix(int length); MarkovMatrix();
MarkovMatrix(char length);
~MarkovMatrix(); ~MarkovMatrix();
void feed(std::string& line); void feed(std::string& line);
void reset(int length=0); // reset the matrix, length>0 -> set a new length void reset(int length=0); // reset the matrix, length>0 -> set a new length
std::unordered_map<std::string, int> hiccup(std::string& previous); std::unordered_map<std::string, int> hiccup(std::string& previous);
// loading and saving of internal state to files
virtual void load(std::istream& is);
virtual void save(std::ostream& os);
private: private:
int length; char length = 0;
std::unordered_map<std::string, std::unordered_map<std::string, int> > matrix; std::unordered_map<std::string, std::unordered_map<std::string, int> > matrix;
const char file_version = 1; // version of file format
}; };
std::istream& operator>>(std::istream& is, MarkovMatrix& matrix);
std::ostream& operator<<(std::ostream& os, MarkovMatrix& matrix);
#endif #endif

View file

@ -1,6 +1,8 @@
#include "SimpleMarkov.hpp" #include "SimpleMarkov.hpp"
#include <chrono> #include <chrono>
#include <fstream>
#include <stdexcept>
/* /*
class SimpleMarkov : public Markov class SimpleMarkov : public Markov
@ -21,6 +23,10 @@ private:
virtual std::string regurgitateLine(std::string start=""); virtual std::string regurgitateLine(std::string start="");
}; };
*/ */
SimpleMarkov::SimpleMarkov(std::string filename)
{
this->load(filename);
}
SimpleMarkov::SimpleMarkov(int length) : SimpleMarkov::SimpleMarkov(int length) :
matrix(length), matrix(length),
@ -36,15 +42,52 @@ SimpleMarkov::~SimpleMarkov()
} }
/*
void SimpleMarkov::load(std::string filename) void SimpleMarkov::load(std::string filename)
{ {
// do nothing std::ifstream ifs(filename);
std::string type, version;
ifs >> type >> version;
if (type != "s" || version != "0001") {
throw std::invalid_argument("Wrong file type or version.");
}
ifs >> this->matrix;
ifs.close();
} }
void SimpleMarkov::save(std::string filename) void SimpleMarkov::save(std::string filename)
{ {
// do nothing std::ofstream ofs(filename);
ofs << "s" << std::endl;
ofs << "0001" << std::endl;
ofs << this->matrix;
ofs.close();
}
*/
void SimpleMarkov::load(std::string filename)
{
std::ifstream ifs(filename);
ifs.close();
}
void SimpleMarkov::save(std::string filename)
{
std::ofstream ofs(filename);
// file type and version of format
ofs << this->file_gentype << std::endl;
ofs << this->file_version << std::endl;
ofs << this->matrix;
ofs.close();
} }

View file

@ -11,6 +11,7 @@
class SimpleMarkov : public Markov class SimpleMarkov : public Markov
{ {
public: public:
SimpleMarkov(std::string filename);
SimpleMarkov(int length=10); SimpleMarkov(int length=10);
virtual ~SimpleMarkov(); virtual ~SimpleMarkov();
@ -26,6 +27,9 @@ private:
MarkovMatrix matrix; MarkovMatrix matrix;
std::default_random_engine generator; std::default_random_engine generator;
const char file_gentype = 's'; // type of the generator
const char file_version = 1; // version of file format
}; };
#endif #endif