From 567e612c72b181ddc672d60d0d5632695181bb37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Cort=C3=A9z?= Date: Sun, 16 Nov 2014 22:30:58 -0600 Subject: [PATCH] Spell checker is refactored in an MVC pattern and now works for tweets --- src/controller/messages.py | 10 ++- src/extra/SpellChecker/__init__.py | 4 + src/extra/SpellChecker/spellchecker.py | 105 +++++++------------------ src/extra/SpellChecker/wxUI.py | 105 ------------------------- src/extra/SpellChecker/wx_ui.py | 78 ++++++++++++++++++ 5 files changed, 119 insertions(+), 183 deletions(-) delete mode 100644 src/extra/SpellChecker/wxUI.py create mode 100644 src/extra/SpellChecker/wx_ui.py diff --git a/src/controller/messages.py b/src/controller/messages.py index 1854c349..9a085f91 100644 --- a/src/controller/messages.py +++ b/src/controller/messages.py @@ -4,14 +4,14 @@ import output import url_shortener import sound from wxUI.dialogs import message, urlList -from extra import translator +from extra import translator, SpellChecker from twitter import utils class tweet(object): def __init__(self, session): super(tweet, self).__init__() self.message = message.tweet(_(u"Write the tweet here"), _(u"tweet - 0 characters"), "") -# widgetUtils.connect_event(self.message.spellcheck, widgetUtils.BUTTON_PRESSED, self.spellcheck) + widgetUtils.connect_event(self.message.spellcheck, widgetUtils.BUTTON_PRESSED, self.spellcheck) # widgetUtils.connect_event(self.message.attach, widgetUtils.BUTTON_PRESSED, self.attach) widgetUtils.connect_event(self.message.text, widgetUtils.ENTERED_TEXT, self.text_processor) widgetUtils.connect_event(self.message.shortenButton, widgetUtils.BUTTON_PRESSED, self.shorten) @@ -71,3 +71,9 @@ class tweet(object): self.message.disable_button("unshortenButton") if len(self.message.get_text()) > 140: sound.player.play("max_length.ogg") + + def spellcheck(self, event=None): + text = self.message.get_text() + checker = SpellChecker.spellchecker.spellChecker(text, "") + if hasattr(checker, "fixed_text"): + self.message.set_text(checker.fixed_text) \ No newline at end of file diff --git a/src/extra/SpellChecker/__init__.py b/src/extra/SpellChecker/__init__.py index e69de29b..fedad8a8 100644 --- a/src/extra/SpellChecker/__init__.py +++ b/src/extra/SpellChecker/__init__.py @@ -0,0 +1,4 @@ +import spellchecker +import platform +if platform.system() == "Windows": + from wx_ui import * \ No newline at end of file diff --git a/src/extra/SpellChecker/spellchecker.py b/src/extra/SpellChecker/spellchecker.py index 801559d5..db40200f 100644 --- a/src/extra/SpellChecker/spellchecker.py +++ b/src/extra/SpellChecker/spellchecker.py @@ -1,105 +1,58 @@ # -*- coding: utf-8 -*- -############################################################ -# Copyright (c) 2013, 2014 Manuel Eduardo Cortéz Vallejo -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -# -############################################################ -import wx +import wx_ui +import widgetUtils import output import config import languageHandler from enchant.checker import SpellChecker from enchant.errors import DictNotFoundError -class spellCheckerDialog(wx.Dialog): +class spellChecker(object): def __init__(self, text, dictionary): - super(spellCheckerDialog, self).__init__(None, 1) + super(spellChecker, self).__init__() + self.active = True try: - if config.main["general"]["language"] == "system": self.checker = SpellChecker() + if config.app["app-settings"]["language"] == "system": self.checker = SpellChecker() else: self.checker = SpellChecker(languageHandler.getLanguage()) self.checker.set_text(text) except DictNotFoundError: - wx.MessageDialog(None, _(u"A bug has happened. There are no dictionaries available for the selected language in TW Blue"), _(u"Error"), wx.ICON_ERROR).ShowModal() - self.Destroy() - panel = wx.Panel(self) - sizer = wx.BoxSizer(wx.VERTICAL) - word = wx.StaticText(panel, -1, _(u"Mis-spelled word")) - self.word = wx.TextCtrl(panel, -1) - wordBox = wx.BoxSizer(wx.HORIZONTAL) - wordBox.Add(word) - wordBox.Add(self.word) - context = wx.StaticText(panel, -1, _(u"Context")) - self.context = wx.TextCtrl(panel, -1) - contextBox = wx.BoxSizer(wx.HORIZONTAL) - contextBox.Add(context) - contextBox.Add(self.context) - 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) - suggestionsBox.Add(self.suggestions) - ignore = wx.Button(panel, -1, _(u"Ignore")) - self.Bind(wx.EVT_BUTTON, self.onIgnore, ignore) - ignoreAll = wx.Button(panel, -1, _(u"Ignore all")) - self.Bind(wx.EVT_BUTTON, self.onIgnoreAll, ignoreAll) - replace = wx.Button(panel, -1, _(u"Replace")) - self.Bind(wx.EVT_BUTTON, self.onReplace, replace) - replaceAll = wx.Button(panel, -1, _(u"Replace all")) - self.Bind(wx.EVT_BUTTON, self.onReplaceAll, replaceAll) - close = wx.Button(panel, wx.ID_CANCEL) - btnBox = wx.BoxSizer(wx.HORIZONTAL) - btnBox.Add(ignore) - btnBox.Add(ignoreAll) - btnBox.Add(replace) - btnBox.Add(replaceAll) - btnBox.Add(close) - sizer.Add(wordBox) - sizer.Add(contextBox) - sizer.Add(suggestionsBox) - sizer.Add(btnBox) - panel.SetSizerAndFit(sizer) - self.check() + wx_ui.dict_not_found_error() + self.active = False + if self.active == True: + 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() + if self.dialog.get_response() == widgetUtils.OK: + self.fixed_text = self.checker.get_text() def check(self): try: self.checker.next() textToSay = _(u"Mis-spelled word: %s") % (self.checker.word,) context = u"... %s %s %s" % (self.checker.leading_context(10), self.checker.word, self.checker.trailing_context(10)) - self.SetTitle(textToSay) + self.dialog.set_title(textToSay) output.speak(textToSay) - self.word.SetValue(self.checker.word) - self.context.ChangeValue(context) - self.suggestions.Set(self.checker.suggest()) - self.suggestions.SetFocus() + self.dialog.set_word_and_suggestions(word=self.checker.word, context=context, suggestions=self.checker.suggest()) except StopIteration: - wx.MessageDialog(self, _(u"The spelling review has finished."), _("Finished"), style=wx.OK).ShowModal() - self.EndModal(wx.ID_OK) - except AttributeError: - pass + wx_ui.finished() + self.dialog.Destroy() +# except AttributeError: +# pass - def onIgnore(self, ev): + def ignore(self, ev): self.check() - def onIgnoreAll(self, ev): + def ignoreAll(self, ev): self.checker.ignore_always(word=self.checker.word) self.check() - def onReplace(self, ev): - self.checker.replace(self.suggestions.GetStringSelection()) + def replace(self, ev): + self.checker.replace(self.dialog.get_selected_suggestion()) self.check() - def onReplaceAll(self, ev): - self.checker.replace_always(self.suggestions.GetStringSelection()) + def replaceAll(self, ev): + self.checker.replace_always(self.dialog.get_selected_suggestion()) self.check() \ No newline at end of file diff --git a/src/extra/SpellChecker/wxUI.py b/src/extra/SpellChecker/wxUI.py deleted file mode 100644 index 801559d5..00000000 --- a/src/extra/SpellChecker/wxUI.py +++ /dev/null @@ -1,105 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################ -# Copyright (c) 2013, 2014 Manuel Eduardo Cortéz Vallejo -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -# -############################################################ -import wx -import output -import config -import languageHandler -from enchant.checker import SpellChecker -from enchant.errors import DictNotFoundError - -class spellCheckerDialog(wx.Dialog): - def __init__(self, text, dictionary): - super(spellCheckerDialog, self).__init__(None, 1) - try: - if config.main["general"]["language"] == "system": self.checker = SpellChecker() - else: self.checker = SpellChecker(languageHandler.getLanguage()) - self.checker.set_text(text) - except DictNotFoundError: - wx.MessageDialog(None, _(u"A bug has happened. There are no dictionaries available for the selected language in TW Blue"), _(u"Error"), wx.ICON_ERROR).ShowModal() - self.Destroy() - panel = wx.Panel(self) - sizer = wx.BoxSizer(wx.VERTICAL) - word = wx.StaticText(panel, -1, _(u"Mis-spelled word")) - self.word = wx.TextCtrl(panel, -1) - wordBox = wx.BoxSizer(wx.HORIZONTAL) - wordBox.Add(word) - wordBox.Add(self.word) - context = wx.StaticText(panel, -1, _(u"Context")) - self.context = wx.TextCtrl(panel, -1) - contextBox = wx.BoxSizer(wx.HORIZONTAL) - contextBox.Add(context) - contextBox.Add(self.context) - 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) - suggestionsBox.Add(self.suggestions) - ignore = wx.Button(panel, -1, _(u"Ignore")) - self.Bind(wx.EVT_BUTTON, self.onIgnore, ignore) - ignoreAll = wx.Button(panel, -1, _(u"Ignore all")) - self.Bind(wx.EVT_BUTTON, self.onIgnoreAll, ignoreAll) - replace = wx.Button(panel, -1, _(u"Replace")) - self.Bind(wx.EVT_BUTTON, self.onReplace, replace) - replaceAll = wx.Button(panel, -1, _(u"Replace all")) - self.Bind(wx.EVT_BUTTON, self.onReplaceAll, replaceAll) - close = wx.Button(panel, wx.ID_CANCEL) - btnBox = wx.BoxSizer(wx.HORIZONTAL) - btnBox.Add(ignore) - btnBox.Add(ignoreAll) - btnBox.Add(replace) - btnBox.Add(replaceAll) - btnBox.Add(close) - sizer.Add(wordBox) - sizer.Add(contextBox) - sizer.Add(suggestionsBox) - sizer.Add(btnBox) - panel.SetSizerAndFit(sizer) - self.check() - - def check(self): - try: - self.checker.next() - textToSay = _(u"Mis-spelled word: %s") % (self.checker.word,) - context = u"... %s %s %s" % (self.checker.leading_context(10), self.checker.word, self.checker.trailing_context(10)) - self.SetTitle(textToSay) - output.speak(textToSay) - self.word.SetValue(self.checker.word) - self.context.ChangeValue(context) - self.suggestions.Set(self.checker.suggest()) - self.suggestions.SetFocus() - except StopIteration: - wx.MessageDialog(self, _(u"The spelling review has finished."), _("Finished"), style=wx.OK).ShowModal() - self.EndModal(wx.ID_OK) - except AttributeError: - pass - - def onIgnore(self, ev): - self.check() - - def onIgnoreAll(self, ev): - self.checker.ignore_always(word=self.checker.word) - self.check() - - def onReplace(self, ev): - self.checker.replace(self.suggestions.GetStringSelection()) - self.check() - - def onReplaceAll(self, ev): - self.checker.replace_always(self.suggestions.GetStringSelection()) - self.check() \ No newline at end of file diff --git a/src/extra/SpellChecker/wx_ui.py b/src/extra/SpellChecker/wx_ui.py new file mode 100644 index 00000000..9e88aa0e --- /dev/null +++ b/src/extra/SpellChecker/wx_ui.py @@ -0,0 +1,78 @@ +# -*- coding: utf-8 -*- +############################################################ +# Copyright (c) 2013, 2014 Manuel Eduardo Cortéz Vallejo +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +############################################################ +import wx + +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"Mis-spelled word")) + self.word = wx.TextCtrl(panel, -1) + wordBox = wx.BoxSizer(wx.HORIZONTAL) + wordBox.Add(word) + wordBox.Add(self.word) + context = wx.StaticText(panel, -1, _(u"Context")) + self.context = wx.TextCtrl(panel, -1) + contextBox = wx.BoxSizer(wx.HORIZONTAL) + contextBox.Add(context) + contextBox.Add(self.context) + 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) + suggestionsBox.Add(self.suggestions) + 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) + btnBox.Add(self.ignoreAll) + btnBox.Add(self.replace) + btnBox.Add(self.replaceAll) + btnBox.Add(close) + sizer.Add(wordBox) + sizer.Add(contextBox) + sizer.Add(suggestionsBox) + sizer.Add(btnBox) + panel.SetSizerAndFit(sizer) + + + def get_response(self): + return self.ShowModal() + + 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 get_selected_suggestion(self): + return self.suggestions.GetStringSelection() + +def dict_not_found_error(): + wx.MessageDialog(None, _(u"A bug has happened. There are no dictionaries available for the selected language in TW Blue"), _(u"Error"), wx.ICON_ERROR).ShowModal() + +def finished(): + wx.MessageDialog(None, _(u"The spelling review has finished."), _("Finished"), style=wx.OK).ShowModal()