From c0939fbbd43ce885439d2d9d5a7cc7289acb764c Mon Sep 17 00:00:00 2001 From: Ozbolt Menegatti Date: Tue, 11 Jun 2019 10:26:10 +0200 Subject: [PATCH] fixed performance bug for representations No more creating millions of namedtuple classes. Works about 15x faster --- wani.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/wani.py b/wani.py index 5717210..223b310 100644 --- a/wani.py +++ b/wani.py @@ -261,9 +261,7 @@ class WordFormMsdCR(WordFormAnyCR): def _render(self): msd = self.word_renderer.get_lemma_msd(self.lemma, self.msd) - WordLemma = namedtuple('WordLemmaOnly', 'msd most_frequent_text lemma text') - backup_word = WordLemma(msd=msd, most_frequent_text=lambda *x: None, lemma=None, text=None) - self.words.append(backup_word) + self.words.append(WordMsdOnly(msd)) return super()._render() @@ -832,12 +830,16 @@ def get_msd(comp): logging.error(d, file=sys.stderr) raise NotImplementedError("MSD?") -def lemma_only_word(msd): - if msd is None: + +class WordMsdOnly: + def __init__(self, msd): + self.msd = msd + self.lemma = None + self.text = None + + def most_frequent_text(self, _): return None - else: - WordLemma = namedtuple('WordLemmaOnly', 'msd most_frequent_text lemma text') - return WordLemma(msd=msd, most_frequent_text=lambda *x: None, lemma=None, text=None) + class Word: def __init__(self, xml, do_msd_translate):