diff options
Diffstat (limited to 'freevars.cpp')
-rw-r--r-- | freevars.cpp | 58 |
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 | ||
5 | freevars::freevars() | 5 | freevar::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 | ||
10 | void freevars::addVar(std::string name, std::string filename) | 19 | bool 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 | ||
30 | std::string freevars::parse(std::string in) | 24 | void 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 | { | 29 | word& 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 | ||