Load files

This commit is contained in:
Joscha 2016-11-20 17:58:02 +00:00
parent a4785dd983
commit 029860bd79
4 changed files with 49 additions and 7 deletions

View file

@ -11,7 +11,7 @@ MarkovMatrix::MarkovMatrix()
}
MarkovMatrix::MarkovMatrix(char length)
MarkovMatrix::MarkovMatrix(int length)
{
this->length = length;
}
@ -57,14 +57,44 @@ std::unordered_map<std::string, int> MarkovMatrix::hiccup(std::string& previous)
void MarkovMatrix::load(std::istream& is)
{
char file_version;
is.read(&file_version, sizeof(char));
int version;
is >> version;
if (version != this->file_version) {
return;
}
is >> this->length;
int size;
is >> size;
std::cout << "size: " << size << std::endl;
std::string read; std::getline(is, read);
for (int i=0; i<size; ++i) {
// read line as string
std::string before;
std::getline(is, before);
// read line as int
std::getline(is, read);
int characters = std::stoi(read);
for (int j=0; j<characters; ++j) {
// read line as string
std::string current;
std::getline(is, current);
// read line as int
std::getline(is, read);
int count = std::stoi(read);
this->matrix[before][current] = count;
}
}
}
void MarkovMatrix::save(std::ostream& os)
{
os << this->file_version << std::endl;
os << this->length << std::endl;
os << this->matrix.size() << std::endl;
for (auto map : this->matrix) {

View file

@ -9,7 +9,7 @@ class MarkovMatrix
{
public:
MarkovMatrix();
MarkovMatrix(char length);
MarkovMatrix(int length);
~MarkovMatrix();
void feed(std::string& line);
@ -21,10 +21,10 @@ public:
virtual void save(std::ostream& os);
private:
char length = 0;
int length = 0;
std::unordered_map<std::string, std::unordered_map<std::string, int> > matrix;
const char file_version = 1; // version of file format
const int file_version = 1; // version of file format
};

View file

@ -4,6 +4,12 @@
#include <fstream>
#include <stdexcept>
#include <iostream>
/*
class SimpleMarkov : public Markov
{
@ -72,7 +78,13 @@ void SimpleMarkov::load(std::string filename)
{
std::ifstream ifs(filename);
char gentype;
int version;
ifs >> gentype >> version;
if (gentype == this->file_gentype and version == this->file_version) {
ifs >> this->matrix;
}
ifs.close();
}

View file

@ -29,7 +29,7 @@ private:
std::default_random_engine generator;
const char file_gentype = 's'; // type of the generator
const char file_version = 1; // version of file format
const int file_version = 1; // version of file format
};
#endif