summary refs log tree commit diff stats
path: root/lib/verb_query.cpp
blob: 654bc334cd913c20bef9c58d92d4d6ec6c5e491d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "verbly.h"

namespace verbly {
  
  verb_query::verb_query(const data& _data) : _data(_data)
  {

  }
  
  verb_query& verb_query::limit(int _limit)
  {
    if ((_limit > 0) || (_limit == unlimited))
    {
      this->_limit = _limit;
    }
  
    return *this;
  }

  verb_query& verb_query::random()
  {
    this->_random = true;
  
    return *this;
  }
  
  verb_query& verb_query::except(const verb& _word)
  {
    _except.push_back(_word);
    
    return *this;
  }
  
  verb_query& verb_query::rhymes_with(const word& _word)
  {
    for (auto rhyme : _word.get_rhymes())
    {
      _rhymes.push_back(rhyme);
    }
    
    if (dynamic_cast<const verb*>(&_word) != nullptr)
    {
      _except.push_back(dynamic_cast<const verb&>(_word));
    }
    
    return *this;
  }
  
  verb_query& verb_query::has_pronunciation()
  {
    this->_has_prn = true;
    
    return *this;
  }
  
  verb_query& verb_query::has_rhyming_noun()
  {
    _has_rhyming_noun = true;
    
    return *this;
  }
  
  verb_query& verb_query::has_rhyming_adjective()
  {
    _has_rhyming_adjective = true;
    
    return *this;
  }
  
  verb_query& verb_query::has_rhyming_adverb()
  {
    _has_rhyming_adverb = true;
    
    return *this;
  }
  
  verb_query& verb_query::has_rhyming_verb()
  {
    _has_rhyming_verb = true;
    
    return *this;
  }
  
  verb_query& verb_query::has_frames()
  {
    this->_has_frames = true;
    
    return *this;
  }
  
  std::list<verb> verb_query::run() const
  {
    std::stringstream construct;
    construct << "SELECT verb_id, infinitive, past_tense, past_participle, ing_form, s_form FROM verbs";
    std::list<std::string> conditions;
    std::list<binding> bindings;
    
    if (_has_prn)
    {
      conditions.push_back("verb_id IN (SELECT verb_id FROM verb_pronunciations)");
    }
    
    if (!_rhymes.empty())
    {
      std::list<std::string> clauses(_rhymes.size(), "(prerhyme != ? AND rhyme = ?)");
      std::string cond = "verb_id IN (SELECT verb_id FROM verb_pronunciations WHERE " + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")";
      conditions.push_back(cond);
      
      for (auto rhy : _rhymes)
      {
        bindings.emplace_back(rhy.get_prerhyme());
        bindings.emplace_back(rhy.get_rhyme());
      }
    }
    
    if (_has_rhyming_noun)
    {
      conditions.push_back("verb_id IN (SELECT a.verb_id FROM verbs AS a INNER JOIN verb_pronunciations AS curp ON curp.noun_id = a.adverb_id INNER JOIN noun_pronunciations AS rhmp ON rhmp.prerhyme != curp.prerhyme AND rhmp.rhyme = curp.rhyme)");
    }
    
    if (_has_rhyming_adjective)
    {
      conditions.push_back("verb_id IN (SELECT a.verb_id FROM verbs AS a INNER JOIN verb_pronunciations AS curp ON curp.noun_id = a.adverb_id INNER JOIN adjective_pronunciations AS rhmp ON rhmp.prerhyme != curp.prerhyme AND rhmp.rhyme = curp.rhyme)");
    }
    
    if (_has_rhyming_adverb)
    {
      conditions.push_back("verb_id IN (SELECT a.verb_id FROM verbs AS a INNER JOIN verb_pronunciations AS curp ON curp.noun_id = a.adverb_id INNER JOIN adverb_pronunciations AS rhmp ON rhmp.prerhyme != curp.prerhyme AND rhmp.rhyme = curp.rhyme)");
    }
    
    if (_has_rhyming_verb)
    {
      conditions.push_back("verb_id IN (SELECT a.verb_id FROM verbs AS a INNER JOIN verb_pronunciations AS curp ON curp.noun_id = a.adverb_id INNER JOIN verb_pronunciations AS rhmp ON rhmp.prerhyme != curp.prerhyme AND rhmp.rhyme = curp.rhyme AND rhmp.verb_id != curp.verb_id)");
    }
    
    for (auto except : _except)
    {
      conditions.push_back("verb_id != ?");
      bindings.emplace_back(except._id);
    }
    
    if (!_has_frames)
    {
      conditions.push_back("verb_id IN (SELECT verb_id FROM verb_groups)");
    }
    
    if (!conditions.empty())
    {
      construct << " WHERE ";
      construct << verbly::implode(std::begin(conditions), std::end(conditions), " AND ");
    }
    
    if (_random)
    {
      construct << " ORDER BY RANDOM()";
    }
    
    if (_limit != unlimited)
    {
      construct << " LIMIT " << _limit;
    }
    
    sqlite3_stmt* ppstmt;
    std::string query = construct.str();
    if (sqlite3_prepare_v2(_data.ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK)
    {
      throw std::runtime_error(sqlite3_errmsg(_data.ppdb));
    }
    
    int i = 1;
    for (auto& binding : bindings)
    {
      switch (binding.get_type())
      {
        case binding::type::integer:
        {
          sqlite3_bind_int(ppstmt, i, binding.get_integer());
          
          break;
        }
        
        case binding::type::string:
        {
          sqlite3_bind_text(ppstmt, i, binding.get_string().c_str(), binding.get_string().length(), SQLITE_TRANSIENT);
          
          break;
        }
      }
      
      i++;
    }
    
    std::list<verb> output;
    while (sqlite3_step(ppstmt) == SQLITE_ROW)
    {
      verb tnc {_data, sqlite3_column_int(ppstmt, 0)};
      tnc._infinitive = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 1)));
      tnc._past_tense = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 2)));
      tnc._past_participle = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 3)));
      tnc._ing_form = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 4)));
      tnc._s_form = std::string(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 5)));
      
      output.push_back(tnc);
    }
    
    sqlite3_finalize(ppstmt);
    
    for (auto& verb : output)
    {
      query = "SELECT pronunciation, prerhyme, rhyme FROM verb_pronunciations WHERE verb_id = ?";
      if (sqlite3_prepare_v2(_data.ppdb, query.c_str(), query.length(), &ppstmt, NULL) != SQLITE_OK)
      {
        throw std::runtime_error(sqlite3_errmsg(_data.ppdb));
      }
      
      sqlite3_bind_int(ppstmt, 1, verb._id);
      
      while (sqlite3_step(ppstmt) == SQLITE_ROW)
      {
        std::string pronunciation(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 0)));
        auto phonemes = verbly::split<std::list<std::string>>(pronunciation, " ");
        
        verb.pronunciations.push_back(phonemes);
        
        if ((sqlite3_column_type(ppstmt, 1) != SQLITE_NULL) && (sqlite3_column_type(ppstmt, 2) != SQLITE_NULL))
        {
          std::string prerhyme(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 1)));
          std::string rhyming(reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 2)));
          verb.rhymes.emplace_back(prerhyme, rhyming);
        }
      }
      
      sqlite3_finalize(ppstmt);
    }
    
    return output;
  }

};