about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorFeffernoose <fefferburbia@gmail.com>2013-10-07 23:32:37 -0400
committerFeffernoose <fefferburbia@gmail.com>2013-10-07 23:32:37 -0400
commit40bf33c9df2d24219826147417ad07dd21e35d17 (patch)
tree91a06d9c5aa4adbe927bddec5522e1b27c6756e5
parentd4b193e6e7fdefc66d9698337581b960c30844ea (diff)
downloadrawr-ebooks-40bf33c9df2d24219826147417ad07dd21e35d17.tar.gz
rawr-ebooks-40bf33c9df2d24219826147417ad07dd21e35d17.tar.bz2
rawr-ebooks-40bf33c9df2d24219826147417ad07dd21e35d17.zip
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.
-rw-r--r--Makefile.am4
-rw-r--r--ebooks.cpp8
-rw-r--r--freevars.cpp45
-rw-r--r--freevars.h21
-rw-r--r--gen.cpp8
5 files changed, 82 insertions, 4 deletions
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
2ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} 2ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS}
3 3
4bin_PROGRAMS = rawr-ebooks rawr-gen 4bin_PROGRAMS = rawr-ebooks rawr-gen
5rawr_ebooks_SOURCES = ebooks.cpp kgramstats.cpp 5rawr_ebooks_SOURCES = ebooks.cpp kgramstats.cpp freevars.cpp
6rawr_gen_SOURCES = gen.cpp kgramstats.cpp 6rawr_gen_SOURCES = gen.cpp kgramstats.cpp freevars.cpp
7rawr_ebooks_CPPFLAGS = $(LIBTWITCURL_CFLAGS) $(YAML_CFLAGS) 7rawr_ebooks_CPPFLAGS = $(LIBTWITCURL_CFLAGS) $(YAML_CFLAGS)
8rawr_ebooks_LDADD = $(LIBTWITCURL_LIBS) $(YAML_LIBS) \ No newline at end of file 8rawr_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 @@
10#include <twitcurl.h> 10#include <twitcurl.h>
11#include <unistd.h> 11#include <unistd.h>
12#include <yaml-cpp/yaml.h> 12#include <yaml-cpp/yaml.h>
13#include "freevars.h"
13 14
14using namespace::std; 15using namespace::std;
15 16
@@ -28,6 +29,11 @@ int main(int argc, char** args)
28 29
29 cout << "Preprocessing corpus..." << endl; 30 cout << "Preprocessing corpus..." << endl;
30 kgramstats* stats = new kgramstats(corpus, 5); 31 kgramstats* stats = new kgramstats(corpus, 5);
32
33 cout << "Preprocessing freevars..." << endl;
34 freevars* vars = new freevars();
35 vars->addVar("name", "names.txt");
36 vars->addVar("noun", "nouns.txt");
31 37
32 cout << "Generating..." << endl; 38 cout << "Generating..." << endl;
33 for (;;) 39 for (;;)
@@ -36,7 +42,7 @@ int main(int argc, char** args)
36 string hi; 42 string hi;
37 for (vector<string>::iterator it = doc.begin(); it != doc.end(); ++it) 43 for (vector<string>::iterator it = doc.begin(); it != doc.end(); ++it)
38 { 44 {
39 hi += *it + " "; 45 hi += vars->parse(*it) + " ";
40 } 46 }
41 47
42 hi = hi.substr(0,140); 48 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 @@
1#include "freevars.h"
2#include <fstream>
3
4freevars::freevars()
5{
6 vars = new map<string, vector<string>* >();
7}
8
9void freevars::addVar(string name, string filename)
10{
11 vector<string>* eltlist = new vector<string>();
12
13 ifstream infile(filename.c_str());
14 if (infile)
15 {
16 string line;
17
18 while (getline(infile, line))
19 {
20 eltlist->push_back(line);
21 }
22 } else {
23 eltlist->push_back("");
24 }
25
26 (*vars)[name] = eltlist;
27}
28
29string freevars::parse(string in)
30{
31 string res(in);
32
33 for (map<string, vector<string>* >::iterator it = vars->begin(); it != vars->end(); it++)
34 {
35 string tofind = "$" + it->first + "$";
36 size_t fpos = res.find(tofind);
37 if (fpos != string::npos)
38 {
39 int r = rand() % it->second->size();
40 res.replace(fpos, tofind.length(), (*it->second)[r], 0, string::npos);
41 }
42 }
43
44 return res;
45} \ 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 @@
1#include <map>
2#include <string>
3#include <vector>
4
5using namespace::std;
6
7#ifndef FREEVARS_H
8#define FREEVARS_H
9
10class freevars
11{
12public:
13 freevars();
14 void addVar(string name, string filename);
15 string parse(string in);
16
17private:
18 map<string, vector<string>* >* vars;
19};
20
21#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 @@
7#include <cstdlib> 7#include <cstdlib>
8#include <fstream> 8#include <fstream>
9#include <iostream> 9#include <iostream>
10#include "freevars.h"
10 11
11using namespace::std; 12using namespace::std;
12 13
@@ -45,6 +46,11 @@ int main(int argc, char** args)
45 cout << "Preprocessing corpus..." << endl; 46 cout << "Preprocessing corpus..." << endl;
46 kgramstats* stats = new kgramstats(corpus, 5); 47 kgramstats* stats = new kgramstats(corpus, 5);
47 48
49 cout << "Preprocessing freevars..." << endl;
50 freevars* vars = new freevars();
51 vars->addVar("name", "names.txt");
52 vars->addVar("noun", "nouns.txt");
53
48 cout << "Generating..." << endl; 54 cout << "Generating..." << endl;
49 for (;;) 55 for (;;)
50 { 56 {
@@ -52,7 +58,7 @@ int main(int argc, char** args)
52 string hi; 58 string hi;
53 for (vector<string>::iterator it = doc.begin(); it != doc.end(); ++it) 59 for (vector<string>::iterator it = doc.begin(); it != doc.end(); ++it)
54 { 60 {
55 hi += *it + " "; 61 hi += vars->parse(*it) + " ";
56 } 62 }
57 63
58 cout << hi << endl; 64 cout << hi << endl;