Added spell checker
This commit is contained in:
		@@ -1,4 +1,4 @@
 | 
			
		||||
import spellchecker
 | 
			
		||||
import platform
 | 
			
		||||
if platform.system() == "Windows":
 | 
			
		||||
 from wx_ui import *
 | 
			
		||||
	from wx_ui import *
 | 
			
		||||
@@ -1,6 +1,5 @@
 | 
			
		||||
# -*- coding: utf-8 -*-
 | 
			
		||||
import logging
 | 
			
		||||
log = logging.getLogger("extra.SpellChecker.spellChecker")
 | 
			
		||||
import wx_ui
 | 
			
		||||
import widgetUtils
 | 
			
		||||
import output
 | 
			
		||||
@@ -9,62 +8,62 @@ import languageHandler
 | 
			
		||||
from enchant.checker import SpellChecker
 | 
			
		||||
from enchant.errors import DictNotFoundError
 | 
			
		||||
from enchant import tokenize
 | 
			
		||||
import twitterFilter
 | 
			
		||||
 | 
			
		||||
log = logging.getLogger("extra.SpellChecker.spellChecker")
 | 
			
		||||
 | 
			
		||||
class spellChecker(object):
 | 
			
		||||
 def __init__(self, text, dictionary):
 | 
			
		||||
  super(spellChecker, self).__init__()
 | 
			
		||||
  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.checker = SpellChecker(filters=[twitterFilter.TwitterFilter, tokenize.EmailFilter, tokenize.URLFilter])
 | 
			
		||||
   else:
 | 
			
		||||
    log.debug("Using language: %s" % (languageHandler.getLanguage(),))
 | 
			
		||||
    self.checker = SpellChecker(languageHandler.getLanguage(), filters=[twitterFilter.TwitterFilter, tokenize.EmailFilter, tokenize.URLFilter])
 | 
			
		||||
   self.checker.set_text(text)
 | 
			
		||||
  except DictNotFoundError:
 | 
			
		||||
   log.exception("Dictionary for language %s not found." % (dictionary,))
 | 
			
		||||
   wx_ui.dict_not_found_error()
 | 
			
		||||
   self.active = False
 | 
			
		||||
  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.get_text()
 | 
			
		||||
	def __init__(self, text, dictionary):
 | 
			
		||||
		super(spellChecker, self).__init__()
 | 
			
		||||
		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.checker = SpellChecker(languageHandler.curLang, filters=[tokenize.EmailFilter, tokenize.URLFilter])
 | 
			
		||||
			else:
 | 
			
		||||
				log.debug("Using language: %s" % (languageHandler.getLanguage(),))
 | 
			
		||||
				self.checker = SpellChecker(languageHandler.curLang, filters=[tokenize.EmailFilter, tokenize.URLFilter])
 | 
			
		||||
			self.checker.set_text(text)
 | 
			
		||||
		except DictNotFoundError:
 | 
			
		||||
			print "no dict"
 | 
			
		||||
			log.exception("Dictionary for language %s not found." % (dictionary,))
 | 
			
		||||
			wx_ui.dict_not_found_error()
 | 
			
		||||
			self.active = False
 | 
			
		||||
		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.get_text()
 | 
			
		||||
 | 
			
		||||
 def check(self):
 | 
			
		||||
  try:
 | 
			
		||||
   self.checker.next()
 | 
			
		||||
   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()
 | 
			
		||||
#  except AttributeError:
 | 
			
		||||
#   pass
 | 
			
		||||
	def check(self):
 | 
			
		||||
		try:
 | 
			
		||||
			self.checker.next()
 | 
			
		||||
			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()
 | 
			
		||||
 | 
			
		||||
 def ignore(self, ev):
 | 
			
		||||
  self.check()
 | 
			
		||||
	def ignore(self, ev):
 | 
			
		||||
		self.check()
 | 
			
		||||
 | 
			
		||||
 def ignoreAll(self, ev):
 | 
			
		||||
  self.checker.ignore_always(word=self.checker.word)
 | 
			
		||||
  self.check()
 | 
			
		||||
	def ignoreAll(self, ev):
 | 
			
		||||
		self.checker.ignore_always(word=self.checker.word)
 | 
			
		||||
		self.check()
 | 
			
		||||
 | 
			
		||||
 def replace(self, ev):
 | 
			
		||||
  self.checker.replace(self.dialog.get_selected_suggestion())
 | 
			
		||||
  self.check()
 | 
			
		||||
	def replace(self, ev):
 | 
			
		||||
		self.checker.replace(self.dialog.get_selected_suggestion())
 | 
			
		||||
		self.check()
 | 
			
		||||
 | 
			
		||||
 def replaceAll(self, ev):
 | 
			
		||||
  self.checker.replace_always(self.dialog.get_selected_suggestion())
 | 
			
		||||
  self.check()
 | 
			
		||||
	def replaceAll(self, ev):
 | 
			
		||||
		self.checker.replace_always(self.dialog.get_selected_suggestion())
 | 
			
		||||
		self.check()
 | 
			
		||||
