Skip to content

Instantly share code, notes, and snippets.

@soopercorp
Forked from krisys/swype.py
Created February 23, 2012 05:41
Show Gist options
  • Save soopercorp/1890713 to your computer and use it in GitHub Desktop.
Save soopercorp/1890713 to your computer and use it in GitHub Desktop.
How swype works?
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