From 300f90822c6eb43fcc4c5664fa7a98dc0755b899 Mon Sep 17 00:00:00 2001 From: LukeItUp Date: Tue, 15 Sep 2020 08:38:44 +0200 Subject: [PATCH] cql query option --- src/message/ske_messages.py | 38 ++++++++++++++++++++++++++++++------- src/view/modals.py | 4 ++++ src/view/view.py | 2 +- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/message/ske_messages.py b/src/message/ske_messages.py index 9ef6b8e..feaf144 100644 --- a/src/message/ske_messages.py +++ b/src/message/ske_messages.py @@ -103,13 +103,28 @@ def match_gf2_examples(data, *args): xhr.send(to_send) +def make_cql_query(ske_index, search_term, pos): + cql_pos= { + "samostalnik": ("S.*", "-s"), + "glagol": ("G.*", "-g"), + "pridevnik": ("P.*", "-p"), + "prislov": ("R.*", "-r"), + "zaimek": ("Z.*", "-z") + } + + if ske_index == 0: + return "[ lemma=\"{0}\" & tag=\"{1}\" ]".format(search_term, cql_pos[pos][0]) + else: + return search_term + cql_pos[pos][1] + + class SkeModal(ClickMessage): def on_event(self, event): # event could be data if this is the return from external library if type(event) in [list, int]: self.add_arg(event) else: - if len(self._args) < 4: + if len(self._args) < 5: self.add_arg(None) super().on_event(event) @@ -117,18 +132,20 @@ class SkeModal(ClickMessage): page_num = self.get_arg(0, int) search_term = self.get_arg(1, str) ske_index = self.get_arg(2, int) + ske_pos_query = self.get_arg(3, str) ske_lookup = model.ske.url_for_kind_index(ske_index) - next_message = msg(SkeModal, page_num, search_term, ske_index) + next_message = msg(SkeModal, page_num, search_term, ske_index, ske_pos_query) # could be none if empty - data = self.get_arg(3) + data = self.get_arg(4) if data is None: params = {"additional_refs": "s.id,p.id", "page_num": page_num, "error_callback": next_message, - "data_parser": get_parser(ske_index)} + "data_parser": get_parser(ske_index), + "querytype": ske_pos_query} gdex = get_preference("ske_gdex") if gdex: @@ -137,14 +154,20 @@ class SkeModal(ClickMessage): return params["gdex"] = gdex + # enable CQL query + if ske_pos_query is not "simple": + search_term_old = search_term + search_term = make_cql_query(ske_index, search_term, ske_pos_query) + model.ske.request(search_term, next_message, ske_lookup, params) - + search_term = search_term_old + elif type(data) is list: window.console.log(data) # check if gf2 examples are loaded or not if not data[0].gf2_check and type(data[0]) is SkeExample: # we get the data, we have to match it with available data on our gf2 examples API - match_gf2_examples(data, page_num, search_term, ske_index) + match_gf2_examples(data, page_num, search_term, ske_index, ske_pos_query) elif type(data[0]) is SkeCollocation: # filtering, grouping and sorting data data.sort(key= lambda x: float(x.other["score"]), reverse=True) @@ -171,7 +194,7 @@ class SkeModal(ClickMessage): class SkeModalGf2Update(SkeModal): def on_event(self, event): response_data = window.JSON.parse(event.target.response) - data = self.get_arg(3) + data = self.get_arg(4) data_dict = {} for example in data: @@ -207,6 +230,7 @@ class SearchInSkeModal(SkeModal): self.add_arg(int(document.getElementById("ske-page-num").value)) self.add_arg(document.getElementById("ske-search").value) self.add_arg(document.getElementById("ske-select").selectedIndex) + self.add_arg(document.getElementById("ske-pos-query").value) super().on_event(event) diff --git a/src/view/modals.py b/src/view/modals.py index 7828785..b8cccc6 100644 --- a/src/view/modals.py +++ b/src/view/modals.py @@ -181,6 +181,10 @@ def ske_list(search_term, data, page_num, senses, ske_kinds): "type": "number", "min": 1, "step": 1}}, "")]), + h("label.fourth.ske-mid-input", {}, [ + h("select#ske-pos-query", {}, [h("option", {}, "{}".format(pos)) for pos in ["simple", "samostalnik", "glagol", "pridevnik", "prislov", "zaimek"]]) + ]), + h("span.fourth.button.ske-right-button", {"on": {"click": message.msg(message.SearchInSkeModal)}}, "Isci")]), h("div.ske-list", diff --git a/src/view/view.py b/src/view/view.py index 9488091..de61a85 100644 --- a/src/view/view.py +++ b/src/view/view.py @@ -58,7 +58,7 @@ class View: def view_ske_button(model): return h( "span#ske-button.button.toggle", - { "on": {"click": msg(ShowSkeModal, 1, model.entry.headword, 0)} }, + { "on": {"click": msg(ShowSkeModal, 1, model.entry.headword, 0, "simple")} }, h("svg#ske-img", { "attrs": { "xmlns": "http://www.w3.org/2000/svg",