diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2016-03-26 09:40:13 -0400 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2016-03-26 09:40:13 -0400 |
commit | 6b7c77f3a28d9a44afacb76e3db58a5ff5f59f4d (patch) | |
tree | 8e52076e662a57e06401ad95936998fee7afbe8b /lib/noun_query.cpp | |
parent | eef5de613c75661e5d94baa086f6f2ddc26c7ed0 (diff) | |
download | verbly-6b7c77f3a28d9a44afacb76e3db58a5ff5f59f4d.tar.gz verbly-6b7c77f3a28d9a44afacb76e3db58a5ff5f59f4d.tar.bz2 verbly-6b7c77f3a28d9a44afacb76e3db58a5ff5f59f4d.zip |
Added full hierarchy search for meronymy
Diffstat (limited to 'lib/noun_query.cpp')
-rw-r--r-- | lib/noun_query.cpp | 302 |
1 files changed, 301 insertions, 1 deletions
diff --git a/lib/noun_query.cpp b/lib/noun_query.cpp index cb11577..2c3f57c 100644 --- a/lib/noun_query.cpp +++ b/lib/noun_query.cpp | |||
@@ -121,6 +121,14 @@ namespace verbly { | |||
121 | return *this; | 121 | return *this; |
122 | } | 122 | } |
123 | 123 | ||
124 | noun_query& noun_query::full_part_meronym_of(filter<noun> _f) | ||
125 | { | ||
126 | _f.clean(); | ||
127 | _full_part_meronym_of = _f; | ||
128 | |||
129 | return *this; | ||
130 | } | ||
131 | |||
124 | noun_query& noun_query::is_part_holonym() | 132 | noun_query& noun_query::is_part_holonym() |
125 | { | 133 | { |
126 | _is_part_holonym = true; | 134 | _is_part_holonym = true; |
@@ -136,6 +144,14 @@ namespace verbly { | |||
136 | return *this; | 144 | return *this; |
137 | } | 145 | } |
138 | 146 | ||
147 | noun_query& noun_query::full_part_holonym_of(filter<noun> _f) | ||
148 | { | ||
149 | _f.clean(); | ||
150 | _full_part_holonym_of = _f; | ||
151 | |||
152 | return *this; | ||
153 | } | ||
154 | |||
139 | noun_query& noun_query::is_substance_meronym() | 155 | noun_query& noun_query::is_substance_meronym() |
140 | { | 156 | { |
141 | _is_substance_meronym = true; | 157 | _is_substance_meronym = true; |
@@ -151,6 +167,14 @@ namespace verbly { | |||
151 | return *this; | 167 | return *this; |
152 | } | 168 | } |
153 | 169 | ||
170 | noun_query& noun_query::full_substance_meronym_of(filter<noun> _f) | ||
171 | { | ||
172 | _f.clean(); | ||
173 | _full_substance_meronym_of = _f; | ||
174 | |||
175 | return *this; | ||
176 | } | ||
177 | |||
154 | noun_query& noun_query::is_substance_holonym() | 178 | noun_query& noun_query::is_substance_holonym() |
155 | { | 179 | { |
156 | _is_substance_holonym = true; | 180 | _is_substance_holonym = true; |
@@ -166,6 +190,14 @@ namespace verbly { | |||
166 | return *this; | 190 | return *this; |
167 | } | 191 | } |
168 | 192 | ||
193 | noun_query& noun_query::full_substance_holonym_of(filter<noun> _f) | ||
194 | { | ||
195 | _f.clean(); | ||
196 | _full_substance_holonym_of = _f; | ||
197 | |||
198 | return *this; | ||
199 | } | ||
200 | |||
169 | noun_query& noun_query::is_member_meronym() | 201 | noun_query& noun_query::is_member_meronym() |
170 | { | 202 | { |
171 | _is_member_meronym = true; | 203 | _is_member_meronym = true; |
@@ -181,6 +213,14 @@ namespace verbly { | |||
181 | return *this; | 213 | return *this; |
182 | } | 214 | } |
183 | 215 | ||
216 | noun_query& noun_query::full_member_meronym_of(filter<noun> _f) | ||
217 | { | ||
218 | _f.clean(); | ||
219 | _full_member_meronym_of = _f; | ||
220 | |||
221 | return *this; | ||
222 | } | ||
223 | |||
184 | noun_query& noun_query::is_member_holonym() | 224 | noun_query& noun_query::is_member_holonym() |
185 | { | 225 | { |
186 | _is_member_holonym = true; | 226 | _is_member_holonym = true; |
@@ -196,6 +236,14 @@ namespace verbly { | |||
196 | return *this; | 236 | return *this; |
197 | } | 237 | } |
198 | 238 | ||
239 | noun_query& noun_query::full_member_holonym_of(filter<noun> _f) | ||
240 | { | ||
241 | _f.clean(); | ||
242 | _full_member_holonym_of = _f; | ||
243 | |||
244 | return *this; | ||
245 | } | ||
246 | |||
199 | noun_query& noun_query::is_proper() | 247 | noun_query& noun_query::is_proper() |
200 | { | 248 | { |
201 | _is_proper = true; | 249 | _is_proper = true; |
@@ -336,7 +384,7 @@ namespace verbly { | |||
336 | { | 384 | { |
337 | std::stringstream construct; | 385 | std::stringstream construct; |
338 | 386 | ||
339 | if (!_full_hypernym_of.empty() || !_full_hyponym_of.empty()) | 387 | if (!_full_hypernym_of.empty() || !_full_hyponym_of.empty() || !_full_part_meronym_of.empty() || !_full_part_holonym_of.empty() || !_full_substance_meronym_of.empty() || !_full_substance_holonym_of.empty() || !_full_member_meronym_of.empty() || !_full_member_holonym_of.empty()) |
340 | { | 388 | { |
341 | construct << "WITH RECURSIVE "; | 389 | construct << "WITH RECURSIVE "; |
342 | 390 | ||
@@ -352,6 +400,36 @@ namespace verbly { | |||
352 | ctes.push_back("hyponym_tree_" + std::to_string(hypernym._id) + " AS (SELECT hyponym_id FROM hypernymy WHERE hypernym_id = " + std::to_string(hypernym._id) + " UNION SELECT h.hyponym_id FROM hyponym_tree_" + std::to_string(hypernym._id) + " AS t INNER JOIN hypernymy AS h ON t.hyponym_id = h.hypernym_id)"); | 400 | ctes.push_back("hyponym_tree_" + std::to_string(hypernym._id) + " AS (SELECT hyponym_id FROM hypernymy WHERE hypernym_id = " + std::to_string(hypernym._id) + " UNION SELECT h.hyponym_id FROM hyponym_tree_" + std::to_string(hypernym._id) + " AS t INNER JOIN hypernymy AS h ON t.hyponym_id = h.hypernym_id)"); |
353 | } | 401 | } |
354 | 402 | ||
403 | for (auto holonym : _full_part_meronym_of.uniq_flatten()) | ||
404 | { | ||
405 | ctes.push_back("part_meronym_tree_" + std::to_string(holonym._id) + " AS (SELECT meronym_id FROM part_meronymy WHERE holonym_id = " + std::to_string(holonym._id) + " UNION SELECT h.meronym_id FROM part_meronym_tree_" + std::to_string(holonym._id) + " AS t INNER JOIN part_meronymy AS h ON t.meronym_id = h.holonym_id)"); | ||
406 | } | ||
407 | |||
408 | for (auto meronym : _full_part_holonym_of.uniq_flatten()) | ||
409 | { | ||
410 | ctes.push_back("part_holonym_tree_" + std::to_string(meronym._id) + " AS (SELECT holonym_id FROM part_meronymy WHERE meronym_id = " + std::to_string(meronym._id) + " UNION SELECT h.holonym_id FROM part_holonym_tree_" + std::to_string(meronym._id) + " AS t INNER JOIN part_meronymy AS h ON t.holonym_id = h.meronym_id)"); | ||
411 | } | ||
412 | |||
413 | for (auto holonym : _full_substance_meronym_of.uniq_flatten()) | ||
414 | { | ||
415 | ctes.push_back("substance_meronym_tree_" + std::to_string(holonym._id) + " AS (SELECT meronym_id FROM substance_meronymy WHERE holonym_id = " + std::to_string(holonym._id) + " UNION SELECT h.meronym_id FROM substance_meronym_tree_" + std::to_string(holonym._id) + " AS t INNER JOIN substance_meronymy AS h ON t.meronym_id = h.holonym_id)"); | ||
416 | } | ||
417 | |||
418 | for (auto meronym : _full_substance_holonym_of.uniq_flatten()) | ||
419 | { | ||
420 | ctes.push_back("substance_holonym_tree_" + std::to_string(meronym._id) + " AS (SELECT holonym_id FROM substance_meronymy WHERE meronym_id = " + std::to_string(meronym._id) + " UNION SELECT h.holonym_id FROM substance_holonym_tree_" + std::to_string(meronym._id) + " AS t INNER JOIN substance_meronymy AS h ON t.holonym_id = h.meronym_id)"); | ||
421 | } | ||
422 | |||
423 | for (auto holonym : _full_member_meronym_of.uniq_flatten()) | ||
424 | { | ||
425 | ctes.push_back("member_meronym_tree_" + std::to_string(holonym._id) + " AS (SELECT meronym_id FROM member_meronymy WHERE holonym_id = " + std::to_string(holonym._id) + " UNION SELECT h.meronym_id FROM member_meronym_tree_" + std::to_string(holonym._id) + " AS t INNER JOIN member_meronymy AS h ON t.meronym_id = h.holonym_id)"); | ||
426 | } | ||
427 | |||
428 | for (auto meronym : _full_member_holonym_of.uniq_flatten()) | ||
429 | { | ||
430 | ctes.push_back("member_holonym_tree_" + std::to_string(meronym._id) + " AS (SELECT holonym_id FROM member_meronymy WHERE meronym_id = " + std::to_string(meronym._id) + " UNION SELECT h.holonym_id FROM member_holonym_tree_" + std::to_string(meronym._id) + " AS t INNER JOIN member_meronymy AS h ON t.holonym_id = h.meronym_id)"); | ||
431 | } | ||
432 | |||
355 | construct << verbly::implode(std::begin(ctes), std::end(ctes), ", "); | 433 | construct << verbly::implode(std::begin(ctes), std::end(ctes), ", "); |
356 | construct << " "; | 434 | construct << " "; |
357 | } | 435 | } |
@@ -619,6 +697,43 @@ namespace verbly { | |||
619 | conditions.push_back(cond.str()); | 697 | conditions.push_back(cond.str()); |
620 | } | 698 | } |
621 | 699 | ||
700 | if (!_full_part_meronym_of.empty()) | ||
701 | { | ||
702 | std::function<std::string (filter<noun>, bool)> recur = [&] (filter<noun> f, bool notlogic) -> std::string { | ||
703 | switch (f.get_type()) | ||
704 | { | ||
705 | case filter<noun>::type::singleton: | ||
706 | { | ||
707 | if (notlogic == f.get_notlogic()) | ||
708 | { | ||
709 | return "noun_id IN (SELECT meronym_id FROM part_meronym_tree_" + std::to_string(f.get_elem()._id) + ")"; | ||
710 | } else { | ||
711 | return "noun_id NOT IN (SELECT meronym_id FROM part_meronym_tree_" + std::to_string(f.get_elem()._id) + ")"; | ||
712 | } | ||
713 | } | ||
714 | |||
715 | case filter<noun>::type::group: | ||
716 | { | ||
717 | bool truelogic = notlogic != f.get_notlogic(); | ||
718 | |||
719 | std::list<std::string> clauses; | ||
720 | std::transform(std::begin(f), std::end(f), std::back_inserter(clauses), [&] (filter<noun> f2) { | ||
721 | return recur(f2, truelogic); | ||
722 | }); | ||
723 | |||
724 | if (truelogic == f.get_orlogic()) | ||
725 | { | ||
726 | return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " AND ") + ")"; | ||
727 | } else { | ||
728 | return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")"; | ||
729 | } | ||
730 | } | ||
731 | } | ||
732 | }; | ||
733 | |||
734 | conditions.push_back(recur(_full_part_meronym_of, false)); | ||
735 | } | ||
736 | |||
622 | if (_is_part_holonym) | 737 | if (_is_part_holonym) |
623 | { | 738 | { |
624 | conditions.push_back("noun_id IN (SELECT holonym_id FROM part_meronymy)"); | 739 | conditions.push_back("noun_id IN (SELECT holonym_id FROM part_meronymy)"); |
@@ -673,6 +788,43 @@ namespace verbly { | |||
673 | conditions.push_back(cond.str()); | 788 | conditions.push_back(cond.str()); |
674 | } | 789 | } |
675 | 790 | ||
791 | if (!_full_part_holonym_of.empty()) | ||
792 | { | ||
793 | std::function<std::string (filter<noun>, bool)> recur = [&] (filter<noun> f, bool notlogic) -> std::string { | ||
794 | switch (f.get_type()) | ||
795 | { | ||
796 | case filter<noun>::type::singleton: | ||
797 | { | ||
798 | if (notlogic == f.get_notlogic()) | ||
799 | { | ||
800 | return "noun_id IN (SELECT holonym_id FROM part_holonym_tree_" + std::to_string(f.get_elem()._id) + ")"; | ||
801 | } else { | ||
802 | return "noun_id NOT IN (SELECT holonym_id FROM part_holonym_tree_" + std::to_string(f.get_elem()._id) + ")"; | ||
803 | } | ||
804 | } | ||
805 | |||
806 | case filter<noun>::type::group: | ||
807 | { | ||
808 | bool truelogic = notlogic != f.get_notlogic(); | ||
809 | |||
810 | std::list<std::string> clauses; | ||
811 | std::transform(std::begin(f), std::end(f), std::back_inserter(clauses), [&] (filter<noun> f2) { | ||
812 | return recur(f2, truelogic); | ||
813 | }); | ||
814 | |||
815 | if (truelogic == f.get_orlogic()) | ||
816 | { | ||
817 | return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " AND ") + ")"; | ||
818 | } else { | ||
819 | return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")"; | ||
820 | } | ||
821 | } | ||
822 | } | ||
823 | }; | ||
824 | |||
825 | conditions.push_back(recur(_full_part_holonym_of, false)); | ||
826 | } | ||
827 | |||
676 | if (_is_substance_meronym) | 828 | if (_is_substance_meronym) |
677 | { | 829 | { |
678 | conditions.push_back("noun_id IN (SELECT meronym_id FROM substance_meronymy)"); | 830 | conditions.push_back("noun_id IN (SELECT meronym_id FROM substance_meronymy)"); |
@@ -727,6 +879,43 @@ namespace verbly { | |||
727 | conditions.push_back(cond.str()); | 879 | conditions.push_back(cond.str()); |
728 | } | 880 | } |
729 | 881 | ||
882 | if (!_full_substance_meronym_of.empty()) | ||
883 | { | ||
884 | std::function<std::string (filter<noun>, bool)> recur = [&] (filter<noun> f, bool notlogic) -> std::string { | ||
885 | switch (f.get_type()) | ||
886 | { | ||
887 | case filter<noun>::type::singleton: | ||
888 | { | ||
889 | if (notlogic == f.get_notlogic()) | ||
890 | { | ||
891 | return "noun_id IN (SELECT meronym_id FROM substance_meronym_tree_" + std::to_string(f.get_elem()._id) + ")"; | ||
892 | } else { | ||
893 | return "noun_id NOT IN (SELECT meronym_id FROM substance_meronym_tree_" + std::to_string(f.get_elem()._id) + ")"; | ||
894 | } | ||
895 | } | ||
896 | |||
897 | case filter<noun>::type::group: | ||
898 | { | ||
899 | bool truelogic = notlogic != f.get_notlogic(); | ||
900 | |||
901 | std::list<std::string> clauses; | ||
902 | std::transform(std::begin(f), std::end(f), std::back_inserter(clauses), [&] (filter<noun> f2) { | ||
903 | return recur(f2, truelogic); | ||
904 | }); | ||
905 | |||
906 | if (truelogic == f.get_orlogic()) | ||
907 | { | ||
908 | return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " AND ") + ")"; | ||
909 | } else { | ||
910 | return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")"; | ||
911 | } | ||
912 | } | ||
913 | } | ||
914 | }; | ||
915 | |||
916 | conditions.push_back(recur(_full_substance_meronym_of, false)); | ||
917 | } | ||
918 | |||
730 | if (_is_substance_holonym) | 919 | if (_is_substance_holonym) |
731 | { | 920 | { |
732 | conditions.push_back("noun_id IN (SELECT holonym_id FROM substance_meronymy)"); | 921 | conditions.push_back("noun_id IN (SELECT holonym_id FROM substance_meronymy)"); |
@@ -781,6 +970,43 @@ namespace verbly { | |||
781 | conditions.push_back(cond.str()); | 970 | conditions.push_back(cond.str()); |
782 | } | 971 | } |
783 | 972 | ||
973 | if (!_full_substance_holonym_of.empty()) | ||
974 | { | ||
975 | std::function<std::string (filter<noun>, bool)> recur = [&] (filter<noun> f, bool notlogic) -> std::string { | ||
976 | switch (f.get_type()) | ||
977 | { | ||
978 | case filter<noun>::type::singleton: | ||
979 | { | ||
980 | if (notlogic == f.get_notlogic()) | ||
981 | { | ||
982 | return "noun_id IN (SELECT holonym_id FROM substance_holonym_tree_" + std::to_string(f.get_elem()._id) + ")"; | ||
983 | } else { | ||
984 | return "noun_id NOT IN (SELECT holonym_id FROM substance_holonym_tree_" + std::to_string(f.get_elem()._id) + ")"; | ||
985 | } | ||
986 | } | ||
987 | |||
988 | case filter<noun>::type::group: | ||
989 | { | ||
990 | bool truelogic = notlogic != f.get_notlogic(); | ||
991 | |||
992 | std::list<std::string> clauses; | ||
993 | std::transform(std::begin(f), std::end(f), std::back_inserter(clauses), [&] (filter<noun> f2) { | ||
994 | return recur(f2, truelogic); | ||
995 | }); | ||
996 | |||
997 | if (truelogic == f.get_orlogic()) | ||
998 | { | ||
999 | return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " AND ") + ")"; | ||
1000 | } else { | ||
1001 | return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")"; | ||
1002 | } | ||
1003 | } | ||
1004 | } | ||
1005 | }; | ||
1006 | |||
1007 | conditions.push_back(recur(_full_substance_holonym_of, false)); | ||
1008 | } | ||
1009 | |||
784 | if (_is_member_meronym) | 1010 | if (_is_member_meronym) |
785 | { | 1011 | { |
786 | conditions.push_back("noun_id IN (SELECT meronym_id FROM member_meronymy)"); | 1012 | conditions.push_back("noun_id IN (SELECT meronym_id FROM member_meronymy)"); |
@@ -835,6 +1061,43 @@ namespace verbly { | |||
835 | conditions.push_back(cond.str()); | 1061 | conditions.push_back(cond.str()); |
836 | } | 1062 | } |
837 | 1063 | ||
1064 | if (!_full_member_meronym_of.empty()) | ||
1065 | { | ||
1066 | std::function<std::string (filter<noun>, bool)> recur = [&] (filter<noun> f, bool notlogic) -> std::string { | ||
1067 | switch (f.get_type()) | ||
1068 | { | ||
1069 | case filter<noun>::type::singleton: | ||
1070 | { | ||
1071 | if (notlogic == f.get_notlogic()) | ||
1072 | { | ||
1073 | return "noun_id IN (SELECT meronym_id FROM member_meronym_tree_" + std::to_string(f.get_elem()._id) + ")"; | ||
1074 | } else { | ||
1075 | return "noun_id NOT IN (SELECT meronym_id FROM member_meronym_tree_" + std::to_string(f.get_elem()._id) + ")"; | ||
1076 | } | ||
1077 | } | ||
1078 | |||
1079 | case filter<noun>::type::group: | ||
1080 | { | ||
1081 | bool truelogic = notlogic != f.get_notlogic(); | ||
1082 | |||
1083 | std::list<std::string> clauses; | ||
1084 | std::transform(std::begin(f), std::end(f), std::back_inserter(clauses), [&] (filter<noun> f2) { | ||
1085 | return recur(f2, truelogic); | ||
1086 | }); | ||
1087 | |||
1088 | if (truelogic == f.get_orlogic()) | ||
1089 | { | ||
1090 | return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " AND ") + ")"; | ||
1091 | } else { | ||
1092 | return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")"; | ||
1093 | } | ||
1094 | } | ||
1095 | } | ||
1096 | }; | ||
1097 | |||
1098 | conditions.push_back(recur(_full_member_meronym_of, false)); | ||
1099 | } | ||
1100 | |||
838 | if (_is_member_holonym) | 1101 | if (_is_member_holonym) |
839 | { | 1102 | { |
840 | conditions.push_back("noun_id IN (SELECT holonym_id FROM member_meronym)"); | 1103 | conditions.push_back("noun_id IN (SELECT holonym_id FROM member_meronym)"); |
@@ -889,6 +1152,43 @@ namespace verbly { | |||
889 | conditions.push_back(cond.str()); | 1152 | conditions.push_back(cond.str()); |
890 | } | 1153 | } |
891 | 1154 | ||
1155 | if (!_full_member_holonym_of.empty()) | ||
1156 | { | ||
1157 | std::function<std::string (filter<noun>, bool)> recur = [&] (filter<noun> f, bool notlogic) -> std::string { | ||
1158 | switch (f.get_type()) | ||
1159 | { | ||
1160 | case filter<noun>::type::singleton: | ||
1161 | { | ||
1162 | if (notlogic == f.get_notlogic()) | ||
1163 | { | ||
1164 | return "noun_id IN (SELECT holonym_id FROM member_holonym_tree_" + std::to_string(f.get_elem()._id) + ")"; | ||
1165 | } else { | ||
1166 | return "noun_id NOT IN (SELECT holonym_id FROM member_holonym_tree_" + std::to_string(f.get_elem()._id) + ")"; | ||
1167 | } | ||
1168 | } | ||
1169 | |||
1170 | case filter<noun>::type::group: | ||
1171 | { | ||
1172 | bool truelogic = notlogic != f.get_notlogic(); | ||
1173 | |||
1174 | std::list<std::string> clauses; | ||
1175 | std::transform(std::begin(f), std::end(f), std::back_inserter(clauses), [&] (filter<noun> f2) { | ||
1176 | return recur(f2, truelogic); | ||
1177 | }); | ||
1178 | |||
1179 | if (truelogic == f.get_orlogic()) | ||
1180 | { | ||
1181 | return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " AND ") + ")"; | ||
1182 | } else { | ||
1183 | return "(" + verbly::implode(std::begin(clauses), std::end(clauses), " OR ") + ")"; | ||
1184 | } | ||
1185 | } | ||
1186 | } | ||
1187 | }; | ||
1188 | |||
1189 | conditions.push_back(recur(_full_member_holonym_of, false)); | ||
1190 | } | ||
1191 | |||
892 | if (_is_proper) | 1192 | if (_is_proper) |
893 | { | 1193 | { |
894 | conditions.push_back("proper = 1"); | 1194 | conditions.push_back("proper = 1"); |