from model.example_clusters import ExampleClusters class MultiwordExample: def __init__(self, example_xml): self.other_attributes = {} self.cluster = -1 self.type = None def import_xml(self, example_xml): for oth_attr in ["lexical_unit_id", "structure_id", "structureName", "audio", "frequency", "logDice"]: if example_xml.hasAttribute(oth_attr): self.other_attributes[oth_attr] = example_xml.getAttribute(oth_attr) self.cluster = MultiwordExample._determine_cluster_number(example_xml) if example_xml.hasAttribute("type"): self.type = example_xml.getAttribute("type") @staticmethod def _determine_cluster_number(example_xml): if not example_xml.hasAttribute("cluster"): return ExampleClusters.first_empty_cluster() else: cluster = int(example_xml.getAttribute("cluster")) ExampleClusters.register_index(cluster) return cluster def export(self, doc, _modified): result = doc.createElement("multiwordExample") for key, value in self.other_attributes.items(): result.setAttribute(key, value) result.setAttribute("cluster", str(self.cluster)) if self.type is not None: result.setAttribute("type", self.type) return result def get_cluster(self): return self.cluster def get_structure(self): if "structureName" in self.other_attributes: return self.other_attributes["structureName"] else: return None def view(self, components): result = "" for comp in components: result += comp.text if not comp.no_space: result += " " return result.strip()