From 460a55cb6c50e872d2d2752468c0e183eee80e7d Mon Sep 17 00:00:00 2001 From: Ozbolt Menegatti Date: Sun, 2 Jun 2019 13:50:53 +0200 Subject: [PATCH] Improving representation speed ~5% --- wani.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/wani.py b/wani.py index 4043669..2447516 100644 --- a/wani.py +++ b/wani.py @@ -881,6 +881,7 @@ class WordMsdRenderer: self.frequent_words = {} self.lemma_msd = {} self.lemma_features = lemma_features + self.memoized_msd_merges = {} def add_words(self, words): self.all_words.extend(words) @@ -918,8 +919,11 @@ class WordMsdRenderer: l1 if l1 != "-" else l2 for l1, l2 in zip(lf[cmsd[0]], cmsd) ) - @staticmethod - def merge_msd(common_msd, new_msd): + def merge_msd(self, common_msd, new_msd): + key = (common_msd, new_msd) + if key in self.memoized_msd_merges: + return self.memoized_msd_merges[key] + def merge_letter(l1, l2): if l1 == "*": return l2 @@ -928,7 +932,9 @@ class WordMsdRenderer: else: return l1 - return "".join(merge_letter(l1, l2) for l1, l2 in zip(common_msd, new_msd)) + value = "".join(merge_letter(l1, l2) for l1, l2 in zip(common_msd, new_msd)) + self.memoized_msd_merges[key] = value + return value def render(self, lemma, msd): if lemma in self.rendered_words: