Skip to content

Instantly share code, notes, and snippets.

@Coldsun1
Created December 20, 2017 22:19
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 Coldsun1/e70283a01361d25304c0bb11d4b37679 to your computer and use it in GitHub Desktop.
Save Coldsun1/e70283a01361d25304c0bb11d4b37679 to your computer and use it in GitHub Desktop.
def scan(sentence):
sentence = sentence.lower()
list_of_tuples = []
words = sentence.split()
for word in words:
word_tuple = scan_word(word)
list_of_tuples.append(word_tuple)
return list_of_tuples
def scan_word(word):
direction_words = ['north', 'south', 'east', 'west', 'down', 'up', 'right', 'back']
verb_words = ['go', 'stop', 'kill', 'eat']
stop_words = ['the', 'in', 'of', 'from', 'at', 'it', 'to']
noun_words = ['door', 'bear', 'princess', 'cabinet']
if word in direction_words:
return ('direction', word)
elif word in verb_words:
return ('verb', word)
elif word in stop_words:
return ('stop', word)
elif word in noun_words:
return ('noun', word)
elif is_number(word):
return ('number', int(word))
else:
return ('error', word)
def is_number(s):
try:
number = int(s)
return True
except ValueError:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment