2014-10-27 16:29:04 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-03-19 21:40:55 -06:00
|
|
|
import os
|
2015-01-18 17:19:39 -06:00
|
|
|
import logging
|
2018-11-22 13:35:19 -06:00
|
|
|
from . import wx_ui
|
2014-11-16 22:30:58 -06:00
|
|
|
import widgetUtils
|
2014-10-27 16:29:04 -06:00
|
|
|
import output
|
|
|
|
import config
|
|
|
|
import languageHandler
|
2017-12-01 12:26:16 -06:00
|
|
|
import enchant
|
|
|
|
import paths
|
2018-11-22 13:35:19 -06:00
|
|
|
from . import twitterFilter
|
2014-10-27 16:29:04 -06:00
|
|
|
from enchant.checker import SpellChecker
|
|
|
|
from enchant.errors import DictNotFoundError
|
2015-02-01 00:49:03 -06:00
|
|
|
from enchant import tokenize
|
2017-12-01 12:26:16 -06:00
|
|
|
log = logging.getLogger("extra.SpellChecker.spellChecker")
|
2014-10-27 16:29:04 -06:00
|
|
|
|
2014-11-16 22:30:58 -06:00
|
|
|
class spellChecker(object):
|
2021-06-16 16:18:41 -05:00
|
|
|
def __init__(self, text, dictionary):
|
|
|
|
super(spellChecker, self).__init__()
|
|
|
|
# Set Dictionary path if not set in a previous call to this method.
|
|
|
|
# Dictionary path will be located in user config, see https://github.com/manuelcortez/twblue/issues/208
|
2020-05-01 13:14:27 -05:00
|
|
|
# dict_path = enchant.get_param("enchant.myspell.dictionary.path")
|
|
|
|
# if dict_path == None:
|
|
|
|
# enchant.set_param("enchant.myspell.dictionary.path", os.path.join(paths.config_path(), "dicts"))
|
|
|
|
# log.debug("Dictionary path set to %s" % (os.path.join(paths.config_path(), "dicts"),))
|
2021-06-16 16:18:41 -05:00
|
|
|
log.debug("Creating the SpellChecker object. Dictionary: %s" % (dictionary,))
|
|
|
|
self.active = True
|
|
|
|
try:
|
|
|
|
if config.app["app-settings"]["language"] == "system":
|
|
|
|
log.debug("Using the system language")
|
|
|
|
self.dict = enchant.DictWithPWL(languageHandler.curLang[:2], os.path.join(paths.config_path(), "wordlist.dict"))
|
|
|
|
else:
|
|
|
|
log.debug("Using language: %s" % (languageHandler.getLanguage(),))
|
|
|
|
self.dict = enchant.DictWithPWL(languageHandler.getLanguage()[:2], os.path.join(paths.config_path(), "wordlist.dict"))
|
|
|
|
except DictNotFoundError:
|
|
|
|
log.exception("Dictionary for language %s not found." % (dictionary,))
|
|
|
|
wx_ui.dict_not_found_error()
|
|
|
|
self.active = False
|
|
|
|
self.checker = SpellChecker(self.dict, filters=[twitterFilter.TwitterFilter, tokenize.EmailFilter, tokenize.URLFilter])
|
|
|
|
self.checker.set_text(text)
|
|
|
|
if self.active == True:
|
|
|
|
log.debug("Creating dialog...")
|
|
|
|
self.dialog = wx_ui.spellCheckerDialog()
|
|
|
|
widgetUtils.connect_event(self.dialog.ignore, widgetUtils.BUTTON_PRESSED, self.ignore)
|
|
|
|
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.replaceAll, widgetUtils.BUTTON_PRESSED, self.replaceAll)
|
|
|
|
widgetUtils.connect_event(self.dialog.add, widgetUtils.BUTTON_PRESSED, self.add)
|
|
|
|
self.check()
|
|
|
|
self.dialog.get_response()
|
|
|
|
self.fixed_text = self.checker.get_text()
|
2014-10-27 16:29:04 -06:00
|
|
|
|
2021-06-16 16:18:41 -05:00
|
|
|
def check(self):
|
|
|
|
try:
|
|
|
|
next(self.checker)
|
|
|
|
textToSay = _(u"Misspelled word: %s") % (self.checker.word,)
|
|
|
|
context = u"... %s %s %s" % (self.checker.leading_context(10), self.checker.word, self.checker.trailing_context(10))
|
|
|
|
self.dialog.set_title(textToSay)
|
|
|
|
output.speak(textToSay)
|
|
|
|
self.dialog.set_word_and_suggestions(word=self.checker.word, context=context, suggestions=self.checker.suggest())
|
|
|
|
except StopIteration:
|
|
|
|
log.debug("Process finished.")
|
|
|
|
wx_ui.finished()
|
|
|
|
self.dialog.Destroy()
|
2014-10-27 16:29:04 -06:00
|
|
|
|
2021-06-16 16:18:41 -05:00
|
|
|
def ignore(self, ev):
|
|
|
|
self.check()
|
2014-10-27 16:29:04 -06:00
|
|
|
|
2021-06-16 16:18:41 -05:00
|
|
|
def ignoreAll(self, ev):
|
|
|
|
self.checker.ignore_always(word=self.checker.word)
|
|
|
|
self.check()
|
2014-10-27 16:29:04 -06:00
|
|
|
|
2021-06-16 16:18:41 -05:00
|
|
|
def replace(self, ev):
|
|
|
|
self.checker.replace(self.dialog.get_selected_suggestion())
|
|
|
|
self.check()
|
2014-10-27 16:29:04 -06:00
|
|
|
|
2021-06-16 16:18:41 -05:00
|
|
|
def replaceAll(self, ev):
|
|
|
|
self.checker.replace_always(self.dialog.get_selected_suggestion())
|
|
|
|
self.check()
|
2017-12-01 12:26:16 -06:00
|
|
|
|
2021-06-16 16:18:41 -05:00
|
|
|
def add(self, ev):
|
|
|
|
self.checker.add()
|
|
|
|
self.check()
|