Refactoring example into multiple files
This commit is contained in:
parent
8c2197e78b
commit
b6cb2dbce5
|
@ -1,178 +0,0 @@
|
|||
from model.data import Data as Editable
|
||||
from model.translation import from_container_list
|
||||
from model.example_clusters import ExampleClusters
|
||||
from lib.snabbdom import h
|
||||
|
||||
|
||||
class Example(Editable):
|
||||
def __init__(self, example_xml):
|
||||
self.translations = from_container_list(example_xml.querySelectorAll("translationContainer"))
|
||||
|
||||
inner_xml = example_xml.querySelector("corpusExample")
|
||||
if inner_xml is not None:
|
||||
self.inner = CorpusExample(inner_xml)
|
||||
else:
|
||||
inner_xml = example_xml.querySelector("multiwordExample")
|
||||
self.inner = MultiwordExample(inner_xml)
|
||||
|
||||
all_components = [ComponentLexeme(el) for el in inner_xml.childNodes]
|
||||
self.components = [comp for comp in all_components if comp.isValid()]
|
||||
|
||||
|
||||
def export(self, doc):
|
||||
result = doc.createElement("exampleContainer")
|
||||
|
||||
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 view(self):
|
||||
return self.inner.view(self.components)
|
||||
|
||||
def get_cluster(self):
|
||||
return self.inner.get_cluster()
|
||||
|
||||
def set_cluster(self, cluster):
|
||||
self.inner.cluster = cluster
|
||||
|
||||
def get_structure(self):
|
||||
return self.inner.get_structure()
|
||||
|
||||
def is_collocation(self):
|
||||
return self.get_view_type() == 2
|
||||
|
||||
def is_multiword(self):
|
||||
return self.get_view_type() != 1
|
||||
|
||||
def get_view_type(self):
|
||||
# as per the bosses, these are the rules for different colors
|
||||
if type(self.inner) is CorpusExample:
|
||||
return 1
|
||||
elif self.inner.type == "collocation":
|
||||
return 2
|
||||
else:
|
||||
return 3
|
||||
|
||||
def get_other_attributes(self):
|
||||
return self.inner.other_attributes
|
||||
|
||||
|
||||
class CorpusExample:
|
||||
def __init__(self, example_xml):
|
||||
self.other_attributes = {}
|
||||
for oth_attr in ["example_id", "modified", "lexical_unit_id", "audio"]:
|
||||
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 key, value in self.other_attributes.items():
|
||||
result.setAttribute(key, value)
|
||||
return result
|
||||
|
||||
def get_cluster(self):
|
||||
return None
|
||||
|
||||
def get_structure(self):
|
||||
return None
|
||||
|
||||
def view(self, components):
|
||||
return [h("span" + comp.view_style(), {}, comp.text) for comp in components]
|
||||
|
||||
|
||||
class MultiwordExample:
|
||||
def __init__(self, example_xml):
|
||||
self.other_attributes = {}
|
||||
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")
|
||||
else:
|
||||
self.type = None
|
||||
|
||||
@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_cluster(cluster)
|
||||
return cluster
|
||||
|
||||
def export(self, doc):
|
||||
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):
|
||||
return " ".join([comp.text for comp in components])
|
||||
|
||||
|
||||
class ComponentLexeme(Editable):
|
||||
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")
|
||||
|
||||
for oth_attr in ["lexical_unit_lexeme_id", "slolex", "kol"]:
|
||||
if xml.hasAttribute(oth_attr):
|
||||
self.other_attributes[oth_attr] = xml.getAttribute(oth_attr)
|
||||
|
||||
self.text = self.text.strip()
|
||||
|
||||
def isValid(self):
|
||||
return len(self.text) > 0
|
||||
|
||||
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
|
||||
|
||||
for key, value in self.other_attributes.items():
|
||||
result.setAttribute(key, value)
|
||||
|
||||
return result
|
||||
|
||||
def view_style(self):
|
||||
result = ".comp-text"
|
||||
if self.role is not None:
|
||||
result += ".comp-role"
|
||||
if self.role == "headword":
|
||||
result += ".comp-role-headword"
|
||||
|
||||
return result
|
||||
|
||||
|
1
src/model/example/__init__.py
Normal file
1
src/model/example/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
from model.example.example import Example
|
46
src/model/example/component_lexeme.py
Normal file
46
src/model/example/component_lexeme.py
Normal file
|
@ -0,0 +1,46 @@
|
|||
from model.data import Data
|
||||
|
||||
class ComponentLexeme(Data):
|
||||
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")
|
||||
|
||||
for oth_attr in ["lexical_unit_lexeme_id", "slolex", "kol"]:
|
||||
if xml.hasAttribute(oth_attr):
|
||||
self.other_attributes[oth_attr] = xml.getAttribute(oth_attr)
|
||||
|
||||
self.text = self.text.strip()
|
||||
|
||||
def isValid(self):
|
||||
return len(self.text) > 0
|
||||
|
||||
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
|
||||
|
||||
for key, value in self.other_attributes.items():
|
||||
result.setAttribute(key, value)
|
||||
|
||||
return result
|
||||
|
||||
def view_style(self):
|
||||
result = ".comp-text"
|
||||
if self.role is not None:
|
||||
result += ".comp-role"
|
||||
if self.role == "headword":
|
||||
result += ".comp-role-headword"
|
||||
|
||||
return result
|
||||
|
||||
|
||||
|
24
src/model/example/corpus_example.py
Normal file
24
src/model/example/corpus_example.py
Normal file
|
@ -0,0 +1,24 @@
|
|||
from lib.snabbdom import h
|
||||
|
||||
class CorpusExample:
|
||||
def __init__(self, example_xml):
|
||||
self.other_attributes = {}
|
||||
for oth_attr in ["example_id", "modified", "lexical_unit_id", "audio"]:
|
||||
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 key, value in self.other_attributes.items():
|
||||
result.setAttribute(key, value)
|
||||
return result
|
||||
|
||||
def get_cluster(self):
|
||||
return None
|
||||
|
||||
def get_structure(self):
|
||||
return None
|
||||
|
||||
def view(self, components):
|
||||
return [h("span" + comp.view_style(), {}, comp.text) for comp in components]
|
||||
|
66
src/model/example/example.py
Normal file
66
src/model/example/example.py
Normal file
|
@ -0,0 +1,66 @@
|
|||
from model.data import Data as Editable
|
||||
from model.translation import from_container_list
|
||||
from lib.snabbdom import h
|
||||
|
||||
from model.example.component_lexeme import ComponentLexeme
|
||||
from model.example.corpus_example import CorpusExample
|
||||
from model.example.multiword_example import MultiwordExample
|
||||
|
||||
|
||||
class Example(Editable):
|
||||
def __init__(self, example_xml):
|
||||
self.translations = from_container_list(example_xml.querySelectorAll("translationContainer"))
|
||||
|
||||
inner_xml = example_xml.querySelector("corpusExample")
|
||||
if inner_xml is not None:
|
||||
self.inner = CorpusExample(inner_xml)
|
||||
else:
|
||||
inner_xml = example_xml.querySelector("multiwordExample")
|
||||
self.inner = MultiwordExample(inner_xml)
|
||||
|
||||
all_components = [ComponentLexeme(el) for el in inner_xml.childNodes]
|
||||
self.components = [comp for comp in all_components if comp.isValid()]
|
||||
|
||||
|
||||
def export(self, doc):
|
||||
result = doc.createElement("exampleContainer")
|
||||
|
||||
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 view(self):
|
||||
return self.inner.view(self.components)
|
||||
|
||||
def get_cluster(self):
|
||||
return self.inner.get_cluster()
|
||||
|
||||
def set_cluster(self, cluster):
|
||||
self.inner.cluster = cluster
|
||||
|
||||
def get_structure(self):
|
||||
return self.inner.get_structure()
|
||||
|
||||
def is_collocation(self):
|
||||
return self.get_view_type() == 2
|
||||
|
||||
def is_multiword(self):
|
||||
return self.get_view_type() != 1
|
||||
|
||||
def get_view_type(self):
|
||||
# as per the bosses, these are the rules for different colors
|
||||
if type(self.inner) is CorpusExample:
|
||||
return 1
|
||||
elif self.inner.type == "collocation":
|
||||
return 2
|
||||
else:
|
||||
return 3
|
||||
|
||||
def get_other_attributes(self):
|
||||
return self.inner.other_attributes
|
||||
|
||||
|
51
src/model/example/multiword_example.py
Normal file
51
src/model/example/multiword_example.py
Normal file
|
@ -0,0 +1,51 @@
|
|||
from model.example_clusters import ExampleClusters
|
||||
|
||||
|
||||
class MultiwordExample:
|
||||
def __init__(self, example_xml):
|
||||
self.other_attributes = {}
|
||||
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")
|
||||
else:
|
||||
self.type = None
|
||||
|
||||
@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_cluster(cluster)
|
||||
return cluster
|
||||
|
||||
def export(self, doc):
|
||||
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):
|
||||
return " ".join([comp.text for comp in components])
|
||||
|
Loading…
Reference in New Issue
Block a user