@@ -1,15 +0,0 @@
 | 
			
		||||
# -*- coding: utf-8 -*-
 | 
			
		||||
import re
 | 
			
		||||
from enchant.tokenize import Filter
 | 
			
		||||
 | 
			
		||||
class TwitterFilter(Filter):
 | 
			
		||||
    """Filter skipping over twitter usernames and hashtags.
 | 
			
		||||
    This filter skips any words matching the following regular expression:
 | 
			
		||||
    ^[#@](\S){1, }$
 | 
			
		||||
    That is, any words that resemble users and hashtags.
 | 
			
		||||
    """
 | 
			
		||||
    _pattern = re.compile(r"^[#@](\S){1,}$")
 | 
			
		||||
    def _skip(self,word):
 | 
			
		||||
        if self._pattern.match(word):
 | 
			
		||||
            return True
 | 
			
		||||
        return False
 | 
			
		||||
@@ -18,62 +18,62 @@
 | 
			
		||||
############################################################
 | 
			
		||||
import wx
 | 
			
		||||
import application
 | 
			
		||||
 | 
			
		||||
class spellCheckerDialog(wx.Dialog):
 | 
			
		||||
 def __init__(self):
 | 
			
		||||
  super(spellCheckerDialog, self).__init__(None, 1)
 | 
			
		||||
  panel = wx.Panel(self)
 | 
			
		||||
  sizer = wx.BoxSizer(wx.VERTICAL)
 | 
			
		||||
  word = wx.StaticText(panel, -1, _(u"Misspelled word"))
 | 
			
		||||
  self.word = wx.TextCtrl(panel, -1)
 | 
			
		||||
  wordBox = wx.BoxSizer(wx.HORIZONTAL)
 | 
			
		||||
  wordBox.Add(word, 0, wx.ALL, 5)
 | 
			
		||||
  wordBox.Add(self.word, 0, wx.ALL, 5)
 | 
			
		||||
  context = wx.StaticText(panel, -1, _(u"Context"))
 | 
			
		||||
  self.context = wx.TextCtrl(panel, -1)
 | 
			
		||||
  contextBox = wx.BoxSizer(wx.HORIZONTAL)
 | 
			
		||||
  contextBox.Add(context, 0, wx.ALL, 5)
 | 
			
		||||
  contextBox.Add(self.context, 0, wx.ALL, 5)
 | 
			
		||||
  suggest = wx.StaticText(panel, -1, _(u"Suggestions"))
 | 
			
		||||
  self.suggestions = wx.ListBox(panel, -1, choices=[], style=wx.LB_SINGLE)
 | 
			
		||||
  suggestionsBox = wx.BoxSizer(wx.HORIZONTAL)
 | 
			
		||||
  suggestionsBox.Add(suggest, 0, wx.ALL, 5)
 | 
			
		||||
  suggestionsBox.Add(self.suggestions, 0, wx.ALL, 5)
 | 
			
		||||
  self.ignore = wx.Button(panel, -1, _(u"Ignore"))
 | 
			
		||||
  self.ignoreAll = wx.Button(panel, -1, _(u"Ignore all"))
 | 
			
		||||
  self.replace = wx.Button(panel, -1, _(u"Replace"))
 | 
			
		||||
  self.replaceAll = wx.Button(panel, -1, _(u"Replace all"))
 | 
			
		||||
  close = wx.Button(panel, wx.ID_CANCEL)
 | 
			
		||||
  btnBox = wx.BoxSizer(wx.HORIZONTAL)
 | 
			
		||||
  btnBox.Add(self.ignore, 0, wx.ALL, 5)
 | 
			
		||||
  btnBox.Add(self.ignoreAll, 0, wx.ALL, 5)
 | 
			
		||||
  btnBox.Add(self.replace, 0, wx.ALL, 5)
 | 
			
		||||
  btnBox.Add(self.replaceAll, 0, wx.ALL, 5)
 | 
			
		||||
  btnBox.Add(close, 0, wx.ALL, 5)
 | 
			
		||||
  sizer.Add(wordBox, 0, wx.ALL, 5)
 | 
			
		||||
  sizer.Add(contextBox, 0, wx.ALL, 5)
 | 
			
		||||
  sizer.Add(suggestionsBox, 0, wx.ALL, 5)
 | 
			
		||||
  sizer.Add(btnBox, 0, wx.ALL, 5)
 | 
			
		||||
  panel.SetSizer(sizer)
 | 
			
		||||
  self.SetClientSize(sizer.CalcMin())
 | 
			
		||||
	def __init__(self):
 | 
			
		||||
		super(spellCheckerDialog, self).__init__(None, 1)
 | 
			
		||||
		panel = wx.Panel(self)
 | 
			
		||||
		sizer = wx.BoxSizer(wx.VERTICAL)
 | 
			
		||||
		word = wx.StaticText(panel, -1, _(u"Misspelled word"))
 | 
			
		||||
		self.word = wx.TextCtrl(panel, -1)
 | 
			
		||||
		wordBox = wx.BoxSizer(wx.HORIZONTAL)
 | 
			
		||||
		wordBox.Add(word, 0, wx.ALL, 5)
 | 
			
		||||
		wordBox.Add(self.word, 0, wx.ALL, 5)
 | 
			
		||||
		context = wx.StaticText(panel, -1, _(u"Context"))
 | 
			
		||||
		self.context = wx.TextCtrl(panel, -1)
 | 
			
		||||
		contextBox = wx.BoxSizer(wx.HORIZONTAL)
 | 
			
		||||
		contextBox.Add(context, 0, wx.ALL, 5)
 | 
			
		||||
		contextBox.Add(self.context, 0, wx.ALL, 5)
 | 
			
		||||
		suggest = wx.StaticText(panel, -1, _(u"Suggestions"))
 | 
			
		||||
		self.suggestions = wx.ListBox(panel, -1, choices=[], style=wx.LB_SINGLE)
 | 
			
		||||
		suggestionsBox = wx.BoxSizer(wx.HORIZONTAL)
 | 
			
		||||
		suggestionsBox.Add(suggest, 0, wx.ALL, 5)
 | 
			
		||||
		suggestionsBox.Add(self.suggestions, 0, wx.ALL, 5)
 | 
			
		||||
		self.ignore = wx.Button(panel, -1, _(u"Ignore"))
 | 
			
		||||
		self.ignoreAll = wx.Button(panel, -1, _(u"Ignore all"))
 | 
			
		||||
		self.replace = wx.Button(panel, -1, _(u"Replace"))
 | 
			
		||||
		self.replaceAll = wx.Button(panel, -1, _(u"Replace all"))
 | 
			
		||||
		close = wx.Button(panel, wx.ID_CANCEL)
 | 
			
		||||
		btnBox = wx.BoxSizer(wx.HORIZONTAL)
 | 
			
		||||
		btnBox.Add(self.ignore, 0, wx.ALL, 5)
 | 
			
		||||
		btnBox.Add(self.ignoreAll, 0, wx.ALL, 5)
 | 
			
		||||
		btnBox.Add(self.replace, 0, wx.ALL, 5)
 | 
			
		||||
		btnBox.Add(self.replaceAll, 0, wx.ALL, 5)
 | 
			
		||||
		btnBox.Add(close, 0, wx.ALL, 5)
 | 
			
		||||
		sizer.Add(wordBox, 0, wx.ALL, 5)
 | 
			
		||||
		sizer.Add(contextBox, 0, wx.ALL, 5)
 | 
			
		||||
		sizer.Add(suggestionsBox, 0, wx.ALL, 5)
 | 
			
		||||
		sizer.Add(btnBox, 0, wx.ALL, 5)
 | 
			
		||||
		panel.SetSizer(sizer)
 | 
			
		||||
		self.SetClientSize(sizer.CalcMin())
 | 
			
		||||
 | 
			
		||||
	def get_response(self):
 | 
			
		||||
		return self.ShowModal()
 | 
			
		||||
 | 
			
		||||
 def get_response(self):
 | 
			
		||||
  return self.ShowModal()
 | 
			
		||||
	def set_title(self, title):
 | 
			
		||||
		return self.SetTitle(title)
 | 
			
		||||
 | 
			
		||||
 def set_title(self, title):
 | 
			
		||||
  return self.SetTitle(title)
 | 
			
		||||
	def set_word_and_suggestions(self, word, context, suggestions):
 | 
			
		||||
		self.word.SetValue(word)
 | 
			
		||||
		self.context.ChangeValue(context)
 | 
			
		||||
		self.suggestions.Set(suggestions)
 | 
			
		||||
		self.suggestions.SetFocus()
 | 
			
		||||
 | 
			
		||||
 def set_word_and_suggestions(self, word, context, suggestions):
 | 
			
		||||
  self.word.SetValue(word)
 | 
			
		||||
  self.context.ChangeValue(context)
 | 
			
		||||
  self.suggestions.Set(suggestions)
 | 
			
		||||
  self.suggestions.SetFocus()
 | 
			
		||||
 | 
			
		||||
 def get_selected_suggestion(self):
 | 
			
		||||
  return self.suggestions.GetStringSelection()
 | 
			
		||||
	def get_selected_suggestion(self):
 | 
			
		||||
		return self.suggestions.GetStringSelection()
 | 
			
		||||
 | 
			
		||||
def dict_not_found_error():
 | 
			
		||||
 wx.MessageDialog(None, _(u"An error has occurred. There are no dictionaries available for the selected language in {0}").format(application.name,), _(u"Error"), wx.ICON_ERROR).ShowModal()
 | 
			
		||||
	wx.MessageDialog(None, _(u"An error has occurred. There are no dictionaries available for the selected language in {0}").format(application.name,), _(u"Error"), wx.ICON_ERROR).ShowModal()
 | 
			
		||||
 | 
			
		||||
def finished():
 | 
			
		||||
 wx.MessageDialog(None, _(u"Spell check complete."), application.name, style=wx.OK).ShowModal()
 | 
			
		||||
	wx.MessageDialog(None, _(u"Spell check complete."), application.name, style=wx.OK).ShowModal()
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user