-
-
Save soopercorp/1890713 to your computer and use it in GitHub Desktop.
How swype works?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WORDS = open('wordlist.txt').read().split() | |
KEYBRD_LAYOUT = ['qwertyuiop', 'asdfghjkl', 'zxcvbnm'] | |
def match(path, word): | |
""" Checks if a word is present in a path or not. """ | |
try: | |
for char in word: | |
path = path.split(char, 1)[1] | |
return True | |
except : return False | |
def get_keyboard_row( char ): | |
""" Returns the row number of the character """ | |
for row_no, row in enumerate(KEYBRD_LAYOUT): | |
if char in row: | |
return row_no | |
def compress(sequence): | |
""" Removes redundant sequential characters. ex : 11123311 => 1231 """ | |
ret_val = [ sequence[0] ] | |
for element in sequence: | |
if ret_val[-1] != element: | |
ret_val.append(element) | |
return ret_val | |
def get_minimum_wordlength(path): | |
""" | |
Returns the minimum possible word length from the path. | |
Uses the number of transitions from different rows in | |
the keyboard layout to determin the minimum length | |
""" | |
row_numbers = map(get_keyboard_row, path) | |
compressed_row_numbers = compress(row_numbers) | |
return len(compressed_row_numbers) - 3 | |
def get_suggestion(path): | |
""" Returns suggestions for a given path. """ | |
suggestions = filter(lambda x: x[0] == path[0] and x[-1] == path[-1], WORDS) | |
suggestions = filter(lambda x: match(path, x), suggestions) | |
min_length = get_minimum_wordlength(path) | |
suggestions = filter(lambda x: len(x) > min_length, suggestions) | |
return suggestions | |
if __name__ == '__main__': | |
test_cases = ['heqerqllo', # hello | |
'qwertyuihgfcvbnjk', # quick | |
'wertyuioiuytrtghjklkjhgfd', # world | |
'dfghjioijhgvcftyuioiuytr', # doctor | |
'aserfcvghjiuytedcftyuytre', # architecture | |
'asdfgrtyuijhvcvghuiklkjuytyuytre', # agriculture | |
'mjuytfdsdftyuiuhgvc', # music | |
'vghjioiuhgvcxsasdvbhuiklkjhgfdsaserty', # vocabulary | |
] | |
for test in test_cases: | |
print get_suggestion(test) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment