-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.py
executable file
·472 lines (406 loc) · 16.4 KB
/
interface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#!/usr/bin/env python3
"""
This is the interface of the VocVoc.
"""
# PyQt4
from PyQt4.QtGui import QDialog, QPushButton, QListWidget, QLineEdit,\
QStatusBar, QLabel, QVBoxLayout, QHBoxLayout, QFileDialog,\
QListWidgetItem, QTextEdit, QMessageBox, QApplication
from PyQt4.QtCore import QFile, Qt, pyqtSignal
from PyQt4.phonon import Phonon
# re
from re import compile as reCompile
# logging
from logging import DEBUG, getLogger
# os
from os.path import basename, join as pJoin
# tempfile
from tempfile import NamedTemporaryFile
# url
from urllib.request import urlopen
from urllib.error import HTTPError
# glob
from glob import glob
# config
from config import __dir__
# SpellChecker
from spellchecker import WordModel, SpellChecker
# misc
from misc import *
__all__ = ['VocDialog', 'App']
class tabEnabledLineEdit(QLineEdit) :
keyCode = { 16777249: 'Ctrl',
78 : 'n',
80 : 'p'
}
ctrlN = pyqtSignal()
ctrlP = pyqtSignal()
def __init__(self, parent=None) :
self.logger = getLogger('VocVoc.VocDialog.lineEdit')
self.info = self.logger.info
self.debug = self.logger.debug
self.keys = list()
super(tabEnabledLineEdit, self).__init__(parent)
def keyPressEvent(self, event) :
keys = self.keys
keyCode = self.keyCode
key = event.key()
if key in keyCode.keys() :
keys.append(keyCode[key])
self.debug('Key pressed : {}.'.format(key))
if 'Ctrl' in keys :
if 'n' in keys :
self.ctrlN.emit()
self.debug('Ctrl and n pressed.')
elif 'p' in keys :
self.ctrlP.emit()
self.debug('Ctrl and p pressed.')
super(tabEnabledLineEdit, self).keyPressEvent(event)
def keyReleaseEvent(self, event) :
key = event.key()
keyCode = self.keyCode
if key in keyCode.keys() :
self.keys.remove(keyCode[key])
self.debug('Key released : {}.'.format(key))
super(tabEnabledLineEdit, self).keyReleaseEvent(event)
class VocDialog(QDialog) :
"""This is the dialog which presents the interface and organise everything."""
MAGICWORD = 'CHANGEME'
findDight = reCompile(r'\d+')
baseURL = 'http://www.gstatic.com/dictionary/static/sounds/de/0/CHANGEME.mp3'
def __init__(self, autoProxy=False, parent=None) :
super(VocDialog, self).__init__(parent)
self.logger = getLogger('VocVoc.VocDialog')
self.info = self.logger.info
self.warn = self.logger.warn
self.debug = self.logger.debug
if autoProxy :
self.info('Starting VocDialog with autoProxy.')
else :
self.info('Starting VocDialog without autoProxy.')
self.mediaObeject = Phonon.createPlayer(Phonon.MusicCategory, Phonon.MediaSource(''))
self.setupUi()
self.connect()
self.initCountWord()
self.candidates = None
self.autoProxy = autoProxy
self.spellChecker = SpellChecker()
self.correct = self.spellChecker.correct
self.corpusDir = self.spellChecker.corpusDir
self.info('VocDialog started.')
def keyPressEvent(self, event) :
self.debug('Key is {}.'.format(event.key()))
super(VocDialog, self).keyPressEvent(event)
def resizeEvent(self, event) :
self.debug("Resized to {}.".format(self.size()))
super(VocDialog, self).resizeEvent(event)
def initCountWord(self) :
"""
The first one is a count about how many time the input is wrong.
WRONG : Not collected in or can be corrected by the wordModel.
The second one is the last time's wrong input.
"""
self.countWord = [0, '']
def setupUi(self) :
"Setup the UI."
self.info('Seting up the UI.')
self.fileDialog = QFileDialog()
self.fileDialog.setFileMode(QFileDialog.AnyFile)
self.fileDialog.setViewMode(QFileDialog.Detail)
self.loadButton = QPushButton( r'Open/New :', self)
self.loadButton.setAutoDefault(False)
self.textList = QListWidget(self)
self.inputLine = tabEnabledLineEdit(self)
self.toggleButton = QPushButton(r'Show/Hide', self)
self.toggleButton.setAutoDefault(False)
self.toggleButton.setCheckable(True)
self.textLabel = QLabel()
self.hBox = QHBoxLayout()
self.hBox.addWidget(self.inputLine)
self.hBox.addWidget(self.toggleButton)
self.statusBar = QStatusBar(self)
msg = 'Hello World! I love YOU!!!'
self.statusBar.showMessage(msg, 5000)
vBox = QVBoxLayout()
items = [self.loadButton, self.textList, self.hBox, self.statusBar]
for item in items :
try :
vBox.addWidget(item)
except :
vBox.addLayout(item)
self.textViewer = QTextEdit()
self.textViewer.setHidden(True)
self.textViewer.setReadOnly(True)
HBox = QHBoxLayout()
items = [vBox, self.textViewer]
for item in items :
try :
HBox.addWidget(item)
except :
HBox.addLayout(item)
self.setLayout(HBox)
self.resize(350, 500)
self.setWindowTitle("VocVoc -- Your Vocabulary Helper")
self.info('UI is set up now.')
def connect(self) :
"Connect signals and slots in the UI."
self.info('Connecting signals and slots.')
self.loadButton.clicked.connect(self.loadFile)
self.inputLine.returnPressed.connect(self.enteredText)
self.inputLine.ctrlN.connect(self.completeHandler)
self.inputLine.ctrlP.connect(lambda : self.completeHandler(False))
self.textList.itemActivated.connect(self.itemActivated)
self.toggleButton.clicked.connect(self.toggleViewer)
if self.logger.isEnabledFor(DEBUG) :
self.mediaObeject.stateChanged.connect( self.errorState )
self.info('Signals and slots connected.')
def errorState(self, state) :
errorStates = {
0: 'Loading',
1: 'Stopped',
2: 'Playing',
3: 'Buffering',
4: 'Paused',
5: 'Error'
}
msg ='{} state in Phonon!'.format( errorStates[state])
self.info(self.mediaObeject.errorType())
if state == 5 :
self.warn(msg)
else :
self.info(msg)
def itemActivated(self, item) :
row = self.textList.row(item)
text = item.text()
if not text.startswith('#') :
self.pronounce(item.text())
self.findWord(text)
if row+1 != self.textList.count() :
self.debug('NOT last row!')
self.textList.setCurrentRow(row+1)
else :
self.debug('Last row!')
def toggleViewer(self) :
if self.textViewer.isHidden() :
self.resize(700, 500)
self.textViewer.show()
else :
self.textViewer.hide()
self.resize(350, 500)
def backAndForward(self, forward=True) :
inputLine = self.inputLine
word = inputLine.text()
setText = inputLine.setText
candidates = self.candidates
count = len(candidates)
try :
position = candidates.index(word)
self.debug('Position found.')
except :
position = None
self.debug('Position not found.')
if forward :
if position is None or position == count - 1 : # At end
position = -1
setText( candidates[position+1] )
else :
if position is None or position == 0 :
position = count
setText( candidates[position-1] )
def completeHandler(self, goNext=True) :
inputLine = self.inputLine
candidates = self.candidates
word = inputLine.text()
if candidates :
self.backAndForward(goNext)
def play(self, path) :
self.mediaObeject.setCurrentSource(Phonon.MediaSource(path))
self.mediaObeject.play()
def pronounce(self, word) :
self.info('Preparing the url to pronounce.')
url = self.baseURL.replace(self.MAGICWORD, word)
if not self.autoProxy :
self.debug('Without the autoProxy, play it using the url as the source.')
self.play(url)
else :
self.info('With the autoProxy, play it after downloading the file.')
try : # May happen HTTPError.
resource = urlopen(url).read()
tempFile = NamedTemporaryFile()
tempFile.write(resource)
self.play(tempFile.name)
except HTTPError as error :
self.warn(repr(error))
self.warn('Pronounciation FAILED.')
self.info('Pronounciation ended.')
def findWord(self, word) :
self.info('Finding word in the text file.')
textViewer = self.textViewer
if textViewer.isHidden() :
return
else :
pass
limit = 5
contexts = list()
textLines = list()
corpuses = glob(''.join([self.corpusDir, '/*']))
self.debug('Found corpuses : {}.'.format(corpuses))
textViewer.clear()
for corpus in corpuses :
textLines.append(locateWord(corpus, word))
for textLine in textLines :
text, lines = textLine[0], textLine[1]
title = ''.join( ['Title : ', basename(text[-1])] )
if lines :
for line in lines :
wantedLines = text[line-limit: line+limit]
#cleanLines = map(self.replace, wantedLines)
context = ''.join(wantedLines)
context = context.replace(word, ' '.join(['*', word, '*']))
context = context.replace('\n\n', self.MAGICWORD)
context = context.replace('\n', ' ')
context = context.replace(self.MAGICWORD, '\n\n')
contexts.append(''.join([title, '\n', context, '\n\n']))
if contexts :
for context in contexts :
textViewer.append(context)
else :
textViewer.append('Sorry, {} not found.'.format(word))
self.info('Word found and showed in the textViewer.')
def wordCount(self, word=None) :
"""
This function uses self.countWord to decide whether record and pronounce the input or not.
RECORD : Add the input into the textList and write it into the file.
If the word itself is correct, return True.
Or if a wrong input were entered twice, return True.
Otherwise with a one-time-entered wrong input, return False.
"""
if self.countWord[0] == 0 : # The word is correct.
self.countWord[1] = ''
return True
elif self.countWord[0] == 1 :
msg = 'Maybe the word is WRONG? Playing beep and saving the word.'
self.debug(msg)
self.countWord[1] = word
self.play('beep.mp3')
return False
elif self.countWord[0] == 2 :
if word != self.countWord[1] : # Different word.
self.debug('DIFEFRENT WORD.')
self.countWord[0] = 1 # Check again.
self.countWord[1] = word # Update it.
self.play('beep.mp3')
return False
else :
self.countWord[0] = 0
self.countWord[1] = ''
return True
else :
self.countWord[0] = 0
def checkWord(self, word) :
statusBar = self.statusBar
showMessage = statusBar.showMessage
candidates = self.correct(word)
if candidates is None : # Not collected.
self.countWord[0] += 1
showMessage('Are you sure?', 3000)
elif candidates[0] != word : # Can be corrected.
self.countWord[0] += 1
self.candidates = candidates
msg = 'Do you mean {} ?'.format(' ,'.join(candidates))
showMessage(msg, 5000)
else : # Collected in the wordModel.
self.findWord(word)
self.countWord[0] = 0
self.debug('Word collected in the wordModel.')
return True
msg = 'wrongTime = {} with the word {}.'.format(self.countWord[0], word)
self.logger.debug(msg)
return self.wordCount(word)
def addText(self, text) :
self.info('Starting to add text.')
textList = self.textList
if text.startswith('#') : # It is a comment.
pass
else : # It is a word.
if self.checkWord(text) :
self.pronounce(text)
else : # self.checkWord(text) return False
return
self.inputLine.clear()
textList.addItem(text)
self.statusBar.clearMessage()
textList.setCurrentRow( textList.count() - 1 )
try : # With the try statement, it can be used as a pronunciation helper.
flush(self.filePath, text)
except Exception :
self.debug('Using this freely without writing to a file as a pronunciation helper.')
self.info('Text added.')
def enteredText(self) :
"Get the text from the input line and add it to the file and the list."
self.info('Adding text to textList and the file.')
textList = self.textList
text = self.inputLine.text().strip().lower()
self.debug( 'Input is {}.'.format(text) )
self.addText(text)
self.info('Text added.')
def loadFile(self) :
"Open the file dialog to select the file and try to start."
# Open the file dialog.
logger = getLogger('VocVoc.VocDialog.loadFile')
info = logger.info
debug = logger.debug
debug('Preparing to load file.')
textList = self.textList
if ( self.fileDialog.exec() ) :
debug('Dialog executed sucessfully.')
filePath = self.fileDialog.selectedFiles()[0]
fileName = basename(filePath)
# Create or read file.
try :
with open(filePath, 'r+') as textFile :
debug('File exists, openning up.')
writenText = textFile.read()
writenText = writenText.splitlines()
textList.clear()
textList.addItems( writenText )
if not 'end' in writenText[-1].strip().lower() :
textList.setCurrentRow( len(writenText)-1 )
else :
textList.setCurrentRow( 0 )
debug('Added items to list and set current row to the last row.')
except IOError as error : # File does not exist. We create one.
debug('File does not exist. Trying to find the dight in the name.')
listNumber = self.findDight.search(fileName)
if listNumber is None : # No number found in the text.
logger.warn('Dight not found in the filename. Try again.')
msg = 'No number found in the file name.\nPlease try again.'
QMessageBox.warning(self, 'List number NOT found.',
msg,
QMessageBox.Ok)
return msg
else : # No existing file but found the number in the file name.
debug('Dight Found. Creating file and adding first line.')
with open(filePath, 'x') as textFile :
firstLine = ''.join( ['# list ' ,str( listNumber.group() )] ) # Cannot put '\n' here.
textFile.write( ''.join([firstLine ,'\n']) )
textList.clear()
textList.addItem(firstLine) # Otherwise there would be a new line in the list.
debug('Set inputLine to write-enabled.')
self.inputLine.setReadOnly(False)
debug('Pass textFile to the dialog')
self.filePath = filePath
info('File loaded.')
def App(autoProxy=False) :
from sys import argv, exit
app = QApplication(argv)
app.setApplicationName(r"TheDevil's VocVoc")
dialog = VocDialog(autoProxy=autoProxy)
dialog.show()
exit( app.exec_() )
if __name__ == '__main__' :
# this is why I have VocVoc.py
#from VocVoc import getLogger as myLogger
#myLogger()
#App()
pass