diff options
Diffstat (limited to 'lib/verb.cpp')
-rw-r--r-- | lib/verb.cpp | 169 |
1 files changed, 20 insertions, 149 deletions
diff --git a/lib/verb.cpp b/lib/verb.cpp index 23f7c92..1f45d53 100644 --- a/lib/verb.cpp +++ b/lib/verb.cpp | |||
@@ -2,6 +2,11 @@ | |||
2 | 2 | ||
3 | namespace verbly { | 3 | namespace verbly { |
4 | 4 | ||
5 | verb::verb() | ||
6 | { | ||
7 | |||
8 | } | ||
9 | |||
5 | verb::verb(const data& _data, int _id) : word(_data, _id) | 10 | verb::verb(const data& _data, int _id) : word(_data, _id) |
6 | { | 11 | { |
7 | 12 | ||
@@ -9,185 +14,51 @@ namespace verbly { | |||
9 | 14 | ||
10 | std::string verb::base_form() const | 15 | std::string verb::base_form() const |
11 | { | 16 | { |
17 | assert(_valid == true); | ||
18 | |||
12 | return _infinitive; | 19 | return _infinitive; |
13 | } | 20 | } |
14 | 21 | ||
15 | std::string verb::infinitive_form() const | 22 | std::string verb::infinitive_form() const |
16 | { | 23 | { |
24 | assert(_valid == true); | ||
25 | |||
17 | return _infinitive; | 26 | return _infinitive; |
18 | } | 27 | } |
19 | 28 | ||
20 | std::string verb::past_tense_form() const | 29 | std::string verb::past_tense_form() const |
21 | { | 30 | { |
31 | assert(_valid == true); | ||
32 | |||
22 | return _past_tense; | 33 | return _past_tense; |
23 | } | 34 | } |
24 | 35 | ||
25 | std::string verb::past_participle_form() const | 36 | std::string verb::past_participle_form() const |
26 | { | 37 | { |
38 | assert(_valid == true); | ||
39 | |||
27 | return _past_participle; | 40 | return _past_participle; |
28 | } | 41 | } |
29 | 42 | ||
30 | std::string verb::ing_form() const | 43 | std::string verb::ing_form() const |
31 | { | 44 | { |
45 | assert(_valid == true); | ||
46 | |||
32 | return _ing_form; | 47 | return _ing_form; |
33 | } | 48 | } |
34 | 49 | ||
35 | std::string verb::s_form() const | 50 | std::string verb::s_form() const |
36 | { | 51 | { |
37 | return _s_form; | 52 | assert(_valid == true); |
38 | } | ||
39 | |||
40 | verb_query::verb_query(const data& _data) : _data(_data) | ||
41 | { | ||
42 | |||
43 | } | ||
44 | |||
45 | verb_query& verb_query::limit(int _limit) | ||
46 | { | ||
47 | if ((_limit > 0) || (_limit == unlimited)) | ||
48 | { | ||
49 | this->_limit = _limit; | ||
50 | } | ||
51 | |||
52 | return *this; | ||
53 | } | ||
54 | |||
55 | verb_query& verb_query::random(bool _random) | ||
56 | { | ||
57 | this->_random = _random; | ||
58 | |||
59 | return *this; | ||
60 | } | ||
61 | |||
62 | verb_query& verb_query::except(const verb& _word) | ||
63 | { | ||
64 | _except.push_back(_word); | ||
65 | |||
66 | return *this; | ||
67 | } | ||
68 | |||
69 | verb_query& verb_query::rhymes_with(const word& _word) | ||
70 | { | ||
71 | for (auto rhyme : _word.rhyme_phonemes()) | ||
72 | { | ||
73 | _rhymes.push_back(rhyme); | ||
74 | } | ||
75 | |||
76 | if (dynamic_cast<const verb*>(&_word) != nullptr) | ||
77 | { | ||
78 | _except.push_back(dynamic_cast<const verb&>(_word)); | ||
79 | } | ||
80 | 53 | ||
81 | return *this; | 54 | return _s_form; |
82 | } | 55 | } |
83 | 56 | ||
84 | verb_query& verb_query::has_pronunciation(bool _has_prn) | 57 | frame_query verb::frames() const |
85 | { | 58 | { |
86 | this->_has_prn = _has_prn; | 59 | assert(_valid == true); |
87 | 60 | ||
88 | return *this; | 61 | return _data->frames().for_verb(*this); |
89 | } | 62 | } |
90 | |||
91 | std::list<verb> verb_query::run() const | ||
92 | { | ||
93 | std::stringstream construct; | ||
94 | construct << "SELECT verb_id, infinitive, past_tense, past_participle, ing_form, s_form FROM verbs"; | ||
95 | std::list<std::string> conditions; | ||
96 | |||
97 | if (_has_prn) | ||
98 | { | ||
99 | conditions.push_back("verb_id IN (SELECT verb_id FROM verb_pronunciations)"); | ||
100 | } | ||
101 | |||
102 | if (!_rhymes.empty()) | ||
103 | { | ||
104 | std::list<std::string> clauses(_rhymes.size(), "pronunciation LIKE @RHMPRN"); | ||
105 | std::string cond = "verb_id IN (SELECT verb_id FROM verb_pronunciations WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")"; | ||
106 | conditions.push_back(cond); | ||
107 | } | ||
108 | |||
109 | for (auto except : _except) | ||
110 | { | ||
111 | conditions.push_back("verb_id != @EXCID"); | ||
112 | } | ||
113 | 63 | ||
114 | if (!conditions.empty()) | ||
115 | { | ||
116 | construct << " WHERE "; | ||
117 | construct << verbly::implode(std::begin(conditions), std::end(conditions), " AND "); | ||
118 | } | ||
119 | |||
120 | if (_random) | ||
121 | { | ||
122 | construct << " ORDER BY RANDOM()"; | ||
123 | } | ||
124 | |||
125 | if (_limit != unlimited) | ||
126 | { | ||
127 | construct << " LIMIT " << _limit; | ||
128 | } | ||
129 | |||
130 | sqlite3_stmt* ppstmt; | ||
131 | std::string query = construct.str(); | ||
132 | if (sqlite3_prepare_v2(_data.ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK) | ||
133 | { | ||
134 | throw std::runtime_error(sqlite3_errmsg(_data.ppdb)); | ||
135 | } | ||
136 | |||
137 | if (!_rhymes.empty()) | ||
138 | { | ||
139 | int i = 0; | ||
140 | for (auto rhyme : _rhymes) | ||
141 | { | ||
142 | std::string rhymer = "%" + rhyme; | ||
143 | sqlite3_bind_text(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@RHMPRN"), rhymer.c_str(), rhymer.length(), SQLITE_STATIC); | ||
144 | |||
145 | i++; | ||
146 | } | ||
147 | } | ||
148 | |||
149 | for (auto except : _except) | ||
150 | { | ||
151 | sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@EXCID"), except._id); | ||
152 | } | ||
153 | |||
154 | std::list<verb> output; | ||
155 | while (sqlite3_step(ppstmt) == SQLITE_ROW) | ||
156 | { | ||
157 | verb tnc {_data, sqlite3_column_int(ppstmt, 0)}; | ||
158 | tnc._infinitive = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 1))); | ||
159 | tnc._past_tense = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 2))); | ||
160 | tnc._past_participle = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 3))); | ||
161 | tnc._ing_form = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 4))); | ||
162 | tnc._s_form = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 5))); | ||
163 | |||
164 | output.push_back(tnc); | ||
165 | } | ||
166 | |||
167 | sqlite3_finalize(ppstmt); | ||
168 | |||
169 | for (auto& verb : output) | ||
170 | { | ||
171 | query = "SELECT pronunciation FROM verb_pronunciations WHERE verb_id = ?"; | ||
172 | if (sqlite3_prepare_v2(_data.ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK) | ||
173 | { | ||
174 | throw std::runtime_error(sqlite3_errmsg(_data.ppdb)); | ||
175 | } | ||
176 | |||
177 | sqlite3_bind_int(ppstmt, 1, verb._id); | ||
178 | |||
179 | while (sqlite3_step(ppstmt) == SQLITE_ROW) | ||
180 | { | ||
181 | std::string pronunciation(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 0))); | ||
182 | auto phonemes = verbly::split<std::list<std::string>>(pronunciation, " "); | ||
183 | |||
184 | verb.pronunciations.push_back(phonemes); | ||
185 | } | ||
186 | |||
187 | sqlite3_finalize(ppstmt); | ||
188 | } | ||
189 | |||
190 | return output; | ||
191 | } | ||
192 | |||
193 | }; | 64 | }; |