about summary refs log tree commit diff stats
path: root/freevars.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2016-02-01 09:30:04 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2016-02-01 09:30:04 -0500
commit617155fe562652c859a380d85cc5710783d79448 (patch)
treef5eee89b0fa4b3c9dfe7187ca78916a71b59045e /freevars.cpp
parentb316e309559d7176af6cf0bb7dcd6dbaa83c01cd (diff)
downloadrawr-ebooks-617155fe562652c859a380d85cc5710783d79448.tar.gz
rawr-ebooks-617155fe562652c859a380d85cc5710783d79448.tar.bz2
rawr-ebooks-617155fe562652c859a380d85cc5710783d79448.zip
Added emoji freevar
Strings of emojis are tokenized separately from anything else, and added to an emoticon freevar, which is mixed in with regular emoticons like :P. This breaks old-style freevars like $name$ and $noun$ so some legacy support for compatibility is left in but eventually $name$ should be made into an actual new freevar. Emoji data is from gemoji (https://github.com/github/gemoji).
Diffstat (limited to 'freevars.cpp')
-rw-r--r--freevars.cpp58
1 files changed, 22 insertions, 36 deletions
diff --git a/freevars.cpp b/freevars.cpp index 54c5aab..4429d00 100644 --- a/freevars.cpp +++ b/freevars.cpp
@@ -1,46 +1,32 @@
1#include "freevars.h" 1#include "freevars.h"
2#include <fstream> 2#include <fstream>
3#include <cstdlib> 3#include "kgramstats.h"
4 4
5freevars::freevars() 5freevar::freevar(word& w, std::string file) : w(w)
6{ 6{
7 vars = new std::map<std::string, std::vector<std::string>* >(); 7 std::ifstream infile(file);
8 if (infile)
9 {
10 std::string line;
11 while (getline(infile, line))
12 {
13 instances.insert(line);
14 w.forms.add(line);
15 }
16 }
8} 17}
9 18
10void freevars::addVar(std::string name, std::string filename) 19bool freevar::check(std::string f) const
11{ 20{
12 std::vector<std::string>* eltlist = new std::vector<std::string>(); 21 return (instances.count(f) == 1);
13
14 std::ifstream infile(filename.c_str());
15 if (infile)
16 {
17 std::string line;
18
19 while (getline(infile, line))
20 {
21 eltlist->push_back(line);
22 }
23 } else {
24 eltlist->push_back("");
25 }
26
27 (*vars)[name] = eltlist;
28} 22}
29 23
30std::string freevars::parse(std::string in) 24void freevar::add(std::string f)
31{ 25{
32 std::string res(in); 26 instances.insert(f);
33 27}
34 for (std::map<std::string, std::vector<std::string>* >::iterator it = vars->begin(); it != vars->end(); it++) 28
35 { 29word& freevar::getWord()
36 std::string tofind = "$" + it->first + "$"; 30{
37 size_t fpos; 31 return w;
38 while ((fpos = res.find(tofind)) != std::string::npos) 32}
39 {
40 int r = rand() % it->second->size();
41 res.replace(fpos, tofind.length(), (*it->second)[r], 0, std::string::npos);
42 }
43 }
44
45 return res;
46} \ No newline at end of file