From a3b46581b0d1daff44109da93cf24e18f87a25ad Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Fri, 12 Feb 2021 20:15:28 +0100 Subject: [PATCH 1/3] Make the simple spell checker use a set instead of a list --- nw/core/spellcheck.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/nw/core/spellcheck.py b/nw/core/spellcheck.py index 8b3807fa..caa8156b 100644 --- a/nw/core/spellcheck.py +++ b/nw/core/spellcheck.py @@ -238,7 +238,7 @@ class NWSpellSimple(NWSpellCheck): when no other is available. This method is fairly slow compared to other implementations. """ - WORDS = [] + theWords = set() def __init__(self): NWSpellCheck.__init__(self) @@ -250,17 +250,19 @@ class NWSpellSimple(NWSpellCheck): """Load a dictionary as a list from the app assets folder. """ self.theLang = theLang - self.WORDS = [] + self.theWords = set() dictFile = os.path.join(self.mainConf.dictPath, theLang+".dict") try: with open(dictFile, mode="r", encoding="utf-8") as wordsFile: for theLine in wordsFile: if len(theLine) == 0 or theLine.startswith("#"): continue - self.WORDS.append(theLine.strip().lower()) - logger.debug("Spell check word list for language %s loaded" % theLang) - logger.debug("Word list contains %d words" % len(self.WORDS)) + self.theWords.add(theLine.strip().lower()) + + logger.debug("Spell check dictionary for language %s loaded" % theLang) + logger.debug("Dictionary contains %d words" % len(self.theWords)) self.spellLanguage = theLang + except Exception as e: logger.error("Failed to load spell check word list for language %s" % theLang) logger.error(str(e)) @@ -268,8 +270,7 @@ class NWSpellSimple(NWSpellCheck): self._readProjectDictionary(projectDict) for pWord in self.projDict: - if pWord not in self.WORDS: - self.WORDS.append(pWord) + self.theWords.add(pWord) return @@ -279,7 +280,7 @@ class NWSpellSimple(NWSpellCheck): word by the syntax highlighter. """ theWord = theWord.replace(self.mainConf.fmtApostrophe, "'").lower() - return theWord in self.WORDS + return theWord in self.theWords def suggestWords(self, theWord): """Get suggestions for correct word from difflib, and make sure @@ -292,7 +293,7 @@ class NWSpellSimple(NWSpellCheck): if len(theWord) == 0: return [] - theMatches = difflib.get_close_matches(theWord.lower(), self.WORDS, n=10, cutoff=0.75) + theMatches = difflib.get_close_matches(theWord.lower(), self.theWords, n=10, cutoff=0.75) theOptions = [] for aWord in theMatches: if len(aWord) == 0: @@ -308,8 +309,8 @@ class NWSpellSimple(NWSpellCheck): """Wrapper for the internal project dictionary feature. """ newWord = newWord.strip().lower() - if newWord not in self.WORDS: - self.WORDS.append(newWord) + if newWord not in self.theWords: + self.theWords.add(newWord) NWSpellCheck.addWord(self, newWord) return From ec1c664ed68411998be2c0c8ef8d0c256cb3b1c1 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Fri, 12 Feb 2021 20:15:38 +0100 Subject: [PATCH 2/3] Fix test --- tests/test_core/test_core_spell.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_core/test_core_spell.py b/tests/test_core/test_core_spell.py index 4a9bde15..90ef0fff 100644 --- a/tests/test_core/test_core_spell.py +++ b/tests/test_core/test_core_spell.py @@ -139,13 +139,13 @@ def testCoreSpell_Simple(monkeypatch, tmpDir, tmpConf): monkeypatch.setattr("builtins.open", causeOSError) spChk.setLanguage("en", wList) assert spChk.spellLanguage is None - assert spChk.WORDS == spChk.projDict + assert spChk.theWords == set(spChk.projDict) monkeypatch.undo() # Load dictionary properly spChk.setLanguage("en", wList) assert spChk.projDict == ["a_word", "b_word", "c_word"] - assert spChk.WORDS == ["e_word", "f_word", "g_word", "a_word", "b_word", "c_word"] + assert spChk.theWords == set(["e_word", "f_word", "g_word", "a_word", "b_word", "c_word"]) # Check words assert spChk.checkWord("a_word") From d08322c1a37bb58fe8e0aab913ac3c98cb9a486b Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Fri, 12 Feb 2021 20:18:32 +0100 Subject: [PATCH 3/3] No point making a list to convert to a set --- tests/test_core/test_core_spell.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_core/test_core_spell.py b/tests/test_core/test_core_spell.py index 90ef0fff..46ee1937 100644 --- a/tests/test_core/test_core_spell.py +++ b/tests/test_core/test_core_spell.py @@ -145,7 +145,7 @@ def testCoreSpell_Simple(monkeypatch, tmpDir, tmpConf): # Load dictionary properly spChk.setLanguage("en", wList) assert spChk.projDict == ["a_word", "b_word", "c_word"] - assert spChk.theWords == set(["e_word", "f_word", "g_word", "a_word", "b_word", "c_word"]) + assert spChk.theWords == {"e_word", "f_word", "g_word", "a_word", "b_word", "c_word"} # Check words assert spChk.checkWord("a_word")