lexonomy_custom_editor/src/message/ske_messages.py

274 lines
9.1 KiB
Python
Raw Permalink Normal View History

2020-05-19 16:54:29 +00:00
from message.simple_messages import ClickMessage, DataChgClickMessage, NoAction
2020-03-30 18:34:49 +00:00
from message.message import msg, Message
from view import modals
from browser import window, document
from lib.snabbdom import h
from lib.preferences import get_preference
from lib.structure_conversions import convert_structure
2020-03-30 18:34:49 +00:00
from model.example.example import Example
from model.example.corpus_example import CorpusExample
from model.example.multiword_example import MultiwordExample
2020-03-30 18:34:49 +00:00
from model.example.component_lexeme import ComponentLexeme
from model.example_clusters import ExampleClusters
2020-03-30 18:34:49 +00:00
class SkeExample:
2020-05-19 16:54:29 +00:00
COLOR_CLASS = {
None: "no-gf2-info",
True: "goof-gf2-info",
False: "bad-gf2-info"
}
def __init__(self):
2020-03-30 18:34:49 +00:00
self.left = ""
self.right = ""
self.mid = ""
self.s_id = ""
2020-05-19 16:54:29 +00:00
self.gf2_good = None
2020-08-04 07:48:17 +00:00
self.gf2_check = False
2020-05-19 16:54:29 +00:00
@staticmethod
def fromLine(line):
result = SkeExample()
2020-03-30 18:34:49 +00:00
for left_el in line.Left:
2020-05-19 16:54:29 +00:00
result.left += left_el.str
2020-03-30 18:34:49 +00:00
for right_el in line.Right:
2020-05-19 16:54:29 +00:00
result.right += right_el.str
2020-03-30 18:34:49 +00:00
for mid_el in line.Kwic:
2020-05-19 16:54:29 +00:00
result.mid += mid_el.str
2020-03-30 18:34:49 +00:00
for ref in line.Refs:
if ref.startswith("s.id"):
2020-05-19 16:54:29 +00:00
result.s_id = ref.split("=")[1]
2020-05-19 16:54:29 +00:00
return result
2020-03-30 18:34:49 +00:00
def view(self):
2020-05-19 16:54:29 +00:00
return [h("span.{}".format(self.COLOR_CLASS[self.gf2_good]), {}, [
h("span", {}, self.left + " "),
h("span", {"style": {"font-weight": "bold"}}, self.mid),
h("span", {}, self.right)])]
2020-03-30 18:34:49 +00:00
class SkeCollocation:
def __init__(self, data):
self.word = data.word
self.frequency = data.count
2020-08-04 07:48:17 +00:00
self.structure_name, self.structure_id = convert_structure(data.gramrel, data.lempos)
self.other = {"score": data.score, "cm": data.cm}
2020-03-30 18:34:49 +00:00
def view(self):
return [h("span.example-text", {}, [
h("span.example-text-2", {}, self.word),
h("span.example-frequency", {}, self.frequency),
h("span.example-logdice", {}, self.other["score"]),
h("span", {}, "; "),
h("span.grey1", {}, self.other["cm"]),
h("span", {}, ","),
h("span.grey2", {}, self.structure_name)])]
2020-08-04 07:48:17 +00:00
2020-03-30 18:34:49 +00:00
def get_parser(ske_index_type):
if ske_index_type == 0:
2020-05-19 16:54:29 +00:00
return lambda data: [SkeExample.fromLine(line) for line in data.Lines]
2020-03-30 18:34:49 +00:00
elif ske_index_type == 1:
return lambda data: [SkeCollocation(item) for item in data.Items]
else:
raise "unknown ske index type"
2020-05-19 16:54:29 +00:00
def match_gf2_examples(data, *args):
if type(data) is not list:
return
url = "https://gf2examples.cjvt.si/get_examples"
xhr = __new__(XMLHttpRequest())
xhr.open("POST", url, True)
request_dict = {}
for example in data:
request_dict[example.s_id] = example.mid
xhr.setRequestHeader("Content-type", "application/json");
xhr.addEventListener("load", msg(SkeModalGf2Update, *args, data))
to_send = window.JSON.stringify(request_dict)
xhr.send(to_send)
2020-03-30 18:34:49 +00:00
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:
2020-03-30 18:34:49 +00:00
self.add_arg(None)
super().on_event(event)
def update_model(self, model):
page_num = self.get_arg(0, int)
search_term = self.get_arg(1, str)
ske_index = self.get_arg(2, int)
2020-03-30 18:34:49 +00:00
ske_lookup = model.ske.url_for_kind_index(ske_index)
2020-05-19 16:54:29 +00:00
next_message = msg(SkeModal, page_num, search_term, ske_index)
2020-03-30 18:34:49 +00:00
# could be none if empty
data = self.get_arg(3)
2020-05-19 16:54:29 +00:00
2020-03-30 18:34:49 +00:00
if data is None:
params = {"additional_refs": "s.id,p.id",
"page_num": page_num,
2020-05-19 16:54:29 +00:00
"error_callback": next_message,
"data_parser": get_parser(ske_index)}
gdex = get_preference("ske_gdex")
if gdex:
if not len(gdex) == 2:
window.console.log("Strange gdex setting, should be a list of length 2: [gdexconf, gdexcnt]")
return
params["gdex"] = gdex
2020-05-19 16:54:29 +00:00
model.ske.request(search_term, next_message, ske_lookup, params)
2020-03-30 18:34:49 +00:00
2020-05-19 16:54:29 +00:00
elif type(data) is list:
window.console.log(data)
# check if gf2 examples are loaded or not
2020-08-04 07:48:17 +00:00
if not data[0].gf2_check and type(data[0]) is SkeExample:
2020-05-19 16:54:29 +00:00
# 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)
model.modal_set(lambda: modals.ske_list(
search_term, data, page_num, model.entry.senses, model.ske.request_kinds))
2020-03-30 18:34:49 +00:00
def reset(self):
return False
2020-05-19 16:54:29 +00:00
class SkeModalGf2Update(SkeModal):
def on_event(self, event):
response_data = window.JSON.parse(event.target.response)
data = self.get_arg(3)
data_dict = {}
for example in data:
example.gf_good = False
data_dict[example.s_id] = example
2020-08-04 07:48:17 +00:00
bad_response = dict(response_data["bad"])
for gf_sid, gf_data in bad_response.items():
data_dict[gf_sid].gf2_good = None
data_dict[gf_sid].gf2_check = True
2020-05-19 16:54:29 +00:00
good_response = dict(response_data["good"])
for gf_sid, gf_data in good_response.items():
data_dict[gf_sid].left = gf_data.left
data_dict[gf_sid].mid = gf_data.mid
data_dict[gf_sid].right = gf_data.right
data_dict[gf_sid].gf2_good = True
2020-08-04 07:48:17 +00:00
data_dict[gf_sid].gf2_check = True
2020-05-19 16:54:29 +00:00
# changed data_dict, now we can redraw!
# just let it do its thing in update_model
2020-03-30 18:34:49 +00:00
class ShowSkeModal(SkeModal):
def on_event(self, event):
# if just showing first time, than show empty results and let user click search
self.add_arg("")
super().on_event(event)
class SearchInSkeModal(SkeModal):
def on_event(self, event):
self.add_arg(int(document.getElementById("ske-page-num").value))
2020-03-30 18:34:49 +00:00
self.add_arg(document.getElementById("ske-search").value)
self.add_arg(document.getElementById("ske-select").selectedIndex)
super().on_event(event)
class SkeInsert(DataChgClickMessage):
def update_model(self, model):
data = self.get_arg(0)
if type(data) is not list:
return
if len(data) == 0:
return
sense_num = document.getElementById("ske-sense-select").selectedIndex
sense = model.entry.senses[sense_num]
elements = document.getElementsByClassName("ske-line-check")
assert(len(elements) == len(data))
for ex, element in zip(data, elements):
if not element.checked:
continue
elif isinstance(ex, SkeExample):
sense.examples.append(self._as_corpus_example(ex))
elif isinstance(ex, SkeCollocation):
sense.examples.append(self._as_multiword_example(ex))
2020-03-30 18:34:49 +00:00
else:
console.log("You really should not be here, my lady")
continue
2020-08-04 07:48:17 +00:00
model.reset()
def _as_corpus_example(self, example):
new_example = Example()
new_example.inner = CorpusExample()
2020-08-04 08:05:36 +00:00
new_example.inner.other_attributes["example_id"] = example.s_id
new_example.inner.cluster = ExampleClusters.first_empty_cluster()
lex_left = ComponentLexeme()
lex_left.text = example["left"]
lex_left.role = None
lex_mid = ComponentLexeme()
lex_mid.text = example["mid"]
lex_mid.role = "collocation"
lex_right = ComponentLexeme()
lex_right.text = example["right"]
lex_right.role = None
new_example.components.extend([lex_left, lex_mid, lex_right])
return new_example
def _as_multiword_example(self, example):
new_collocation = Example()
new_collocation.inner = MultiwordExample()
new_collocation.inner.other_attributes["structure_id"] = example.structure_id
new_collocation.inner.other_attributes["structureName"] = example.structure_name
new_collocation.inner.other_attributes["logDice"] = example.other["score"]
new_collocation.inner.other_attributes["frequency"] = example.frequency
new_collocation.inner.type = "collocation"
lex_left = ComponentLexeme()
lex_left.text = ""
lex_left.role = None
lex_mid = ComponentLexeme()
lex_mid.text = example.word
lex_mid.role = "collocation"
lex_right = ComponentLexeme()
lex_right.text = ""
lex_right.role = None
2020-03-30 18:34:49 +00:00
new_collocation.components.extend([lex_left, lex_mid, lex_right])
return new_collocation