From 4c94e100e87a09284f0e0a5bc0df688672492a1e Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 27 Mar 2016 14:28:54 -0400 Subject: Added prefix/suffix search, and word complexity search for nouns, adjectives, and adverbs Word complexity refers to the number of words in a noun, adjective, or adverb. --- lib/noun_query.cpp | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) (limited to 'lib/noun_query.cpp') diff --git a/lib/noun_query.cpp b/lib/noun_query.cpp index 2c3f57c..83bb47d 100644 --- a/lib/noun_query.cpp +++ b/lib/noun_query.cpp @@ -60,6 +60,29 @@ namespace verbly { return *this; } + noun_query& noun_query::with_prefix(filter _f) + { + _f.clean(); + _with_prefix = _f; + + return *this; + } + + noun_query& noun_query::with_suffix(filter _f) + { + _f.clean(); + _with_suffix = _f; + + return *this; + } + + noun_query& noun_query::with_complexity(int _arg) + { + _with_complexity = _arg; + + return *this; + } + noun_query& noun_query::is_hypernym() { _is_hypernym = true; @@ -461,6 +484,85 @@ namespace verbly { conditions.push_back(cond); } + if (!_with_prefix.empty()) + { + std::function, bool)> recur = [&] (filter f, bool notlogic) -> std::string { + switch (f.get_type()) + { + case filter::type::singleton: + { + if (notlogic == f.get_notlogic()) + { + return "singular LIKE @PREFIX"; + } else { + return "singular NOT LIKE @PREFIX"; + } + } + + case filter::type::group: + { + bool truelogic = notlogic != f.get_notlogic(); + + std::list clauses; + std::transform(std::begin(f), std::end(f), std::back_inserter(clauses), [&] (filter f2) { + return recur(f2, truelogic); + }); + + if (truelogic == f.get_orlogic()) + { + return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " AND ") + ")"; + } else { + return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")"; + } + } + } + }; + + conditions.push_back(recur(_with_prefix, false)); + } + + if (!_with_suffix.empty()) + { + std::function, bool)> recur = [&] (filter f, bool notlogic) -> std::string { + switch (f.get_type()) + { + case filter::type::singleton: + { + if (notlogic == f.get_notlogic()) + { + return "singular LIKE @SUFFIX"; + } else { + return "singular NOT LIKE @SUFFIX"; + } + } + + case filter::type::group: + { + bool truelogic = notlogic != f.get_notlogic(); + + std::list clauses; + std::transform(std::begin(f), std::end(f), std::back_inserter(clauses), [&] (filter f2) { + return recur(f2, truelogic); + }); + + if (truelogic == f.get_orlogic()) + { + return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " AND ") + ")"; + } else { + return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")"; + } + } + } + }; + + conditions.push_back(recur(_with_suffix, false)); + } + + if (_with_complexity != unlimited) + { + conditions.push_back("complexity = @COMPLEX"); + } + if (_is_hypernym) { conditions.push_back("noun_id IN (SELECT hypernym_id FROM hypernymy)"); @@ -1610,6 +1712,23 @@ namespace verbly { sqlite3_bind_text(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@SFORM"), sform.c_str(), sform.size(), SQLITE_STATIC); } + for (auto prefix : _with_prefix.inorder_flatten()) + { + std::string pfat = prefix + "%"; + sqlite3_bind_text(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@PREFIX"), pfat.c_str(), pfat.length(), SQLITE_STATIC); + } + + for (auto suffix : _with_suffix.inorder_flatten()) + { + std::string pfat = "%" + suffix; + sqlite3_bind_text(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@SUFFIX"), pfat.c_str(), pfat.length(), SQLITE_STATIC); + } + + if (_with_complexity != unlimited) + { + sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@COMPLEX"), _with_complexity); + } + for (auto hyponym : _hypernym_of.inorder_flatten()) { sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@HYPO"), hyponym._id); -- cgit 1.4.1