Spellchecker: allow adding words to personal dictionaries

This commit is contained in:
Manuel Cortez 2017-12-01 12:26:16 -06:00
parent 4d1732b3aa
commit 2c1bd6a8c8
2 changed files with 17 additions and 8 deletions

View File

@ -1,15 +1,17 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import logging import logging
log = logging.getLogger("extra.SpellChecker.spellChecker")
import wx_ui import wx_ui
import widgetUtils import widgetUtils
import output import output
import config import config
import languageHandler import languageHandler
import enchant
import paths
import twitterFilter
from enchant.checker import SpellChecker from enchant.checker import SpellChecker
from enchant.errors import DictNotFoundError from enchant.errors import DictNotFoundError
from enchant import tokenize from enchant import tokenize
import twitterFilter log = logging.getLogger("extra.SpellChecker.spellChecker")
class spellChecker(object): class spellChecker(object):
def __init__(self, text, dictionary): def __init__(self, text, dictionary):
@ -19,15 +21,16 @@ class spellChecker(object):
try: try:
if config.app["app-settings"]["language"] == "system": if config.app["app-settings"]["language"] == "system":
log.debug("Using the system language") log.debug("Using the system language")
self.checker = SpellChecker(languageHandler.curLang[:2], filters=[twitterFilter.TwitterFilter, tokenize.EmailFilter, tokenize.URLFilter]) self.dict = enchant.DictWithPWL(languageHandler.curLang[:2], paths.config_path("wordlist.dict"))
else: else:
log.debug("Using language: %s" % (languageHandler.getLanguage(),)) log.debug("Using language: %s" % (languageHandler.getLanguage(),))
self.checker = SpellChecker(languageHandler.getLanguage()[:2], filters=[twitterFilter.TwitterFilter, tokenize.EmailFilter, tokenize.URLFilter]) self.dict = enchant.DictWithPWL(languageHandler.getLanguage()[:2], paths.config_path("wordlist.dict"))
self.checker.set_text(text)
except DictNotFoundError: except DictNotFoundError:
log.exception("Dictionary for language %s not found." % (dictionary,)) log.exception("Dictionary for language %s not found." % (dictionary,))
wx_ui.dict_not_found_error() wx_ui.dict_not_found_error()
self.active = False self.active = False
self.checker = SpellChecker(self.dict, filters=[twitterFilter.TwitterFilter, tokenize.EmailFilter, tokenize.URLFilter])
self.checker.set_text(text)
if self.active == True: if self.active == True:
log.debug("Creating dialog...") log.debug("Creating dialog...")
self.dialog = wx_ui.spellCheckerDialog() self.dialog = wx_ui.spellCheckerDialog()
@ -35,6 +38,7 @@ class spellChecker(object):
widgetUtils.connect_event(self.dialog.ignoreAll, widgetUtils.BUTTON_PRESSED, self.ignoreAll) widgetUtils.connect_event(self.dialog.ignoreAll, widgetUtils.BUTTON_PRESSED, self.ignoreAll)
widgetUtils.connect_event(self.dialog.replace, widgetUtils.BUTTON_PRESSED, self.replace) widgetUtils.connect_event(self.dialog.replace, widgetUtils.BUTTON_PRESSED, self.replace)
widgetUtils.connect_event(self.dialog.replaceAll, widgetUtils.BUTTON_PRESSED, self.replaceAll) widgetUtils.connect_event(self.dialog.replaceAll, widgetUtils.BUTTON_PRESSED, self.replaceAll)
widgetUtils.connect_event(self.dialog.add, widgetUtils.BUTTON_PRESSED, self.add)
self.check() self.check()
self.dialog.get_response() self.dialog.get_response()
self.fixed_text = self.checker.get_text() self.fixed_text = self.checker.get_text()
@ -51,8 +55,6 @@ class spellChecker(object):
log.debug("Process finished.") log.debug("Process finished.")
wx_ui.finished() wx_ui.finished()
self.dialog.Destroy() self.dialog.Destroy()
# except AttributeError:
# pass
def ignore(self, ev): def ignore(self, ev):
self.check() self.check()
@ -67,4 +69,8 @@ class spellChecker(object):
def replaceAll(self, ev): def replaceAll(self, ev):
self.checker.replace_always(self.dialog.get_selected_suggestion()) self.checker.replace_always(self.dialog.get_selected_suggestion())
self.check() self.check()
def add(self, ev):
self.checker.add()
self.check()

View File

@ -18,6 +18,7 @@
############################################################ ############################################################
import wx import wx
import application import application
class spellCheckerDialog(wx.Dialog): class spellCheckerDialog(wx.Dialog):
def __init__(self): def __init__(self):
super(spellCheckerDialog, self).__init__(None, 1) super(spellCheckerDialog, self).__init__(None, 1)
@ -42,12 +43,14 @@ class spellCheckerDialog(wx.Dialog):
self.ignoreAll = wx.Button(panel, -1, _(u"Ignore all")) self.ignoreAll = wx.Button(panel, -1, _(u"Ignore all"))
self.replace = wx.Button(panel, -1, _(u"Replace")) self.replace = wx.Button(panel, -1, _(u"Replace"))
self.replaceAll = wx.Button(panel, -1, _(u"Replace all")) self.replaceAll = wx.Button(panel, -1, _(u"Replace all"))
self.add = wx.Button(panel, -1, _(u"Add to personal dictionary"))
close = wx.Button(panel, wx.ID_CANCEL) close = wx.Button(panel, wx.ID_CANCEL)
btnBox = wx.BoxSizer(wx.HORIZONTAL) btnBox = wx.BoxSizer(wx.HORIZONTAL)
btnBox.Add(self.ignore, 0, wx.ALL, 5) btnBox.Add(self.ignore, 0, wx.ALL, 5)
btnBox.Add(self.ignoreAll, 0, wx.ALL, 5) btnBox.Add(self.ignoreAll, 0, wx.ALL, 5)
btnBox.Add(self.replace, 0, wx.ALL, 5) btnBox.Add(self.replace, 0, wx.ALL, 5)
btnBox.Add(self.replaceAll, 0, wx.ALL, 5) btnBox.Add(self.replaceAll, 0, wx.ALL, 5)
btnBox.Add(self.add, 0, wx.ALL, 5)
btnBox.Add(close, 0, wx.ALL, 5) btnBox.Add(close, 0, wx.ALL, 5)
sizer.Add(wordBox, 0, wx.ALL, 5) sizer.Add(wordBox, 0, wx.ALL, 5)
sizer.Add(contextBox, 0, wx.ALL, 5) sizer.Add(contextBox, 0, wx.ALL, 5)