Skip to content

Instantly share code, notes, and snippets.

@rtDNVdza
Created November 24, 2013 07:38
Show Gist options
  • Save rtDNVdza/7624485 to your computer and use it in GitHub Desktop.
Save rtDNVdza/7624485 to your computer and use it in GitHub Desktop.
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