Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
LPTHW Exercise 48, my lexicon.py version
def scan(sentence):
direction = ('north', 'south', 'east', 'west', 'down', 'up', 'left', 'right', 'back')
verb = ('go', 'stop', 'kill', 'eat')
stop = ('the', 'in', 'of', 'from', 'at', 'it')
noun = ('door', 'bear', 'princess', 'cabinet')
# create a tuple named 'number' which consists of integers from 0 till 100000
number = tuple(range(0, 100001, 1))
# convers the tuple elements into strings
number = tuple(map(str, number))
error = ('ASDFADFASDF', 'IAS')
sentence = sentence.split()
new_list = []
for i in sentence:
if i in noun:
i = [('noun', i)]
new_list += i
elif i in error:
i = [('error', i)]
new_list += i
elif i in direction:
i = [('direction', i)]
new_list += i
elif i in verb:
i = [('verb', i)]
new_list += i
elif i in stop:
i = [('stop', i)]
new_list += i
elif i in number:
try:
i = int(i)
i = [('number', i)]
new_list += i
except ValueError:
return None
return new_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment