-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdictionary.py
233 lines (179 loc) · 8.31 KB
/
dictionary.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
import json
import os
import itertools
import copy
class Dictionary():
"""Tools for interacting with Plover dictionary"""
@staticmethod
def create_default():
"""Creates a dictionary using the default file locations"""
dictionary = None
resources_directory = os.path.join(os.path.dirname(__file__), 'resources')
dictfile = open(os.path.join(resources_directory, 'dict.json'), 'r')
commonfile = open(os.path.join(resources_directory, 'common.json'), 'r')
conversionfile = open(os.path.join(resources_directory, 'binaryToSteno.json'), 'r')
dictionary = Dictionary(json.load(dictfile), json.load(commonfile), json.load(conversionfile))
dictfile.close()
commonfile.close()
conversionfile.close()
return dictionary
def __init__(self, dictionary=None, common=None, conversion=None, threshold=None):
if dictionary:
self.data = dict((self.encode_brief(self.expand_brief(k)), dictionary[k]) for k in dictionary)
if common:
self.common = list(v["Word"] for i, v in enumerate(common)) if threshold is None else \
list(v["Word"] for i, v in enumerate(common) if int(v["Rank"]) < threshold)
# print ' '.join(self.common)
if conversion:
self.conversion = dict((v, int(k)) for(k, v) in conversion.items())
def filter(self, match_string, require_string=None, include_multisyllabic=False):
"""Returns a subset of the dictionary given the match_string and other settings"""
'''data is in the form of
"ARPL": "arm"
test stuff using
# if self.test_bool_exp(match_string, k, None))
'''
result = copy.deepcopy(self.data)
match_string = self.encode_brief(self.expand_brief(match_string))
# monosyllabic
if not include_multisyllabic:
result = dict((k, result[k]) for k in result if u'/' not in k)
# common words only
if hasattr(self, 'common'):
result = dict((k, result[k]) for k in result if result[k] in self.common)
if match_string is not '':
combos = self.combinations(match_string, require_string)
result = dict((k, result[k]) for k in result if k in combos)
result = dict((self.decode_brief(k), result[k]) for k in result)
return result
def __filter(self, k, combos):
for c in combos:
if c in k:
return True
return False
def combinations(self, match_string, require_string=None):
result = set()
if len(match_string) > 2:
for i in range(2, len(match_string) + 1):
for x in itertools.combinations(match_string, i): result.add(''.join(x))
for x in match_string: result.add(x)
elif len(match_string) is 2:
result = set([match_string, match_string[0], match_string[1]])
else:
result = set(match_string)
if(require_string):
requires = set(self.encode_brief(self.expand_brief(s)) for s in require_string.split(','))
result = set(k for k in result if self.__filter(k, requires))
return result
def prepare_for_quiz(self, dictionary):
"""Gets the data into a form that is consumable by the quiz.js on the client. currently, this means an array of binary keys to press"""
result = []
for x in dictionary.keys():
# old way
# result[self.convert_to_binary(x)] = "binary"
result.append(self.convert_to_binary(x))
return result
def convert_to_binary(self, keystroke_string):
new_keystroke = self.expand_brief(keystroke_string)
binary = 0
x = 0
while x < len(new_keystroke):
token = new_keystroke[x]
if self.conversion.has_key(token):
binary = binary | self.conversion[token]
x += 1
else:
token = new_keystroke[x] + new_keystroke[x + 1]
if self.conversion.has_key(token):
binary = binary | self.conversion[token]
x += 2
return binary
def expand_brief(self, brief):
"""Account for handedness in the briefs"""
if brief is None:
return None
right_hand = False
vowel_visited = False
output = ''
for x in brief:
if x == '-':
right_hand = True
elif x == '#':
output = output + x
elif x == '*':
output = output + x
right_hand = True
else:
if x in 'EU':
right_hand = True
if right_hand:
output = output + "-" + x
else:
if x in 'AO':
vowel_visited = True
output = output + x + "-"
elif vowel_visited:
right_hand = True
output = output + '-' + x
else:
output = output + x + "-"
return output
def encode_brief(self, brief):
"""Just another representation of steno keys. Condenses all hyphenated keys of the expanded form into lower case"""
output = ''
right_hand = False
brief = str(brief)
i = 0
while i < len(brief):
if str.isalpha(brief[i]):
output = output + brief[i]
i = i + 1
elif brief[i] is '-' and i + 1 < len(brief) and str.isalpha(brief[i + 1]):
output = output + str.lower(brief[i + 1])
i = i + 1
else:
output = output + brief[i]
i = i + 1
return output
def decode_brief(self, brief):
output = ''
hyphen_added = False
seen_vowel = False
for c in brief:
if c in 'AO*':
seen_vowel = True
if str.islower(c) and not hyphen_added:
if not seen_vowel:
output = output + "-"
hyphen_added = True
output = output + str.upper(c)
return output
def wordlist(self, match_string, required_string=None):
filtered = self.filter(match_string, required_string)
return list(set(filtered.values()))
def lesson(self, description, match_string, required_string):
return '*** %s ***\n [%s]\n' % (description, '\t'.join(dictionary.wordlist(match_string, required_string)))
def debug_list_missing_words(self):
"""Returns a list of the words that are in common but not in the dictionary"""
#assume that dict.json is much bigger than common.json
search = set(self.data.values())
data = list((k) for k in self.common if k not in search)
return data
if __name__ == "__main__":
"""running some tests here"""
assets_dir = os.path.join(os.path.dirname(__file__), 'resources')
with open(os.path.join(assets_dir, 'dict.json'), 'r') as dictfile:
with open(os.path.join(assets_dir, 'common.json'), 'r') as commonfile:
with open(os.path.join(assets_dir, 'binaryToSteno.json'), 'r') as conversionfile:
dictionary = Dictionary(json.load(dictfile), json.load(commonfile), json.load(conversionfile), 5000)
##print dictionary.lesson('Ending TS', 'STKPWHRAO*EUTS', '-TS') # ADDED
#print dictionary.lesson('Ending -BGS = X', 'STKPWHRAO*EUBGS', '-BGS')
#print dictionary.lesson('Ending -GS = tion, sion, xion ', 'STKPWHRAO*EUFGS', '-GS') # ADDED
#print dictionary.lesson('Ending D', 'STKPWHRAO*EUD', '-D') #
#print dictionary.lesson('Ending Z', 'STKPWHRAO*EUZ', '-Z')
#print dictionary.lesson('KP = X', 'KPAO*EUFRPBLGTS', 'KP')
#print dictionary.lesson('STKPB = Z', 'STKPBAO*EUFRPBLGTS', 'STKPB')
print 'Done.'
# print json.dumps(returned)
# for (k, v) in returned:
# print "%s=%s" % (k, v)