1

Allow text word lists to be imported

Former-commit-id: ac96ec75dd83094a5dd1455a77f2d0fb68bed0b7
This commit is contained in:
Alex Yatskov 2014-12-24 17:26:05 +09:00
parent 1d5b9f8234
commit 1e2f0e51a2
2 changed files with 22 additions and 4 deletions

View File

@ -72,7 +72,8 @@ class MainWindowReader(QtGui.QMainWindow, gen.reader_ui.Ui_MainWindowReader):
self.actionFind.triggered.connect(self.onActionFind) self.actionFind.triggered.connect(self.onActionFind)
self.actionFindNext.triggered.connect(self.onActionFindNext) self.actionFindNext.triggered.connect(self.onActionFindNext)
self.actionHomepage.triggered.connect(self.onActionHomepage) self.actionHomepage.triggered.connect(self.onActionHomepage)
self.actionImportKindleDeck.triggered.connect(self.onActionImportKindleDeck) self.actionKindleDeck.triggered.connect(self.onActionKindleDeck)
self.actionWordList.triggered.connect(self.onActionWordList)
self.actionOpen.triggered.connect(self.onActionOpen) self.actionOpen.triggered.connect(self.onActionOpen)
self.actionPreferences.triggered.connect(self.onActionPreferences) self.actionPreferences.triggered.connect(self.onActionPreferences)
self.actionToggleWrap.toggled.connect(self.onActionToggleWrap) self.actionToggleWrap.toggled.connect(self.onActionToggleWrap)
@ -183,7 +184,7 @@ class MainWindowReader(QtGui.QMainWindow, gen.reader_ui.Ui_MainWindowReader):
self.openFile(filename) self.openFile(filename)
def onActionImportKindleDeck(self): def onActionKindleDeck(self):
filename = QtGui.QFileDialog.getOpenFileName( filename = QtGui.QFileDialog.getOpenFileName(
parent=self, parent=self,
caption='Select a Kindle deck to import', caption='Select a Kindle deck to import',
@ -194,6 +195,17 @@ class MainWindowReader(QtGui.QMainWindow, gen.reader_ui.Ui_MainWindowReader):
self.importWordList(words) self.importWordList(words)
def onActionWordList(self):
filename = QtGui.QFileDialog.getOpenFileName(
parent=self,
caption='Select a word list file to import',
filter='Text files (*.txt);;All files (*.*)'
)
if filename:
words = reader_util.extractWordList(filename)
self.importWordList(words)
def onActionPreferences(self): def onActionPreferences(self):
dialog = preferences.DialogPreferences(self, self.preferences, self.anki) dialog = preferences.DialogPreferences(self, self.preferences, self.anki)
if dialog.exec_() == QtGui.QDialog.Accepted: if dialog.exec_() == QtGui.QDialog.Accepted:

View File

@ -18,6 +18,7 @@
from PyQt4 import QtGui from PyQt4 import QtGui
import re import re
import codecs
import sqlite3 import sqlite3
@ -249,5 +250,10 @@ def extractKindleDeck(filename):
return words return words
def importWordList(self, words): def extractWordList(filename):
pass words = list()
with codecs.open(unicode(filename), 'rb', 'utf-8') as fp:
words = re.split('[;,\s]', fp.read())
return filter(None, words)