Merge branch 'testing' into user_dict

This commit is contained in:
Veronica K. B. Olsen
2021-02-12 20:25:05 +01:00
2 changed files with 12 additions and 13 deletions
+10 -11
View File
@@ -246,7 +246,7 @@ class NWSpellSimple(NWSpellCheck):
when no other is available. This method is fairly slow compared to when no other is available. This method is fairly slow compared to
other implementations. other implementations.
""" """
WORDS = [] theWords = set()
def __init__(self): def __init__(self):
NWSpellCheck.__init__(self) NWSpellCheck.__init__(self)
@@ -258,17 +258,17 @@ class NWSpellSimple(NWSpellCheck):
"""Load a dictionary as a list from the app assets folder. """Load a dictionary as a list from the app assets folder.
""" """
self.theLang = theLang self.theLang = theLang
self.WORDS = [] self.theWords = set()
dictFile = os.path.join(self.mainConf.dictPath, theLang+".dict") dictFile = os.path.join(self.mainConf.dictPath, theLang+".dict")
try: try:
with open(dictFile, mode="r", encoding="utf-8") as wordsFile: with open(dictFile, mode="r", encoding="utf-8") as wordsFile:
for theLine in wordsFile: for theLine in wordsFile:
if len(theLine) == 0 or theLine.startswith("#"): if len(theLine) == 0 or theLine.startswith("#"):
continue continue
self.WORDS.append(theLine.strip().lower()) self.theWords.add(theLine.strip().lower())
logger.debug("Spell check word list for language %s loaded" % theLang) logger.debug("Spell check dictionary for language %s loaded" % theLang)
logger.debug("Word list contains %d words" % len(self.WORDS)) logger.debug("Dictionary contains %d words" % len(self.theWords))
self.spellLanguage = theLang self.spellLanguage = theLang
except Exception: except Exception:
@@ -278,8 +278,7 @@ class NWSpellSimple(NWSpellCheck):
self._readProjectDictionary(projectDict) self._readProjectDictionary(projectDict)
for pWord in self.projDict: for pWord in self.projDict:
if pWord not in self.WORDS: self.theWords.add(pWord)
self.WORDS.append(pWord)
return return
@@ -289,7 +288,7 @@ class NWSpellSimple(NWSpellCheck):
word by the syntax highlighter. word by the syntax highlighter.
""" """
theWord = theWord.replace(self.mainConf.fmtApostrophe, "'").lower() theWord = theWord.replace(self.mainConf.fmtApostrophe, "'").lower()
return theWord in self.WORDS return theWord in self.theWords
def suggestWords(self, theWord): def suggestWords(self, theWord):
"""Get suggestions for correct word from difflib, and make sure """Get suggestions for correct word from difflib, and make sure
@@ -302,7 +301,7 @@ class NWSpellSimple(NWSpellCheck):
if len(theWord) == 0: if len(theWord) == 0:
return [] 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 = [] theOptions = []
for aWord in theMatches: for aWord in theMatches:
if len(aWord) == 0: if len(aWord) == 0:
@@ -318,8 +317,8 @@ class NWSpellSimple(NWSpellCheck):
"""Wrapper for the internal project dictionary feature. """Wrapper for the internal project dictionary feature.
""" """
newWord = newWord.strip().lower() newWord = newWord.strip().lower()
if newWord not in self.WORDS: if newWord not in self.theWords:
self.WORDS.append(newWord) self.theWords.add(newWord)
NWSpellCheck.addWord(self, newWord) NWSpellCheck.addWord(self, newWord)
return return
+2 -2
View File
@@ -139,13 +139,13 @@ def testCoreSpell_Simple(monkeypatch, tmpDir, tmpConf):
monkeypatch.setattr("builtins.open", causeOSError) monkeypatch.setattr("builtins.open", causeOSError)
spChk.setLanguage("en", wList) spChk.setLanguage("en", wList)
assert spChk.spellLanguage is None assert spChk.spellLanguage is None
assert spChk.WORDS == spChk.projDict assert spChk.theWords == set(spChk.projDict)
monkeypatch.undo() monkeypatch.undo()
# Load dictionary properly # Load dictionary properly
spChk.setLanguage("en", wList) spChk.setLanguage("en", wList)
assert spChk.projDict == ["a_word", "b_word", "c_word"] 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 == {"e_word", "f_word", "g_word", "a_word", "b_word", "c_word"}
# Check words # Check words
assert spChk.checkWord("a_word") assert spChk.checkWord("a_word")