forked from jjlee/amphetype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lesson.py
180 lines (151 loc) · 6.7 KB
/
Lesson.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
from __future__ import with_statement, division
from itertools import islice
import random
import time
import codecs
from Data import DB
import Globals
try:
import editdist
except ImportError:
import editdist_fake as editdist
import Text
from Config import *
from QtUtil import *
class StringListWidget(QTextEdit):
def __init__(self, *args):
super(StringListWidget, self).__init__(*args)
self.setWordWrapMode(QTextOption.WordWrap)
self.setAcceptRichText(False)
self.delayflag = 0
self.connect(self, SIGNAL("textChanged()"), self.textChanged)
def addList(self, lst):
self.append(u' '.join(lst))
def getList(self):
return str(self.toPlainText()).split()
def addFromTyped(self):
words = [x[0] for x in DB.fetchall('select distinct data from statistic where type = 2 order by random()')]
self.filterWords(words)
def addFromFile(self):
filen = QFileDialog.getOpenFileName()
if filen == u'':
return
try:
with codecs.open(filen, "r", "utf_8_sig") as f:
words = f.read().split()
except Exception as e:
QMessageBox.warning(self, "Couldn't Read File", str(e))
return
random.shuffle(words)
self.filterWords(words)
def filterWords(self, words):
n = Settings.get('str_extra')
w = Settings.get('str_what')
if w == 'r': # random
pass
else:
control = self.getList()
if len(control) == 0:
return
if w == 'e': # encompassing
stream = list(map(lambda x: (sum([x.count(c) for c in control]), x), words))
print("str:", list(stream)[0:10])
preres = list(islice(filter(lambda x: x[0] > 0, stream), 4*n))
print("pre:", preres)
preres.sort(key=lambda x: x[0], reverse=True)
words = list(map(lambda x: x[1], preres))
else: # similar
words = filter(lambda x:
0 < min([
editdist.distance(x.encode('latin1', 'replace'),
y.encode('latin1', 'replace'))/max(len(y), len(x))
for y in control]) < .26, words)
if Settings.get('str_clear') == 'r': # replace = clear
self.clear()
self.addList(islice(words, n))
def textChanged(self):
if self.delayflag > 0:
self.delayflag += 1
return
self.emit(SIGNAL("updated"))
self.delayflag = 1
QTimer.singleShot(500, self.revertFlag)
def revertFlag(self):
if self.delayflag > 1:
self.emit(SIGNAL("updated"))
self.delayflag = 0
class LessonGenerator(QWidget):
def __init__(self, *args):
super(LessonGenerator, self).__init__(*args)
self.strings = StringListWidget()
self.sample = QTextEdit()
self.sample.setWordWrapMode(QTextOption.WordWrap)
self.sample.setAcceptRichText(False)
self.les_name = QLineEdit()
self.setLayout(AmphBoxLayout([
["Welcome to Amphetype's automatic lesson generator!"],
["You can retrieve a list of words/keys/trigrams to practice from the Analysis tab, import from an external file, or even type in your own (separated by space).\n"""],
10,
["In generating lessons, I will make", SettingsEdit("gen_copies"),
"copies the list below and divide them into sublists of size",
SettingsEdit("gen_take"), "(0 for all).", None],
["I will then", SettingsCombo("gen_mix", [('c', "concatenate"), ('m', "commingle")]),
"corresponding sublists into atomic building blocks which are fashioned into lessons according to your lesson size preferences.", None],
[
([
(self.strings, 1),
[SettingsCombo('str_clear', [('s', "Supplement"), ('r', "Replace")]), "list with",
SettingsEdit("str_extra"),
SettingsCombo('str_what', [('e', 'encompassing'), ('s', 'similar'), ('r', 'random')]),
"words from", AmphButton("a file", self.strings.addFromFile),
"or", AmphButton("analysis database", self.strings.addFromTyped), None]
], 1),
([
"Lessons (separated by empty lines):",
(self.sample, 1),
[None, AmphButton("Add to Sources", self.acceptLessons), "with name", self.les_name]
], 1)
]
]))
self.connect(Settings, SIGNAL("change_gen_take"), self.generatePreview)
self.connect(Settings, SIGNAL("change_gen_copies"), self.generatePreview)
self.connect(Settings, SIGNAL("change_gen_mix"), self.generatePreview)
self.connect(self.strings, SIGNAL("updated"), self.generatePreview)
def wantReview(self, words):
Globals.pendingLessons = Globals.pendingLessons + self.generateLesson(words)
print(Globals.pendingLessons)
if Globals.pendingLessons:
self.emit(SIGNAL("newReview"), Globals.pendingLessons.pop())
else:
self.emit(SIGNAL("newReview"), "")
def generatePreview(self):
words = self.strings.getList()
sentences = self.generateLesson(words)
self.sample.clear()
#for x in Text.to_lessons(sentences):
for x in sentences:
self.sample.append(x + "\n\n")
def generateLesson(self, words):
copies = Settings.get('gen_copies')
take = Settings.get('gen_take')
mix = Settings.get('gen_mix')
sentences = []
while len(words) > 0:
sen = words[0:take] * copies
words[0:take] = []
if mix == 'm': # mingle
random.shuffle(sen)
sentences.append(u' '.join(sen))
#print sentences
return sentences
def acceptLessons(self, name=None):
name = str(self.les_name.text())
if len(name.strip()) == 0:
name = "<Lesson %s>" % time.strftime("%y-%m-%d %H:%M")
lessons = [v for v in [x.strip() for x in str(self.sample.toPlainText()).split("\n\n")] if v]
if len(lessons) == 0:
QMessageBox.information(self, "No Lessons", "Generate some lessons before you try to add them!")
return
self.emit(SIGNAL("newLessons"), name, lessons, 1)
def addStrings(self, *args):
self.strings.addList(*args)