274 lines
9.1 KiB
Python
274 lines
9.1 KiB
Python
from message.simple_messages import ClickMessage, DataChgClickMessage, NoAction
|
|
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
|
|
|
|
from model.example.example import Example
|
|
from model.example.corpus_example import CorpusExample
|
|
from model.example.multiword_example import MultiwordExample
|
|
from model.example.component_lexeme import ComponentLexeme
|
|
from model.example_clusters import ExampleClusters
|
|
|
|
|
|
class SkeExample:
|
|
COLOR_CLASS = {
|
|
None: "no-gf2-info",
|
|
True: "goof-gf2-info",
|
|
False: "bad-gf2-info"
|
|
}
|
|
|
|
def __init__(self):
|
|
self.left = ""
|
|
self.right = ""
|
|
self.mid = ""
|
|
self.s_id = ""
|
|
self.gf2_good = None
|
|
self.gf2_check = False
|
|
|
|
@staticmethod
|
|
def fromLine(line):
|
|
result = SkeExample()
|
|
|
|
for left_el in line.Left:
|
|
result.left += left_el.str
|
|
|
|
for right_el in line.Right:
|
|
result.right += right_el.str
|
|
|
|
for mid_el in line.Kwic:
|
|
result.mid += mid_el.str
|
|
|
|
for ref in line.Refs:
|
|
if ref.startswith("s.id"):
|
|
result.s_id = ref.split("=")[1]
|
|
|
|
return result
|
|
|
|
|
|
def view(self):
|
|
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)])]
|
|
|
|
|
|
class SkeCollocation:
|
|
def __init__(self, data):
|
|
self.word = data.word
|
|
self.frequency = data.count
|
|
self.structure_name, self.structure_id = convert_structure(data.gramrel, data.lempos)
|
|
|
|
self.other = {"score": data.score, "cm": data.cm}
|
|
|
|
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)])]
|
|
|
|
|
|
def get_parser(ske_index_type):
|
|
if ske_index_type == 0:
|
|
return lambda data: [SkeExample.fromLine(line) for line in data.Lines]
|
|
elif ske_index_type == 1:
|
|
return lambda data: [SkeCollocation(item) for item in data.Items]
|
|
else:
|
|
raise "unknown ske index type"
|
|
|
|
|
|
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)
|
|
|
|
|
|
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:
|
|
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)
|
|
ske_lookup = model.ske.url_for_kind_index(ske_index)
|
|
|
|
next_message = msg(SkeModal, page_num, search_term, ske_index)
|
|
|
|
# could be none if empty
|
|
data = self.get_arg(3)
|
|
|
|
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)}
|
|
|
|
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
|
|
|
|
model.ske.request(search_term, next_message, ske_lookup, params)
|
|
|
|
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)
|
|
|
|
model.modal_set(lambda: modals.ske_list(
|
|
search_term, data, page_num, model.entry.senses, model.ske.request_kinds))
|
|
|
|
def reset(self):
|
|
return False
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
data_dict[gf_sid].gf2_check = True
|
|
|
|
# changed data_dict, now we can redraw!
|
|
# just let it do its thing in update_model
|
|
|
|
|
|
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))
|
|
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))
|
|
|
|
else:
|
|
console.log("You really should not be here, my lady")
|
|
continue
|
|
|
|
model.reset()
|
|
|
|
def _as_corpus_example(self, example):
|
|
new_example = Example()
|
|
new_example.inner = CorpusExample()
|
|
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
|
|
|
|
new_collocation.components.extend([lex_left, lex_mid, lex_right])
|
|
return new_collocation
|