Skip to content

Instantly share code, notes, and snippets.

@chelesergiu
Created April 4, 2020 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chelesergiu/e46a8b70893c6e497114957d5fe61991 to your computer and use it in GitHub Desktop.
Save chelesergiu/e46a8b70893c6e497114957d5fe61991 to your computer and use it in GitHub Desktop.
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