Save to file (loading yet to be implemented)
This commit is contained in:
parent
35e485cb76
commit
a4785dd983
4 changed files with 109 additions and 5 deletions
|
|
@ -1,9 +1,17 @@
|
|||
#include "MarkovMatrix.hpp"
|
||||
|
||||
#include <algorithm> // for std::max
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
|
||||
MarkovMatrix::MarkovMatrix(int length)
|
||||
MarkovMatrix::MarkovMatrix()
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
|
||||
MarkovMatrix::MarkovMatrix(char 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);
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,16 +8,27 @@
|
|||
class MarkovMatrix
|
||||
{
|
||||
public:
|
||||
MarkovMatrix(int length);
|
||||
MarkovMatrix();
|
||||
MarkovMatrix(char length);
|
||||
~MarkovMatrix();
|
||||
|
||||
void feed(std::string& line);
|
||||
void reset(int length=0); // reset the matrix, length>0 -> set a new length
|
||||
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:
|
||||
int length;
|
||||
char length = 0;
|
||||
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
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
#include "SimpleMarkov.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
|
||||
/*
|
||||
class SimpleMarkov : public Markov
|
||||
|
|
@ -21,6 +23,10 @@ private:
|
|||
virtual std::string regurgitateLine(std::string start="");
|
||||
};
|
||||
*/
|
||||
SimpleMarkov::SimpleMarkov(std::string filename)
|
||||
{
|
||||
this->load(filename);
|
||||
}
|
||||
|
||||
SimpleMarkov::SimpleMarkov(int length) :
|
||||
matrix(length),
|
||||
|
|
@ -36,15 +42,52 @@ SimpleMarkov::~SimpleMarkov()
|
|||
}
|
||||
|
||||
|
||||
/*
|
||||
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)
|
||||
{
|
||||
// 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();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
class SimpleMarkov : public Markov
|
||||
{
|
||||
public:
|
||||
SimpleMarkov(std::string filename);
|
||||
SimpleMarkov(int length=10);
|
||||
virtual ~SimpleMarkov();
|
||||
|
||||
|
|
@ -26,6 +27,9 @@ private:
|
|||
|
||||
MarkovMatrix matrix;
|
||||
std::default_random_engine generator;
|
||||
|
||||
const char file_gentype = 's'; // type of the generator
|
||||
const char file_version = 1; // version of file format
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue