Created
November 24, 2013 07:38
-
-
Save rtDNVdza/7624485 to your computer and use it in GitHub Desktop.
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
class lexicon(object): | |
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'] | |
finalList = [] | |
@staticmethod | |
def isnum(s): | |
try: | |
return int(s) | |
except ValueError: | |
return None | |
@staticmethod | |
def scan(sentence): | |
lexicon.finalList = [] | |
words = sentence.split(" ") | |
for i in words: | |
if lexicon.isnum(i): | |
newGroup = ('number' , int(i)) | |
lexicon.finalList.append(newGroup) | |
else: | |
flag = 0 | |
for ii in lexicon.direction: | |
if ii == i: | |
newGroup = ('direction', ii) | |
lexicon.finalList.append(newGroup) | |
flag = 1 | |
for ii in lexicon.verb: | |
if ii == i and flag != 1: | |
newGroup = ('verb', ii) | |
lexicon.finalList.append(newGroup) | |
flag = 1 | |
for ii in lexicon.stop: | |
if ii == i and flag != 1: | |
newGroup = ('stop', ii) | |
lexicon.finalList.append(newGroup) | |
flag = 1 | |
for ii in lexicon.noun: | |
if ii == i and flag != 1: | |
newGroup = ('noun', ii) | |
lexicon.finalList.append(newGroup) | |
flag = 1 | |
if flag != 1: | |
newGroup = ('error', i) | |
lexicon.finalList.append(newGroup) | |
return lexicon.finalList |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment