Now better parsing/exporting of examples, not only comps but also text elements.
This commit is contained in:
parent
1e90c666a0
commit
98cce79b79
|
@ -28,18 +28,26 @@ class Example(Editable):
|
||||||
|
|
||||||
def __init__(self, example_xml):
|
def __init__(self, example_xml):
|
||||||
self.translations = from_container_list(example_xml.querySelectorAll("translationContainer"))
|
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")
|
inner_xml = example_xml.querySelector("corpusExample")
|
||||||
if ce_xml is not None:
|
if inner_xml is not None:
|
||||||
self.inner = CorpusExample(example_xml)
|
self.inner = CorpusExample(inner_xml)
|
||||||
else:
|
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):
|
def export(self, doc):
|
||||||
result = doc.createElement("exampleContainer")
|
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
|
return result
|
||||||
|
|
||||||
def text(self):
|
def text(self):
|
||||||
|
@ -56,18 +64,13 @@ class Example(Editable):
|
||||||
|
|
||||||
class CorpusExample:
|
class CorpusExample:
|
||||||
def __init__(self, example_xml):
|
def __init__(self, example_xml):
|
||||||
super().__init__(example_xml)
|
|
||||||
xml = example_xml.querySelector("corpusExample")
|
|
||||||
|
|
||||||
self.other_attributes = {}
|
self.other_attributes = {}
|
||||||
for oth_attr in ["example_id", "modified", "lexical_unit_id", "audio"]:
|
for oth_attr in ["example_id", "modified", "lexical_unit_id", "audio"]:
|
||||||
if xml.hasAttribute(oth_attr):
|
if example_xml.hasAttribute(oth_attr):
|
||||||
self.other_attributes[oth_attr] = xml.getAttribute(oth_attr)
|
self.other_attributes[oth_attr] = example_xml.getAttribute(oth_attr)
|
||||||
|
|
||||||
def export(self, doc):
|
def export(self, doc):
|
||||||
result = doc.createElement("corpusExample")
|
result = doc.createElement("corpusExample")
|
||||||
for comp in self.components:
|
|
||||||
result.appendChild(comp.export(doc))
|
|
||||||
for key, value in self.other_attributes.items():
|
for key, value in self.other_attributes.items():
|
||||||
result.setAttribute(key, value)
|
result.setAttribute(key, value)
|
||||||
return result
|
return result
|
||||||
|
@ -80,26 +83,20 @@ class CorpusExample:
|
||||||
|
|
||||||
class MultiwordExample:
|
class MultiwordExample:
|
||||||
def __init__(self, example_xml):
|
def __init__(self, example_xml):
|
||||||
super().__init__(example_xml)
|
|
||||||
xml = example_xml.querySelector("multiwordExample")
|
|
||||||
|
|
||||||
self.other_attributes = {}
|
self.other_attributes = {}
|
||||||
for oth_attr in ["type", "lexical_unit_id", "structure_id", "structureName", "audio", "frequency", "logDice"]:
|
for oth_attr in ["type", "lexical_unit_id", "structure_id", "structureName", "audio", "frequency", "logDice"]:
|
||||||
if xml.hasAttribute(oth_attr):
|
if example_xml.hasAttribute(oth_attr):
|
||||||
self.other_attributes[oth_attr] = xml.getAttribute(oth_attr)
|
self.other_attributes[oth_attr] = example_xml.getAttribute(oth_attr)
|
||||||
|
|
||||||
self.cluster_valid = False
|
self.cluster_valid = False
|
||||||
self.cluster = -1
|
self.cluster = -1
|
||||||
if xml.hasAttribute("cluster"):
|
if example_xml.hasAttribute("cluster"):
|
||||||
self.cluster_valid = True
|
self.cluster_valid = True
|
||||||
self.cluster = int(xml.getAttribute("cluster"))
|
self.cluster = int(example_xml.getAttribute("cluster"))
|
||||||
|
|
||||||
def export(self, doc):
|
def export(self, doc):
|
||||||
result = doc.createElement("multiwordExample")
|
result = doc.createElement("multiwordExample")
|
||||||
|
|
||||||
for comp in self.components:
|
|
||||||
result.appendChild(comp.export(doc))
|
|
||||||
|
|
||||||
for key, value in self.other_attributes.items():
|
for key, value in self.other_attributes.items():
|
||||||
result.setAttribute(key, value)
|
result.setAttribute(key, value)
|
||||||
|
|
||||||
|
@ -117,15 +114,23 @@ class MultiwordExample:
|
||||||
|
|
||||||
class ComponentLexeme:
|
class ComponentLexeme:
|
||||||
def __init__(self, xml):
|
def __init__(self, xml):
|
||||||
|
self.other_attributes = {}
|
||||||
|
|
||||||
|
if xml.nodeName == "#text":
|
||||||
|
self.text = xml.data
|
||||||
|
self.role = None
|
||||||
|
else:
|
||||||
self.text = xml.textContent
|
self.text = xml.textContent
|
||||||
self.role = xml.getAttribute("role")
|
self.role = xml.getAttribute("role")
|
||||||
|
|
||||||
self.other_attributes = {}
|
|
||||||
for oth_attr in ["lexical_unit_lexeme_id", "slolex", "kol"]:
|
for oth_attr in ["lexical_unit_lexeme_id", "slolex", "kol"]:
|
||||||
if xml.hasAttribute(oth_attr):
|
if xml.hasAttribute(oth_attr):
|
||||||
self.other_attributes[oth_attr] = xml.getAttribute(oth_attr)
|
self.other_attributes[oth_attr] = xml.getAttribute(oth_attr)
|
||||||
|
|
||||||
def export(self, doc):
|
def export(self, doc):
|
||||||
|
if self.role is None:
|
||||||
|
return doc.createTextNode(self.text)
|
||||||
|
|
||||||
result = doc.createElement("comp")
|
result = doc.createElement("comp")
|
||||||
result.setAttribute("role", self.role)
|
result.setAttribute("role", self.role)
|
||||||
result.textContent = self.text
|
result.textContent = self.text
|
||||||
|
|
Loading…
Reference in New Issue
Block a user