From 40bf33c9df2d24219826147417ad07dd21e35d17 Mon Sep 17 00:00:00 2001 From: Feffernoose Date: Mon, 7 Oct 2013 23:32:37 -0400 Subject: Implemented freevars Arbitrary variable tokens can now be defined (though at this point only in the code itself) as a pair of a variable name and a filename pointing to a plain text file containing a newline-delimited list of elements. When a token of the form $name$ (where name is the name of a variable) is encountered, the output will include a random element from the appropriate list. The variables $name$ and $noun$ are hard-coded at this point, but the program will not crash if names.txt and nouns.txt do not exist and will instead just silently ignore the variables. --- Makefile.am | 4 ++-- ebooks.cpp | 8 +++++++- freevars.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ freevars.h | 21 +++++++++++++++++++++ gen.cpp | 8 +++++++- 5 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 freevars.cpp create mode 100644 freevars.h diff --git a/Makefile.am b/Makefile.am index 3d4dad6..299dc10 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,7 +2,7 @@ AUTOMAKE_OPTIONS = subdir-objects ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} bin_PROGRAMS = rawr-ebooks rawr-gen -rawr_ebooks_SOURCES = ebooks.cpp kgramstats.cpp -rawr_gen_SOURCES = gen.cpp kgramstats.cpp +rawr_ebooks_SOURCES = ebooks.cpp kgramstats.cpp freevars.cpp +rawr_gen_SOURCES = gen.cpp kgramstats.cpp freevars.cpp rawr_ebooks_CPPFLAGS = $(LIBTWITCURL_CFLAGS) $(YAML_CFLAGS) rawr_ebooks_LDADD = $(LIBTWITCURL_LIBS) $(YAML_LIBS) \ No newline at end of file diff --git a/ebooks.cpp b/ebooks.cpp index ed660a9..753ead6 100644 --- a/ebooks.cpp +++ b/ebooks.cpp @@ -10,6 +10,7 @@ #include #include #include +#include "freevars.h" using namespace::std; @@ -28,6 +29,11 @@ int main(int argc, char** args) cout << "Preprocessing corpus..." << endl; kgramstats* stats = new kgramstats(corpus, 5); + + cout << "Preprocessing freevars..." << endl; + freevars* vars = new freevars(); + vars->addVar("name", "names.txt"); + vars->addVar("noun", "nouns.txt"); cout << "Generating..." << endl; for (;;) @@ -36,7 +42,7 @@ int main(int argc, char** args) string hi; for (vector::iterator it = doc.begin(); it != doc.end(); ++it) { - hi += *it + " "; + hi += vars->parse(*it) + " "; } hi = hi.substr(0,140); diff --git a/freevars.cpp b/freevars.cpp new file mode 100644 index 0000000..ca69f6c --- /dev/null +++ b/freevars.cpp @@ -0,0 +1,45 @@ +#include "freevars.h" +#include + +freevars::freevars() +{ + vars = new map* >(); +} + +void freevars::addVar(string name, string filename) +{ + vector* eltlist = new vector(); + + ifstream infile(filename.c_str()); + if (infile) + { + string line; + + while (getline(infile, line)) + { + eltlist->push_back(line); + } + } else { + eltlist->push_back(""); + } + + (*vars)[name] = eltlist; +} + +string freevars::parse(string in) +{ + string res(in); + + for (map* >::iterator it = vars->begin(); it != vars->end(); it++) + { + string tofind = "$" + it->first + "$"; + size_t fpos = res.find(tofind); + if (fpos != string::npos) + { + int r = rand() % it->second->size(); + res.replace(fpos, tofind.length(), (*it->second)[r], 0, string::npos); + } + } + + return res; +} \ No newline at end of file diff --git a/freevars.h b/freevars.h new file mode 100644 index 0000000..6fdc4c2 --- /dev/null +++ b/freevars.h @@ -0,0 +1,21 @@ +#include +#include +#include + +using namespace::std; + +#ifndef FREEVARS_H +#define FREEVARS_H + +class freevars +{ +public: + freevars(); + void addVar(string name, string filename); + string parse(string in); + +private: + map* >* vars; +}; + +#endif FREEVARS_H \ No newline at end of file diff --git a/gen.cpp b/gen.cpp index e4a58e5..1d381c8 100644 --- a/gen.cpp +++ b/gen.cpp @@ -7,6 +7,7 @@ #include #include #include +#include "freevars.h" using namespace::std; @@ -45,6 +46,11 @@ int main(int argc, char** args) cout << "Preprocessing corpus..." << endl; kgramstats* stats = new kgramstats(corpus, 5); + cout << "Preprocessing freevars..." << endl; + freevars* vars = new freevars(); + vars->addVar("name", "names.txt"); + vars->addVar("noun", "nouns.txt"); + cout << "Generating..." << endl; for (;;) { @@ -52,7 +58,7 @@ int main(int argc, char** args) string hi; for (vector::iterator it = doc.begin(); it != doc.end(); ++it) { - hi += *it + " "; + hi += vars->parse(*it) + " "; } cout << hi << endl; -- cgit 1.4.1