From 4cc3c2cdc6d11d47ceb6293c6a285f07fbb21c6a Mon Sep 17 00:00:00 2001 From: Ozbolt Menegatti Date: Thu, 11 Jun 2020 19:52:28 +0200 Subject: [PATCH] much cleaner way to handle "last component in multiword example does not have a space" --- src/model/example/component_lexeme.py | 12 ++++++++---- src/model/example/example.py | 15 ++++++++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/model/example/component_lexeme.py b/src/model/example/component_lexeme.py index 7f4343a..5757316 100644 --- a/src/model/example/component_lexeme.py +++ b/src/model/example/component_lexeme.py @@ -1,6 +1,11 @@ from model.data import Data + class ComponentLexeme(Data): + # special value for last component that evaluates to True by default but is not + # exported to true + LAST_COMPONENT_SPACE = "last component does not have space" + def __init__(self): self.other_attributes = {} self.text = "" @@ -25,7 +30,7 @@ class ComponentLexeme(Data): def isValid(self): return len(self.text) > 0 - def export(self, doc, is_multiword, last_component): + def export(self, doc): if self.role is None: return doc.createTextNode(self.text) @@ -33,9 +38,8 @@ class ComponentLexeme(Data): result.setAttribute("role", self.role) result.textContent = self.text - # no space is only allowed in in multiword examples that are not last component - if self.no_space is True and is_multiword and not last_component: - result.setAttribute("space", "false") + if self.no_space and self.no_space != self.LAST_COMPONENT_SPACE: + result.setAttribute("space", "false") for key, value in self.other_attributes.items(): result.setAttribute(key, value) diff --git a/src/model/example/example.py b/src/model/example/example.py index 453fb5d..1ef67da 100644 --- a/src/model/example/example.py +++ b/src/model/example/example.py @@ -17,6 +17,12 @@ class Example(Data): self.components = [] self.edited = False + # removes space from last component if multiword example + def check_multiword_components(self): + if self.is_multiword(): + self.components[len(self.components) - 1].no_space = ComponentLexeme.LAST_COMPONENT_SPACE + + def import_xml(self, example_xml): self.translations = from_container_list(example_xml.querySelectorAll("translationContainer")) @@ -38,14 +44,17 @@ class Example(Data): if comp.isValid(): self.components.append(comp) + self.check_multiword_components() + def export(self, doc): + self.check_multiword_components() + result = doc.createElement("exampleContainer") inner = self.inner.export(doc, self.edited) # TODO: bad quick fix - for idx, comp in enumerate(self.components): - inner.appendChild(comp.export( - doc, self.is_multiword(), idx == len(self.components) - 1)) + for comp in self.components: + inner.appendChild(comp.export(doc)) result.appendChild(inner) return result