-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyfix.py
executable file
·212 lines (170 loc) · 7.39 KB
/
keyfix.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
#!/usr/bin/env python3
import json
import hunspell
from difflib import SequenceMatcher
directions = ['left', 'top_left', 'top_right', 'right', 'bottom_left', 'bottom_right', 'self']
class Key(object):
"""This defines the Key object. It has a series of adjecent keys."""
def __init__(self, lower_letter, upper_letter, caps_mod, hand, physical):
self.lower_letter = lower_letter
self.upper_letter = upper_letter
self.caps_mod = caps_mod
self.hand = hand
self.physical = physical
self.keys = {}
def __find_key__(self, key_list, key):
"""Find key by lowercase value in a list of key objects"""
return next((key_obj for key_obj in key_list if key_obj.lower_letter == key), None)
def set_surrounding_letters(self, key_dict, key_list):
"""Sets the surrounding keys"""
for direction in directions:
try:
self.keys[direction] = self.__find_key__(key_list, key_dict[direction])
except:
self.keys[direction] = None
self.keys['self'] = self
return self
def shift(self, direction):
"""Returns the key in the proposed direction"""
if self.keys[direction.lower()] and self.keys[direction.lower()].physical:
return self.keys[direction.lower()]
else:
return None
def print_key(self):
string = " "
string += self.keys['top_left'].lower_letter if self.keys['top_left'] else " "
string += " "
string += self.keys['top_right'].lower_letter if self.keys['top_right'] else " "
string += "\n "
string += self.keys['left'].lower_letter if self.keys['left'] else " "
string += " "
string += self.lower_letter
string += " "
string += self.keys['right'].lower_letter if self.keys['right'] else " "
string += "\n "
string += self.keys['bottom_left'].lower_letter if self.keys['bottom_left'] else " "
string += " "
string += self.keys['bottom_right'].lower_letter if self.keys['bottom_right'] else " "
print()
print(string)
print()
class Keyboard(object):
"""This defines a keyboard, or a collection of Keys."""
def __init__(self, layout_file):
with open(layout_file) as layout:
data = json.load(layout)
keys = list(map(self.__make_key__, data))
self.keys = list(map(lambda key: key[0].set_surrounding_letters(key[1], list(zip(*keys))[0]), keys))
self.key_dict = {}
for key in self.keys:
self.key_dict[key.lower_letter] = key
self.key_dict[key.upper_letter] = key
def __make_key__(self, key_dict):
try:
physical = key_dict['physical']
except:
physical = True
return (Key(key_dict['letter'], key_dict['upper'], key_dict['caps_mod'],key_dict['hand'], physical), key_dict)
def __shift_keys__(self, keys, direction, direction2):
return list(map(lambda key: key.shift(direction) if key.hand == "left" else key.shift(direction2), keys))
def __keys_to_string__(self, keys):
return "".join(list(map(lambda key: key.lower_letter, keys)))
def __caps_insert__(self,keys):
return_list = []
previous = "lower"
current = "lower"
for key, letter in keys:
# After these checks, current may not == previous
if letter == key.upper_letter:
current = "upper"
if letter == key.lower_letter:
current = "lower"
# If current != previous, we know that caps lock may have been pressed
if previous != current:
return_list.append(self.key_dict['caps'])
return_list.append(key)
# Update previous to most recent value
previous = current
return return_list
def __hand__(self,keys):
return_list = []
for direction in directions:
for direction2 in directions:
return_list.append(self.__shift_keys__(keys, direction, direction2))
return return_list
def shift(self, word):
"""Shift provided word in every direction."""
# Convert string to list of Keys
keys = list(zip(map(lambda letter: self.key_dict[letter], word), word))
# We have inserted the caps keys so we can properly shift
caps_keys = self.__caps_insert__(keys)
# For each direction, shift Keys into new Key lists
words = self.__hand__(caps_keys)
# Filter out Key lists that contain a None character
real_words = filter(lambda keys: None not in keys, words)
# Convert Key lists into Strings
strings = map(lambda keys: self.__keys_to_string__(keys), real_words)
return list(strings)
def print_keyboard(self):
for key in self.keys:
key.print_key()
en_US = hunspell.HunSpell("/usr/share/myspell/dicts/en_US.dic", "/usr/share/myspell/dicts/en_US.aff")
def word_with_approximation(word):
suggestion = en_US.suggest(word)
if len(suggestion) > 0:
corrected = suggestion[0].decode("utf-8").lower()
probability = SequenceMatcher(None, word, corrected).ratio()
return (corrected, probability)
else:
return (word, 0)
def pair_word_with_rank(word_rank, word):
try:
return (word, word_rank[word])
except:
return (word, 99999)
def check_word(word_rank, keyboard, x):
shifted = keyboard.shift(x)
correct_word = []
filtered = list(filter(lambda word: "-" not in word, shifted))
for spelling in filtered:
print("Spelling for '" + x + "': " + spelling)
suggestion = en_US.suggest(spelling)
if len(suggestion) > 0 and suggestion[0].lower() == spelling:
correct_word.append(spelling)
if len(correct_word) == 0:
corrected = list(map(word_with_approximation, filtered))
for tup in corrected:
print("tup: " + str(tup))
correct_word = max(corrected, key=lambda pair: pair[1])[0]
print("Word: " + x)
print(" Probably: " + correct_word)
elif len(correct_word) == 1:
print("ONLY ONE")
print("Word: " + x)
print(" Probably: " + correct_word[0])
else:
print("MORE THAN ONE")
ranks_tuple = list(map(lambda word: pair_word_with_rank(word_rank, word), correct_word))
print("ranks_tuple:" + str(ranks_tuple))
best_tuple = min(ranks_tuple, key=lambda rank: rank[1])
best_match = best_tuple[0]
print("Word: " + x)
print(" Probably: " + best_match)
def main():
with open("words.json","r") as words:
word_rank = json.load(words)
keyboard = Keyboard("qwerty.json")
print("Shift Error Detection Program")
print("Made by Riley Trautman & Maisam Arif")
print("Type in any word with your fingers not on the homerow and you will get the word that you planned on typing instead")
print("For example hello shifted to the left is gwkki")
for word in ["y3oo9", "biow", "pkw0", "helli", "j8w7j834w5aje8jt", "boop", "bepo", "beepe", "AJ", "escEJ", "KOGvwr", "yu;rt", "rtkwe", "56o34", "miwp4onundiq5ion"]:
check_word(word_rank, keyboard, word)
while (True):
print("------------------------------------------")
x = input()
if x == "exit":
return
check_word(word_rank, keyboard, x)
if __name__ == "__main__":
main()