Unified searching for translation location and cluster nums back in example translations

This commit is contained in:
2020-01-26 13:35:00 +01:00
parent 747cd694a7
commit b860fa4807
3 changed files with 40 additions and 65 deletions
+27 -32
View File
@@ -6,25 +6,6 @@ from model.translation import Translation
from model.sense import Sense
def get_translation_location(entry, translation):
def find_in_clusters(si, clusters):
for ci, cluster in enumerate(clusters):
for ti, search_translation in enumerate(cluster):
if search_translation == translation:
return (si, ci, ti), (sense, cluster)
return None
for si, sense in enumerate(entry.senses):
res = find_in_clusters(si, sense.translations)
if res is not None:
return res
for example in sense.examples:
res = find_in_clusters(si, example.translations)
if res is not None:
return res
window.console.log("should not be here...")
class EditTranslation(DataChgClickMessage):
def update_model(self, model):
@@ -39,20 +20,34 @@ class EditTranslation(DataChgClickMessage):
new_cluster_idx = int(document.getElementById("cluster-num").value) - 1
self.handle_cluster_change(new_cluster_idx, model)
@staticmethod
def get_translation_location(entry, translation):
def find_in_clusters(parent):
for ci, cluster in enumerate(parent.translations):
for ti, search_translation in enumerate(cluster):
if search_translation == translation:
return (ci, ti), (parent, cluster)
return None
for sense in entry.senses:
res = find_in_clusters(sense)
if res is not None:
return res
for example in sense.examples:
res = find_in_clusters(example)
if res is not None:
return res
window.console.log("should not be here...")
def handle_cluster_change(self, new_cluster_idx, model):
if self.old_cluster_idx == new_cluster_idx:
return
# first, find out the correct sense
for sense in model.entry.senses:
for cidx, cluster in enumerate(sense.translations):
for tidx, t in enumerate(cluster):
if t == self.translation:
#found, lets do whatever needs to be done
self.do_cluster_change(sense, cluster, cidx, tidx, new_cluster_idx)
# we are done, lets return
return
(cidx, tidx), (parent, cluster) = EditTranslation.get_translation_location(model.entry, self.translation)
self.do_cluster_change(parent, cluster, cidx, tidx, new_cluster_idx)
def do_cluster_change(self, sense_or_example, cluster, cidx, tidx, new_cluster_idx):
# remove the translation from the old cluster
@@ -76,7 +71,7 @@ class EditTranslation(DataChgClickMessage):
class MoveRight(DataChgClickMessage):
def update_model(self, model):
translation = self.get_arg(0, Translation)
(_, _, idx), (_, cluster) = get_translation_location(model.entry, translation)
(_, idx), (_, cluster) = EditTranslation.get_translation_location(model.entry, translation)
if idx != len(cluster) - 1:
cluster[idx], cluster[idx + 1] = cluster[idx + 1], cluster[idx]
model.translation = None
@@ -85,7 +80,7 @@ class MoveRight(DataChgClickMessage):
class MoveLeft(DataChgClickMessage):
def update_model(self, model):
translation = self.get_arg(0, Translation)
(_, _, idx), (_, cluster) = get_translation_location(model.entry, translation)
(_, idx), (_, cluster) = EditTranslation.get_translation_location(model.entry, translation)
if idx != 0 and len(cluster) > 1:
cluster[idx], cluster[idx - 1] = cluster[idx - 1], cluster[idx]
model.translation = None
@@ -94,7 +89,7 @@ class MoveLeft(DataChgClickMessage):
class BinTranslation(DataChgClickMessage):
def update_model(self, model):
translation = self.get_arg(0, Translation)
(_, cidx, tidx), (sense, cluster) = get_translation_location(model.entry, translation)
(cidx, tidx), (sense, cluster) = EditTranslation.get_translation_location(model.entry, translation)
if len(cluster) == 1:
# remove empty cluster
sense.translations.splice(cidx, 1)