From eea25b996854ce636f3b080fe540730eaf1e7330 Mon Sep 17 00:00:00 2001 From: matic_t Date: Tue, 29 Sep 2020 04:07:05 -0700 Subject: [PATCH] export multiword example as new entry --- src/export.py | 134 ++++++++++++++++++++++++++++++++++++ src/message/__init__.py | 2 +- src/message/example_edit.py | 17 ++++- src/model/sense.py | 1 - src/view/view.py | 9 ++- 5 files changed, 158 insertions(+), 5 deletions(-) diff --git a/src/export.py b/src/export.py index 94792f2..7ffdafc 100644 --- a/src/export.py +++ b/src/export.py @@ -190,3 +190,137 @@ def _export_label_list(doc, lst): label_el.setAttribute('type', key) result.appendChild(label_el) return result + +def _original_xml_query_selector(selector, entry, doc, parent_selector = selector.rsplit(' ', 1)[0]): + query = entry.original_xml.querySelector(selector) + if query is None: + query = doc.createElement(selector.rsplit(' ', 1)[1]) + entry.original_xml.querySelector(parent_selector).appendChild(query) + return query + + +def export_example_to_entry_xml(example): + parser = __new__(DOMParser()) + doc = parser.parseFromString("", "text/xml") + entry_xml = doc.firstChild + + head = doc.createElement("head") + entry_xml.appendChild(head) + + status = doc.createElement("status") + head.appendChild(status) + + headword = doc.createElement("headword") + head.appendChild(headword) + + lemma = doc.createElement("lemma") + lemma.textContent = " ".join(comp.text for comp in example.components) + lemma.setAttribute("type", "compound") + headword.appendChild(lemma) + + homonymy = doc.createElement("homonymy") + headword.appendChild(homonymy) + + lexical_unit = doc.createElement("lexicalUnit") + lexical_unit.setAttribute("type", "MWE") + head.appendChild(lexical_unit) + + if example.inner.other_attributes['structure_id'] != None: + lexical_unit.setAttribute("structure_id", example.inner.other_attributes['structure_id']) + + for comp in example.components: + comp_xml = doc.createElement("component") + lexeme = doc.createElement("lexeme") + lexeme.textContent = comp.text + comp_xml.appendChild(lexeme) + lexical_unit.appendChild(comp_xml) + + grammar = doc.createElement("grammar") + category = doc.createElement("category") + grammar.appendChild(category) + head.appendChild(grammar) + + variant_list = doc.createElement("variantList") + head.appendChild(variant_list) + related_entry_list = doc.createElement("relatedEntryList") + head.appendChild(related_entry_list) + label_list = doc.createElement("labelList") + head.appendChild(label_list) + comment = doc.createElement("comment") + head.appendChild(comment) + + body = doc.createElement("body") + entry_xml.appendChild(body) + + sense_list = doc.createElement("senseList") + body.appendChild(sense_list) + + sense = doc.createElement("sense") + sense_list.appendChild(sense) + + sense_label_list = doc.createElement("labelList") + sense.appendChild(sense_label_list) + + first_translation = example.translations[0][0] + translation_label_list = doc.createElement("labelList") + + # Add labels to sense if label value isn't kontrastivno or približek else keep them in translation + for key, value in first_translation.tags: + key, value = export_tag(key, value) + label_el = doc.createElement("label") + label_list = translation_label_list if value == "kontrastivno" or value == "približek" else sense_label_list + # if idx >= 0: + # label_el = first_translation.original_xml.querySelectorAll("labelList label")[idx].cloneNode(True) + + label_el.textContent = value + label_el.setAttribute('type', key) + + label_list.appendChild(label_el) + + # Set definition as explanation if explanation in slo + definition_list = doc.createElement("definitionList") + sense.appendChild(definition_list) + for explanation in first_translation.explanationList: + if explanation.language == "slo": + definition = doc.createElement("definition") + definition.setAttribute("type", "indicator") + definition.textContent = explanation.value + definition_list.appendChild(definition) + first_translation.explanationList.remove(explanation) + + translation_container_list = doc.createElement("translationContainerList") + sense.appendChild(translation_container_list) + + translation_container = doc.createElement("translationContainer") + translation_container_list.appendChild(translation_container) + + if len(translation_label_list) > 0: + translation_container.appendChild(translation_label_list) + + # translation = first_translation.original_xml.querySelector("translation").cloneNode(True) if first_translation.original_xml != None else doc.createElement("translation") + translation = doc.createElement("translation") + translation_container.appendChild(translation) + + translation.textContent = first_translation.translation + translation.setAttribute("targetLang", first_translation.targetLang) + + if first_translation.audio: + translation.setAttribute("audio", first_translation.audio) + + if first_translation.source: + translation.setAttribute("source", first_translation.source) + + if len(first_translation.explanationList) > 0 : + _export_explanation_list(doc, first_translation.explanationList, translation) + + + example.translations[0] = example.translations[0][1:] + export_translation_list(doc, example, translation_container_list) + + return doc + # for comp in example.components: + # if comp.role == "collocate": + # self.headword = comp.role + # break + + # self.headword = example. \ No newline at end of file diff --git a/src/message/__init__.py b/src/message/__init__.py index 0e18322..81bafe6 100644 --- a/src/message/__init__.py +++ b/src/message/__init__.py @@ -4,7 +4,7 @@ from message.show_messages import ShowEntryLabelsEdit, ShowEditTranslation, Show from message.simple_edits import EditSenseLabel, EditSenseDefinition, EditComment, AddSenseLabel, AddSense, AddExampleTranslation, DoChosenExamples, AddToLabelList, AddToGenericList, EditVariants, EditHomonymy, EditRelatedEntries, EditEntryLabels, ExampleClusterEdit, ExampleClusterAdd from message.show_menu import ShowTranslationMenu, ShowSenseMenu, ShowExampleMenu from message.sense_edit import SenseMoveUp, SenseMoveDown, SenseBin, AddMultiwordExample -from message.example_edit import ExampleMoveUp, ExampleMoveDown, ExampleBin, ExampleRoleChange, ExampleComponentSpace, ExampleComponentAdd, ExampleComponentRemove, EditExampleText, ToggleExamples, ToggleClusters +from message.example_edit import ExampleAsNewEntry, ExampleMoveUp, ExampleMoveDown, ExampleBin, ExampleRoleChange, ExampleComponentSpace, ExampleComponentAdd, ExampleComponentRemove, EditExampleText, ToggleExamples, ToggleClusters from message.delete_messages import DeleteComment, DeleteVariants, DeleteHomonymy, DeleteRelatedEntries, DeleteEntryLabels from message.ske_messages import ShowSkeModal, SearchInSkeModal, SkeInsert diff --git a/src/message/example_edit.py b/src/message/example_edit.py index 7b06fc0..03bee6c 100644 --- a/src/message/example_edit.py +++ b/src/message/example_edit.py @@ -3,6 +3,10 @@ from message.simple_messages import DataChgClickMessage, ClickMessage, NoReset from message.message import Message from model.example import Example, ComponentLexeme from model.sense import Sense +from model.entry import Entry +from model.model import Model +import lib.screenful as screenful +from export import export_to_xml, export_example_to_entry_xml @@ -13,6 +17,17 @@ def _get_example_idx(example, model): return (sense, eidx) +class ExampleAsNewEntry(ClickMessage): + def update_model(self, model): + example = self.get_arg(0, Example) + entry_xml = export_example_to_entry_xml(example) + new_entry = Entry() + new_entry.import_xml(entry_xml) + + new_model = Model() + new_model.entry = new_entry + screenful.screenful().Editor['new'](None, export_to_xml(new_model)) + class ExampleMoveUp(DataChgClickMessage): def update_model(self, model): example = self.get_arg(0, Example) @@ -60,7 +75,7 @@ class EditExampleText(Message): if example.newly_created: example.newly_created = False sense.examples.append(example) - + idx = 0 for txt in document.getElementsByClassName("example-component-text"): example.components[idx].text = txt.value diff --git a/src/model/sense.py b/src/model/sense.py index 4e9e6cc..2566c7f 100644 --- a/src/model/sense.py +++ b/src/model/sense.py @@ -37,7 +37,6 @@ class Sense(Data): def merge_labels(self): return ", ".join(val for _, val in self.labels) - def view(self, model, sense_num): examples = [example.view(model, self) for example in self.examples] diff --git a/src/view/view.py b/src/view/view.py index de61a85..083901a 100644 --- a/src/view/view.py +++ b/src/view/view.py @@ -98,11 +98,16 @@ class View: elif type(menu_target) is Example: example = menu_target sense = example_sense(example, entry) - return h("span.popup-menu", { "style": style }, [ + dom_children = [ h("button.shyButton", { "on": {"click": msg(ShowExampleEdit, example, sense)}}, "✎"), h("button.shyButton", { "on": {"click": msg(ExampleMoveUp, example)}}, "↑"), h("button.shyButton", { "on": {"click": msg(ExampleMoveDown, example)}}, "↓"), - h("button.shyButton", { "on": {"click": msg(ExampleBin, example)}}, "🗑")]) + h("button.shyButton", { "on": {"click": msg(ExampleBin, example)}}, "🗑")] + + if example.is_multiword(): + dom_children.insert(1, h("button.shyButton", { "on": {"click": msg(ExampleAsNewEntry, example, sense)}}, "As new Entry")) + + return h("span.popup-menu", { "style": style }, dom_children) else: console.log("Should not be heree!!")