/
code.py
222 lines (176 loc) · 6.84 KB
/
code.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
'''
This is an app to learn new languages through YouTube.
'''
from PyQt5.QtWidgets import (QWidget, QToolTip, QGridLayout, QHBoxLayout,
QPushButton, QApplication, QMessageBox)
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import (QWidget, QSlider, QTextEdit,
QLabel, QApplication)
from PyQt5.QtCore import Qt, QObject, pyqtSignal, QTimer
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QPushButton, QAction, QLineEdit, QMessageBox
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
from random import randint
from bisect import bisect_left
import sys
import srt
import mpv
import nltk
import locale
import pdb
from docopt import docopt
class Communicate(QObject):
delay_quiz = pyqtSignal()
quiz = pyqtSignal()
class Example(QWidget):
def __init__(self, filename, sub, blacklist):
super().__init__()
self.blacklist = blacklist
self.correct = []
self.incorrect = []
self.line = ''
self.finished = False
with open(sub) as f:
self.subs = list(srt.parse(f.read()))
self.initUI()
self.initVideo(filename, sub)
def initUI(self):
self.sub_text = QTextEdit(self, readOnly=True)
self.sub_text.setCurrentFont(QFont('SansSerif', 20))
self.toggle_play = QPushButton('Pause', self)
self.toggle_play.clicked.connect(self.togglePlay)
skip = QPushButton('Skip', self)
submit = QPushButton('Submit', self)
submit.clicked.connect(self.submit)
self.seek = QSlider(Qt.Horizontal, self)
self.seek.setFocusPolicy(Qt.NoFocus)
self.guess = QLineEdit(self)
self.guess.returnPressed.connect(self.submit)
controls = QHBoxLayout()
controls.setStretch(0, 2)
controls.setStretch(1, 2)
controls.setStretch(2, 6)
controls.addWidget(self.toggle_play)
controls.addWidget(skip)
controls.addWidget(self.seek)
grid = QGridLayout()
grid.setSpacing(10)
grid.addWidget(self.sub_text, 0, 0, 2, 4)
grid.addWidget(self.guess, 2, 0, 1, 3)
grid.addWidget(submit, 2, 3, 1, 1)
grid.addLayout(controls, 3, 0, 1, 4)
self.c = Communicate()
self.c.quiz.connect(self.quiz)
self.c.delay_quiz.connect(self.delay_quiz)
self.setLayout(grid)
self.setWindowTitle('HackCMU')
self.setGeometry(200, 200, 600, 400)
self.show()
def initVideo(self, filename, sub):
locale.setlocale(locale.LC_NUMERIC, 'C')
self.player = mpv.MPV(input_default_bindings=True, input_vo_keyboard=True, geometry="55%", sub_scale=0)
self.player.play(filename)
self.player.sub_add(sub)
@self.player.property_observer('sub-text')
def subtitle_change(_name, text):
if text:
new_line = ' '.join([x.strip() for x in text.split('\n')])
if self.line != new_line:
self.line = new_line
self.c.delay_quiz.emit()
@self.player.property_observer('playback-time')
def time_change(_name, new_time):
self.duration = round(self.player.duration or self.duration)
self.now = round(new_time or self.now)
self.seek.setTickInterval(self.duration)
self.seek.setValue(self.now)
if not self.finished and abs(self.now - self.duration) < 2:
self.finished = True
QMessageBox.information(self, 'Message', 'Correct: {0}\nIncorrect: {1}'.format(len(self.correct), len(self.incorrect)))
def currentSub(self):
ctime = self.player.playback_time
for sub in self.subs:
if sub.start.total_seconds() < ctime < sub.end.total_seconds():
return sub
def delay_quiz(self):
time = self.till_sub_end()
if time:
QTimer.singleShot(time, self.c.quiz)
def till_sub_end(self):
sub = self.currentSub()
if sub:
return round((sub.end.total_seconds() - self.player.playback_time) * 1000)
else:
return None
def quiz(self):
till_end = self.till_sub_end()
if not till_end:
pass
elif till_end > 600:
self.c.delay_quiz.emit()
else:
if randint(1,1) == 1:
self.pause()
tokens = nltk.word_tokenize(self.line)
# Don't quiz on a line shorter than 3 tokens
if len(tokens) > 2:
self.blank = choose_blank(tokens, self.blacklist)
if self.blank:
self.sub_text.setPlainText(self.line.replace(self.blank, '_'*len(self.blank)))
else:
self.sub_text.setPlainText(self.line)
else:
self.sub_text.setPlainText(self.line)
def togglePlay(self):
if self.player.pause:
self.play()
else:
self.pause()
def play(self):
self.player.pause = False
self.toggle_play.setText('Pause')
def pause(self):
self.player.pause = True
self.toggle_play.setText('Play')
def submit(self):
if loose_eq(self.blank, self.guess.text()):
self.correct.append(self.blank)
QMessageBox.information(self, 'Message', 'Correct!')
else:
self.incorrect.append(self.blank)
QMessageBox.information(self, 'Message', "No, the word was {0}.".format(self.blank))
self.guess.clear()
self.sub_text.setPlainText(self.line)
self.play()
def closeEvent(self, event):
print("Correct:")
for word in self.correct:
print(word)
print("Incorrect:")
for word in self.incorrect:
print(word)
event.accept()
def loose_eq(word, guess):
return word.strip().lower() == guess.strip().lower()
def choose_blank(tokens, blacklist, tries=10):
if tries == 0:
return None
else:
blank_idx = randint(0, len(tokens) - 1)
blank = tokens[blank_idx]
if len(blank) < 3 or blank.lower() in blacklist:
return choose_blank(tokens, blacklist, tries=tries - 1)
else:
return blank
if __name__ == '__main__':
app = QApplication([sys.argv[0]])
args = docopt(__doc__, version='0.1')
blacklist = []
if args['--blacklist']:
with open(args['--blacklist']) as f:
for line in f:
blacklist.append(line.strip().lower())
ex = Example(args['<filename>'], args['<sub>'], blacklist)
code = app.exec_()
sys.exit(code)