Spell checking tools and project word lists now cooperate

This commit is contained in:
Veronica K. B. Olsen
2019-11-07 22:05:03 +01:00
parent f2265abf32
commit f4accab93f
4 changed files with 86 additions and 12 deletions
+41 -3
View File
@@ -27,11 +27,12 @@ class NWSpellSimple(NWSpellCheck):
def __init__(self):
NWSpellCheck.__init__(self)
self.mainConf = nw.CONFIG
logger.debug("Simple spell checking activated")
return
def setLanguage(self, theLang, projectDict=None):
self.WORDS = []
dictFile = path.join(self.mainConf.dictPath,theLang+".dict")
try:
with open(dictFile,mode="r",encoding="utf-8") as wordsFile:
@@ -44,16 +45,53 @@ class NWSpellSimple(NWSpellCheck):
except Exception as e:
logger.error("Failed to load spell check word list for language %s" % theLang)
logger.error(str(e))
self._readProjectDictionary(projectDict)
for pWord in self.PROJW:
if pWord not in self.WORDS:
self.WORDS.append(pWord)
return
def checkWord(self, theWord):
theWord = theWord.replace(self.mainConf.fmtSingleQuotes[1],"'").lower()
"""Check if a word exists in the word list. Make sure to keep
this function as fast as possible as it is called for every
word by the syntax highlighter.
"""
theWord = theWord.replace(self.mainConf.fmtApostrophe,"'").lower()
return theWord in self.WORDS
def suggestWords(self, theWord):
return get_close_matches(theWord.lower(), self.WORDS, n=10, cutoff=0.75)
"""Get suggestions for correct word from difflib, and make sure
the first character is upper case if that was also the case for
the word be3ing checked. Also make sure the apostrophe is
changed to the one in the dictionary, and then put back in the
results.
"""
theWord = theWord.strip()
if len(theWord) == 0:
return []
firstUp = theWord[0] == theWord[0].upper()
theWord = theWord.lower()
theMatches = get_close_matches(theWord, self.WORDS, n=10, cutoff=0.75)
theOptions = []
for aWord in theMatches:
if len(aWord) == 0:
continue
if firstUp:
aWord = aWord[0].upper() + aWord[1:]
aWord = aWord.replace("'",self.mainConf.fmtApostrophe)
theOptions.append(aWord)
return theOptions
def addWord(self, newWord):
newWord = newWord.strip().lower()
if newWord not in self.WORDS:
self.WORDS.append(newWord)
NWSpellCheck.addWord(self, newWord)
return
def listDictionaries(self):