From 6a5833ddc71df45aa3fdd80b7f2720de7d5efb4d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 19 Apr 2014 21:43:23 +0530 Subject: [PATCH] Manage user dictionaries: Add a button to import a list of words in bulk into the user dictionary --- src/calibre/gui2/tweak_book/spell.py | 43 ++++++++++++++++++++++++++-- src/calibre/spell/dictionary.py | 5 +++- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/calibre/gui2/tweak_book/spell.py b/src/calibre/gui2/tweak_book/spell.py index 5a78da6d45..e2c20c76b6 100644 --- a/src/calibre/gui2/tweak_book/spell.py +++ b/src/calibre/gui2/tweak_book/spell.py @@ -15,7 +15,7 @@ from PyQt4.Qt import ( QStackedLayout, QLabel, QVBoxLayout, QWidget, QPushButton, QIcon, QDialogButtonBox, QLineEdit, QDialog, QToolButton, QFormLayout, QHBoxLayout, pyqtSignal, QAbstractTableModel, QModelIndex, QTimer, QTableView, QCheckBox, - QComboBox, QListWidget, QListWidgetItem, QInputDialog) + QComboBox, QListWidget, QListWidgetItem, QInputDialog, QPlainTextEdit) from calibre.constants import __appname__, plugins from calibre.ebooks.oeb.polish.spell import replace_word, get_all_words, merge_locations @@ -174,6 +174,9 @@ class ManageUserDictionaries(Dialog): # {{{ b.clicked.connect(self.remove_word) b.setIcon(QIcon(I('minus.png'))) h.addWidget(b) + self.import_words_button = b = QPushButton(_('&Import list of words'), self) + b.clicked.connect(self.import_words) + l.addWidget(b) self.show_current_dictionary() @@ -289,6 +292,42 @@ class ManageUserDictionaries(Dialog): # {{{ if idx > -1: self.words.scrollToItem(self.words.item(idx)) + def import_words(self): + d = QDialog(self) + d.l = l = QFormLayout(d) + d.setWindowTitle(_('Import list of words')) + d.w = w = QPlainTextEdit(d) + l.addRow(QLabel(_('Enter a list of words, one per line'))) + l.addRow(w) + d.b = b = QPushButton(_('Paste from clipboard')) + l.addRow(b) + b.clicked.connect(w.paste) + d.la = la = QLabel(_('Words in the user dictionary must have an associated language. Choose the language below:')) + la.setWordWrap(True) + l.addRow(la) + d.le = le = LanguagesEdit(d) + lc = canonicalize_lang(get_lang()) + if lc: + le.lang_codes = [lc] + l.addRow(_('&Language:'), le) + d.bb = bb = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel) + l.addRow(bb) + bb.accepted.connect(d.accept), bb.rejected.connect(d.reject) + + if d.exec_() != d.Accepted: + return + lc = le.lang_codes + if not lc: + return error_dialog(self, _('Must specify language'), _( + 'You must specify a language to import words'), show=True) + words = set(filter(None, [x.strip() for x in unicode(w.toPlainText()).splitlines()])) + lang = lc[0] + words = {(w, lang) for w in words} - self.current_dictionary.words + if dictionaries.add_to_user_dictionary(self.current_dictionary.name, words, None): + dictionaries.clear_caches() + self.show_current_dictionary() + self.dictionaries_changed = True + def remove_word(self): words = {i.data(Qt.UserRole).toPyObject() for i in self.words.selectedItems()} if words: @@ -1064,5 +1103,5 @@ def find_next(word, locations, current_editor, current_editor_name, if __name__ == '__main__': app = QApplication([]) dictionaries.initialize() - SpellCheck.test() + ManageUserDictionaries.test() del app diff --git a/src/calibre/spell/dictionary.py b/src/calibre/spell/dictionary.py index fbd006b7ab..a512653ee3 100644 --- a/src/calibre/spell/dictionary.py +++ b/src/calibre/spell/dictionary.py @@ -249,7 +249,10 @@ class Dictionaries(object): if ud is None: raise ValueError('Cannot add to the dictionary named: %s as no such dictionary exists' % name) wl = len(ud.words) - ud.words.add((word, locale.langcode)) + if isinstance(word, (set, frozenset)): + ud.words |= word + else: + ud.words.add((word, locale.langcode)) if len(ud.words) > wl: self.save_user_dictionaries() self.word_cache.pop((word, locale), None)