From b4dd1d1a9ea84e9773dd3e3ba939fa4a907e56d3 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Tue, 5 Nov 2019 23:22:53 +0100 Subject: [PATCH] Added simple spell chacking using difflib --- nw/config.py | 9 +++--- nw/tools/__init__.py | 2 ++ nw/tools/spellsimple.py | 62 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 nw/tools/spellsimple.py diff --git a/nw/config.py b/nw/config.py index 5a50395e..d65519cb 100644 --- a/nw/config.py +++ b/nw/config.py @@ -51,9 +51,10 @@ class Config: self.appPath = None self.appRoot = None self.appIcon = None - self.guiPath = None + self.assetPath = None self.themeRoot = None - self.themePath = None + self.graphPath = None + self.dictPath = None # Set default values self.confChanged = False @@ -165,10 +166,10 @@ class Config: self.lastPath = self.homePath self.appPath = getattr(sys, "_MEIPASS", path.abspath(path.dirname(__file__))) self.appRoot = path.join(self.appPath,path.pardir) - self.helpPath = path.join(self.appRoot,"help","en_GB") - self.guiPath = path.join(self.appPath,"gui") + self.assetPath = path.join(self.appPath,"assets") self.themeRoot = path.join(self.appPath,"themes") self.graphPath = path.join(self.appPath,"graphics") + self.dictPath = path.join(self.assetPath,"dict") self.appIcon = path.join(self.graphPath, nwFiles.APP_ICON) # If config folder does not exist, make it. diff --git a/nw/tools/__init__.py b/nw/tools/__init__.py index 52bff781..582d18fb 100644 --- a/nw/tools/__init__.py +++ b/nw/tools/__init__.py @@ -4,6 +4,7 @@ from nw.tools.analyse import TextAnalysis from nw.tools.optlaststate import OptLastState from nw.tools.spellcheck import NWSpellCheck from nw.tools.spellenchant import NWSpellEnchant +from nw.tools.spellsimple import NWSpellSimple from nw.tools.translate import numberToWord from nw.tools.wordcount import countWords @@ -12,6 +13,7 @@ __all__ = [ "OptLastState", "NWSpellCheck", "NWSpellEnchant", + "NWSpellSimple", "numberToWord", "countWords", ] diff --git a/nw/tools/spellsimple.py b/nw/tools/spellsimple.py new file mode 100644 index 00000000..b2216de9 --- /dev/null +++ b/nw/tools/spellsimple.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- +"""novelWriter Spell Check Simple + + novelWriter – Spell Check Simple +================================== + Simple Python3 spell checker based on difflib + + File History: + Created: 2019-06-11 [0.1.5] + +""" + +import logging +import nw + +from os import path +from collections import Counter +from difflib import get_close_matches + +logger = logging.getLogger(__name__) + +from nw.tools.spellcheck import NWSpellCheck + +class NWSpellSimple(NWSpellCheck): + + WORDS = [] + + def __init__(self): + NWSpellCheck.__init__(self) + self.mainConf = nw.CONFIG + logger.debug("Norvig spell checking activated") + return + + def setLanguage(self, theLang, projectDict=None): + dictFile = 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)) + except Exception as e: + logger.error("Failed to load spell check word list for language %s" % theLang) + logger.error(str(e)) + return + + def checkWord(self, theWord): + theWord = theWord.replace(self.mainConf.fmtSingleQuotes[1],"'").lower() + return theWord in self.WORDS + + def suggestWords(self, theWord): + return get_close_matches(theWord.lower(), self.WORDS, n=10, cutoff=0.60) + + def addWord(self, newWord): + return + + def listDictionaries(self): + return ["default"] + +# END Class NWSpellSimple