From 460a854dad35eff98e8d3e032513bd4a1df0f9ea Mon Sep 17 00:00:00 2001 From: Ozbolt Menegatti Date: Fri, 24 Jan 2020 01:46:58 +0100 Subject: [PATCH] Actually reading examples, not just quick hack :) --- src/model/example.py | 96 +++++++++++++++++++++++++++++++++++++++----- src/view/modals.py | 2 +- src/view/view.py | 2 +- 3 files changed, 88 insertions(+), 12 deletions(-) diff --git a/src/model/example.py b/src/model/example.py index 8a3c0af..99c84aa 100644 --- a/src/model/example.py +++ b/src/model/example.py @@ -1,19 +1,95 @@ from model.editable import Editable from model.translation import from_container_list + class Example(Editable): - def __init__(self, example_xml): - # example = example_xml.querySelector("example") - # self.example = example.textContent if example else "" - + @staticmethod + def from_xml(example_xml): ce_xml = example_xml.querySelector("corpusExample") if ce_xml is not None: - self.example_type = "corpus" - inner_xml = ce_xml + return CorpusExample(example_xml) else: - self.example_type = "multiword" - inner_xml = example_xml.querySelector("multiwordExample") + return MultiwordExample(example_xml) + + + @staticmethod + def add_clusters(entry): + nocluster_examples = [] + taken_clusters = [] + + # gahter all taken cluster numbers and all examples without clusters + for sense in entry.senses: + for example in sense.examples: + cluster = example.get_cluster() + if cluster == -1: + nocluster_examples.append(example) + elif cluster is not None: + taken_clusters.append(cluster) + + cnum = 1 + for example in nocluster_examples: + while cnum in taken_clusters: + cnum += 1 + taken_clusters.append(cnum) + + example.cluster = cnum - self.original_xml = inner_xml - self.text = inner_xml.textContent + + def __init__(self, example_xml): self.translations = from_container_list(example_xml.querySelectorAll("translationContainer")) + self.components = [ComponentLexeme(el) for el in example_xml.querySelectorAll("comp")] + + + def get_cluster(self): + return None + + def get_valid_cluster(self): + return None + + def text(self): + return " ".join([comp.text for comp in self.components]) + + +class CorpusExample(Example): + def __init__(self, example_xml): + super().__init__(example_xml) + xml = example_xml.querySelector("corpusExample") + + self.other_attributes = {} + for oth_attr in ["example_id", "modified", "lexical_unit_id", "audio"]: + if xml.hasAttribute(oth_attr): + self.other_attributes[oth_attr] = xml.getAttribute(oth_attr) + + +class MultiwordExample(Example): + def __init__(self, example_xml): + super().__init__(example_xml) + xml = example_xml.querySelector("multiwordExample") + + self.other_attributes = {} + for oth_attr in ["type", "lexical_unit_id", "structure_id", "structureName", "audio", "frequency", "logDice"]: + if xml.hasAttribute(oth_attr): + self.other_attributes[oth_attr] = xml.getAttribute(oth_attr) + + self.cluster_valid = False + self.cluster = -1 + if xml.hasAttribute("cluster"): + self.cluster_valid = True + self.cluster = int(xml.getAttribute("cluster")) + + def get_cluster(self): + return self.cluster + + def get_valid_cluster(self): + return self.cluster if self.cluster_valid else None + + +class ComponentLexeme: + def __init__(self, xml): + self.text = xml.textContent + self.role = xml.getAttribute("role") + + self.other_attributes = {} + for oth_attr in ["lexical_unit_lexeme_id", "slolex", "kol"]: + if xml.hasAttribute(oth_attr): + self.other_attributes[oth_attr] = xml.getAttribute(oth_attr) diff --git a/src/view/modals.py b/src/view/modals.py index daca52c..065531f 100644 --- a/src/view/modals.py +++ b/src/view/modals.py @@ -37,7 +37,7 @@ def edit_sense_label(sense): def edit_example(example): - content = question("Edit example", example.text) + content = question("Edit example", example.text()) return modal_template(content, "Edit Example", message.EditExampleTranslation(example)) diff --git a/src/view/view.py b/src/view/view.py index d025f45..bffff39 100644 --- a/src/view/view.py +++ b/src/view/view.py @@ -108,7 +108,7 @@ class View: return h("div.example", {}, [ h("div.example-dot", {}, "▣"), h(example_tag, {}, [ - h("span.example-text", {"on": {"click": msg(ShowExampleMenu(example))} }, example.text), + h("span.example-text", {"on": {"click": msg(ShowExampleMenu(example))} }, example.text()), h("div.example-translation-list", {}, [ h("div.example-translation", {}, [ h("span.example-arrow", {}, "↪"),