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/adjective_query.cpp | 119 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) (limited to 'lib/adjective_query.cpp') diff --git a/lib/adjective_query.cpp b/lib/adjective_query.cpp index ec100e3..283fdca 100644 --- a/lib/adjective_query.cpp +++ b/lib/adjective_query.cpp @@ -53,6 +53,29 @@ namespace verbly { return *this; } + adjective_query& adjective_query::with_prefix(filter _f) + { + _f.clean(); + _with_prefix = _f; + + return *this; + } + + adjective_query& adjective_query::with_suffix(filter _f) + { + _f.clean(); + _with_suffix = _f; + + return *this; + } + + adjective_query& adjective_query::with_complexity(int _arg) + { + _with_complexity = _arg; + + return *this; + } + adjective_query& adjective_query::is_variant() { this->_is_variant = true; @@ -231,6 +254,85 @@ namespace verbly { case adjective::positioning::undefined: break; } + 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 "base_form LIKE @PREFIX"; + } else { + return "base_form 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 "base_form LIKE @SUFFIX"; + } else { + return "base_form 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_variant) { conditions.push_back("adjective_id IN (SELECT adjective_id FROM variation)"); @@ -691,6 +793,23 @@ namespace verbly { sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@EXCID"), except._id); } + 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 attribute : _variant_of.inorder_flatten()) { sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@ATTRID"), attribute._id); -- cgit 1.4.1