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
+1
View File
@@ -90,6 +90,7 @@ class Config:
self.showTabsNSpaces = False self.showTabsNSpaces = False
self.showLineEndings = False self.showLineEndings = False
self.fmtApostrophe = nwUnicode.U_RSQUO
self.fmtSingleQuotes = [nwUnicode.U_LSQUO,nwUnicode.U_RSQUO] self.fmtSingleQuotes = [nwUnicode.U_LSQUO,nwUnicode.U_RSQUO]
self.fmtDoubleQuotes = [nwUnicode.U_LDQUO,nwUnicode.U_RDQUO] self.fmtDoubleQuotes = [nwUnicode.U_LDQUO,nwUnicode.U_RDQUO]
+34
View File
@@ -22,8 +22,11 @@ class NWSpellCheck():
SP_SYMSPELL = "symspell" SP_SYMSPELL = "symspell"
theDict = None theDict = None
PROJW = []
def __init__(self): def __init__(self):
self.mainConf = nw.CONFIG
self.projectDict = None
return return
def setLanguage(self, theLang, projectDict=None): def setLanguage(self, theLang, projectDict=None):
@@ -36,9 +39,40 @@ class NWSpellCheck():
return [] return []
def addWord(self, newWord): def addWord(self, newWord):
if self.projectDict is not None and newWord not in self.PROJW:
newWord = newWord.strip()
self.PROJW.append(newWord)
try:
with open(self.projectDict,mode="w+",encoding="utf-8") as outFile:
for pWord in self.PROJW:
outFile.write("%s\n" % pWord)
except Exception as e:
logger.error("Failed to write to project word list at %s" % str(self.projectDict))
logger.error(str(e))
return return
def listDictionaries(self): def listDictionaries(self):
return [] return []
##
# Internal Functions
##
def _readProjectDictionary(self, projectDict):
self.PROJW = []
if projectDict is not None:
self.projectDict = projectDict
try:
with open(projectDict,mode="r",encoding="utf-8") as wordsFile:
for theLine in wordsFile:
theLine = theLine.strip()
if len(theLine) > 0 and theLine not in self.PROJW:
self.PROJW.append(theLine)
logger.debug("Project word list")
logger.debug("Project word list contains %d words" % len(self.PROJW))
except Exception as e:
logger.error("Failed to load project word list")
logger.error(str(e))
return
# END Class NWSpellCheck # END Class NWSpellCheck
+10 -9
View File
@@ -32,15 +32,16 @@ class NWSpellEnchant(NWSpellCheck):
crash. crash.
""" """
try: try:
if projectDict is None: self.theDict = enchant.Dict(theLang)
self.theDict = enchant.Dict(theLang)
else:
self.theDict = enchant.DictWithPWL(theLang, projectDict)
logger.debug("Enchant spell checking for language %s loaded" % theLang) logger.debug("Enchant spell checking for language %s loaded" % theLang)
except: except:
logger.error("Failed to load enchant spell checking for language %s" % theLang) logger.error("Failed to load enchant spell checking for language %s" % theLang)
self.theDict = NWSpellEnchantDummy() self.theDict = NWSpellEnchantDummy()
self._readProjectDictionary(projectDict)
for pWord in self.PROJW:
self.theDict.add_to_session(pWord)
return return
def checkWord(self, theWord): def checkWord(self, theWord):
@@ -50,7 +51,8 @@ class NWSpellEnchant(NWSpellCheck):
return self.theDict.suggest(theWord) return self.theDict.suggest(theWord)
def addWord(self, newWord): def addWord(self, newWord):
self.theDict.add_to_pwl(newWord) self.theDict.add_to_session(newWord)
NWSpellCheck.addWord(self, newWord)
return return
def listDictionaries(self): def listDictionaries(self):
@@ -58,10 +60,9 @@ class NWSpellEnchant(NWSpellCheck):
for spTag, spProvider in enchant.list_dicts(): for spTag, spProvider in enchant.list_dicts():
spList = [] spList = []
if spTag[:2] in isoLanguage.ISO_639_1: if spTag[:2] in isoLanguage.ISO_639_1:
langName = isoLanguage.ISO_639_1[spTag[:2]] spList.append(isoLanguage.ISO_639_1[spTag[:2]])
else: else:
langName = spTag[:2] spList.append(spTag[:2])
spList.append(langName)
if len(spTag) > 3: if len(spTag) > 3:
spList.append("(%s)" % spTag[3:]) spList.append("(%s)" % spTag[3:])
spList.append("[%s]" % spProvider.name) spList.append("[%s]" % spProvider.name)
@@ -82,7 +83,7 @@ class NWSpellEnchantDummy:
def suggest(self, theWord): def suggest(self, theWord):
return [] return []
def add_to_pwl(self, theWord): def add_to_session(self, theWord):
return return
# END Class NWSpellEnchantDummy # END Class NWSpellEnchantDummy
+41 -3
View File
@@ -27,11 +27,12 @@ class NWSpellSimple(NWSpellCheck):
def __init__(self): def __init__(self):
NWSpellCheck.__init__(self) NWSpellCheck.__init__(self)
self.mainConf = nw.CONFIG
logger.debug("Simple spell checking activated") logger.debug("Simple spell checking activated")
return return
def setLanguage(self, theLang, projectDict=None): def setLanguage(self, theLang, projectDict=None):
self.WORDS = []
dictFile = path.join(self.mainConf.dictPath,theLang+".dict") dictFile = 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:
@@ -44,16 +45,53 @@ class NWSpellSimple(NWSpellCheck):
except Exception as e: except Exception as e:
logger.error("Failed to load spell check word list for language %s" % theLang) logger.error("Failed to load spell check word list for language %s" % theLang)
logger.error(str(e)) logger.error(str(e))
self._readProjectDictionary(projectDict)
for pWord in self.PROJW:
if pWord not in self.WORDS:
self.WORDS.append(pWord)
return return
def checkWord(self, theWord): 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 return theWord in self.WORDS
def suggestWords(self, theWord): 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): def addWord(self, newWord):
newWord = newWord.strip().lower()
if newWord not in self.WORDS:
self.WORDS.append(newWord)
NWSpellCheck.addWord(self, newWord)
return return
def listDictionaries(self): def listDictionaries(self):