diff options
Diffstat (limited to 'lib/noun_query.cpp')
-rw-r--r-- | lib/noun_query.cpp | 119 |
1 files changed, 119 insertions, 0 deletions
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 { | |||
60 | return *this; | 60 | return *this; |
61 | } | 61 | } |
62 | 62 | ||
63 | noun_query& noun_query::with_prefix(filter<std::string> _f) | ||
64 | { | ||
65 | _f.clean(); | ||
66 | _with_prefix = _f; | ||
67 | |||
68 | return *this; | ||
69 | } | ||
70 | |||
71 | noun_query& noun_query::with_suffix(filter<std::string> _f) | ||
72 | { | ||
73 | _f.clean(); | ||
74 | _with_suffix = _f; | ||
75 | |||
76 | return *this; | ||
77 | } | ||
78 | |||
79 | noun_query& noun_query::with_complexity(int _arg) | ||
80 | { | ||
81 | _with_complexity = _arg; | ||
82 | |||
83 | return *this; | ||
84 | } | ||
85 | |||
63 | noun_query& noun_query::is_hypernym() | 86 | noun_query& noun_query::is_hypernym() |
64 | { | 87 | { |
65 | _is_hypernym = true; | 88 | _is_hypernym = true; |
@@ -461,6 +484,85 @@ namespace verbly { | |||
461 | conditions.push_back(cond); | 484 | conditions.push_back(cond); |
462 | } | 485 | } |
463 | 486 | ||
487 | if (!_with_prefix.empty()) | ||
488 | { | ||
489 | std::function<std::string (filter<std::string>, bool)> recur = [&] (filter<std::string> f, bool notlogic) -> std::string { | ||
490 | switch (f.get_type()) | ||
491 | { | ||
492 | case filter<std::string>::type::singleton: | ||
493 | { | ||
494 | if (notlogic == f.get_notlogic()) | ||
495 | { | ||
496 | return "singular LIKE @PREFIX"; | ||
497 | } else { | ||
498 | return "singular NOT LIKE @PREFIX"; | ||
499 | } | ||
500 | } | ||
501 | |||
502 | case filter<std::string>::type::group: | ||
503 | { | ||
504 | bool truelogic = notlogic != f.get_notlogic(); | ||
505 | |||
506 | std::list<std::string> clauses; | ||
507 | std::transform(std::begin(f), std::end(f), std::back_inserter(clauses), [&] (filter<std::string> f2) { | ||
508 | return recur(f2, truelogic); | ||
509 | }); | ||
510 | |||
511 | if (truelogic == f.get_orlogic()) | ||
512 | { | ||
513 | return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " AND ") + ")"; | ||
514 | } else { | ||
515 | return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")"; | ||
516 | } | ||
517 | } | ||
518 | } | ||
519 | }; | ||
520 | |||
521 | conditions.push_back(recur(_with_prefix, false)); | ||
522 | } | ||
523 | |||
524 | if (!_with_suffix.empty()) | ||
525 | { | ||
526 | std::function<std::string (filter<std::string>, bool)> recur = [&] (filter<std::string> f, bool notlogic) -> std::string { | ||
527 | switch (f.get_type()) | ||
528 | { | ||
529 | case filter<std::string>::type::singleton: | ||
530 | { | ||
531 | if (notlogic == f.get_notlogic()) | ||
532 | { | ||
533 | return "singular LIKE @SUFFIX"; | ||
534 | } else { | ||
535 | return "singular NOT LIKE @SUFFIX"; | ||
536 | } | ||
537 | } | ||
538 | |||
539 | case filter<std::string>::type::group: | ||
540 | { | ||
541 | bool truelogic = notlogic != f.get_notlogic(); | ||
542 | |||
543 | std::list<std::string> clauses; | ||
544 | std::transform(std::begin(f), std::end(f), std::back_inserter(clauses), [&] (filter<std::string> f2) { | ||
545 | return recur(f2, truelogic); | ||
546 | }); | ||
547 | |||
548 | if (truelogic == f.get_orlogic()) | ||
549 | { | ||
550 | return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " AND ") + ")"; | ||
551 | } else { | ||
552 | return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")"; | ||
553 | } | ||
554 | } | ||
555 | } | ||
556 | }; | ||
557 | |||
558 | conditions.push_back(recur(_with_suffix, false)); | ||
559 | } | ||
560 | |||
561 | if (_with_complexity != unlimited) | ||
562 | { | ||
563 | conditions.push_back("complexity = @COMPLEX"); | ||
564 | } | ||
565 | |||
464 | if (_is_hypernym) | 566 | if (_is_hypernym) |
465 | { | 567 | { |
466 | conditions.push_back("noun_id IN (SELECT hypernym_id FROM hypernymy)"); | 568 | conditions.push_back("noun_id IN (SELECT hypernym_id FROM hypernymy)"); |
@@ -1610,6 +1712,23 @@ namespace verbly { | |||
1610 | sqlite3_bind_text(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@SFORM"), sform.c_str(), sform.size(), SQLITE_STATIC); | 1712 | sqlite3_bind_text(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@SFORM"), sform.c_str(), sform.size(), SQLITE_STATIC); |
1611 | } | 1713 | } |
1612 | 1714 | ||
1715 | for (auto prefix : _with_prefix.inorder_flatten()) | ||
1716 | { | ||
1717 | std::string pfat = prefix + "%"; | ||
1718 | sqlite3_bind_text(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@PREFIX"), pfat.c_str(), pfat.length(), SQLITE_STATIC); | ||
1719 | } | ||
1720 | |||
1721 | for (auto suffix : _with_suffix.inorder_flatten()) | ||
1722 | { | ||
1723 | std::string pfat = "%" + suffix; | ||
1724 | sqlite3_bind_text(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@SUFFIX"), pfat.c_str(), pfat.length(), SQLITE_STATIC); | ||
1725 | } | ||
1726 | |||
1727 | if (_with_complexity != unlimited) | ||
1728 | { | ||
1729 | sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@COMPLEX"), _with_complexity); | ||
1730 | } | ||
1731 | |||
1613 | for (auto hyponym : _hypernym_of.inorder_flatten()) | 1732 | for (auto hyponym : _hypernym_of.inorder_flatten()) |
1614 | { | 1733 | { |
1615 | sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@HYPO"), hyponym._id); | 1734 | sqlite3_bind_int(ppstmt, sqlite3_bind_parameter_index(ppstmt, "@HYPO"), hyponym._id); |