Now better parsing/exporting of examples, not only comps but also text elements.

This commit is contained in:
Ozbolt Menegatti 2020-01-25 12:30:16 +01:00
parent 1e90c666a0
commit 98cce79b79

View File

@ -28,18 +28,26 @@ class Example(Editable):
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")]
ce_xml = example_xml.querySelector("corpusExample")
if ce_xml is not None:
self.inner = CorpusExample(example_xml)
inner_xml = example_xml.querySelector("corpusExample")
if inner_xml is not None:
self.inner = CorpusExample(inner_xml)
else:
self.inner = MultiwordExample(example_xml)
inner_xml = example_xml.querySelector("multiwordExample")
self.inner = MultiwordExample(inner_xml)
self.components = [ComponentLexeme(el) for el in inner_xml.childNodes]
def export(self, doc):
result = doc.createElement("exampleContainer")
result.appendChild(self.inner.export(doc))
inner = self.inner.export(doc)
# TODO: bad quick fix
for comp in self.components:
inner.appendChild(comp.export(doc))
result.appendChild(inner)
return result
def text(self):
@ -56,18 +64,13 @@ class Example(Editable):
class CorpusExample:
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)
if example_xml.hasAttribute(oth_attr):
self.other_attributes[oth_attr] = example_xml.getAttribute(oth_attr)
def export(self, doc):
result = doc.createElement("corpusExample")
for comp in self.components:
result.appendChild(comp.export(doc))
for key, value in self.other_attributes.items():
result.setAttribute(key, value)
return result
@ -80,26 +83,20 @@ class CorpusExample:
class MultiwordExample:
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)
if example_xml.hasAttribute(oth_attr):
self.other_attributes[oth_attr] = example_xml.getAttribute(oth_attr)
self.cluster_valid = False
self.cluster = -1
if xml.hasAttribute("cluster"):
if example_xml.hasAttribute("cluster"):
self.cluster_valid = True
self.cluster = int(xml.getAttribute("cluster"))
self.cluster = int(example_xml.getAttribute("cluster"))
def export(self, doc):
result = doc.createElement("multiwordExample")
for comp in self.components:
result.appendChild(comp.export(doc))
for key, value in self.other_attributes.items():
result.setAttribute(key, value)
@ -117,15 +114,23 @@ class MultiwordExample:
class ComponentLexeme:
def __init__(self, xml):
self.other_attributes = {}
if xml.nodeName == "#text":
self.text = xml.data
self.role = None
else:
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)
def export(self, doc):
if self.role is None:
return doc.createTextNode(self.text)
result = doc.createElement("comp")
result.setAttribute("role", self.role)
result.textContent = self.text