socializer/src/extra/SpellChecker/spellchecker.py

70 lines
2.2 KiB
Python
Raw Normal View History

2016-02-13 17:06:36 -06:00
# -*- coding: utf-8 -*-
2019-01-02 04:42:53 +03:00
from __future__ import unicode_literals
import os
2016-02-13 17:06:36 -06:00
import logging
import widgetUtils
import output
import config
import languageHandler
from platform_utils import paths
from . import checker
2019-01-02 04:42:53 +03:00
from . import wx_ui
2016-02-16 17:20:55 -06:00
log = logging.getLogger("extra.SpellChecker.spellChecker")
2016-02-13 17:06:36 -06:00
class spellChecker(object):
def __init__(self, text):
2016-02-16 17:20:55 -06:00
super(spellChecker, self).__init__()
self.active = True
self.checker = checker.SpellChecker()
log.debug("Using language: %s" % (languageHandler.getLanguage(),))
2016-02-16 17:20:55 -06:00
try:
2019-12-03 12:06:36 -06:00
self.checker.set_language(languageHandler.curLang::2])
except ValueError:
log.exception("Dictionary for language %s not found." % (languageHandler.curLang,))
2016-02-16 17:20:55 -06:00
wx_ui.dict_not_found_error()
self.active = False
self.checker.set_text(text)
self.generator = self.checker.check_words()
2016-02-16 17:20:55 -06:00
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)
self.check()
self.dialog.get_response()
self.fixed_text = self.checker.text
2016-02-13 17:06:36 -06:00
2016-02-16 17:20:55 -06:00
def check(self):
try:
suggestions, context, self.wordIndex = next(self.generator)
2019-01-02 04:42:53 +03:00
textToSay = _("Misspelled word: %s") % (self.checker.word,)
context = context
2016-02-16 17:20:55 -06:00
self.dialog.set_title(textToSay)
output.speak(textToSay)
self.dialog.set_word_and_suggestions(word=self.checker.word, context=context, suggestions=[suggestion.term for suggestion in suggestions])
2016-02-16 17:20:55 -06:00
except StopIteration:
log.debug("Process finished.")
wx_ui.finished()
self.dialog.Destroy()
2016-02-13 17:06:36 -06:00
2016-02-16 17:20:55 -06:00
def ignore(self, ev):
self.check()
2016-02-13 17:06:36 -06:00
2016-02-16 17:20:55 -06:00
def ignoreAll(self, ev):
self.checker.ignore_word(word=self.checker.word)
2016-02-16 17:20:55 -06:00
self.check()
2016-02-13 17:06:36 -06:00
2016-02-16 17:20:55 -06:00
def replace(self, ev):
self.checker.replace(self.dialog.get_selected_suggestion())
self.check()
2016-02-13 17:06:36 -06:00
2016-02-16 17:20:55 -06:00
def replaceAll(self, ev):
self.checker.replace_all(self.dialog.get_selected_suggestion())
self.check()
def clean(self):
if hasattr(self, "dialog"):
self.dialog.Destroy()