import string class Parser: def __init__(self): self.curstr = '' self.ClearOutput() self.it = '' def ClearOutput(self): self.verb = '' self.toobj = '' self.dirobj = '' self.inobj = '' self.atobj = '' self.withobj = '' def Parse(self,pStr): # print '(Parsing: '+pStr+')' self.ClearOutput() # convert commas to conjunctions str = string.join(string.split(pStr,','), ' and ') # get words; strip out garbage words = filter(lambda x,g=garbs: x not in g, string.split(str)) # first word should be a verb -- if not, assume 'use' w = 0 if words[w] in verbs: self.verb = words[w] w = w + 1 else: self.verb = 'use' # after verb: nothing, noun or conjuction if len(words) < w+1: return '' # no more words if words[w] in nouns: # verb, noun if words[w] == 'it': words[w] = self.it self.dirobj = words[w] self.it = words[w] if len(words) < w+2: return '' if words[w+1] in nouns: # V IO DO if words[w+1] == 'it': words[w+1] = self.it self.toobj = words[w] self.dirobj = words[w+1] self.it = words[w+1] # eat the direct object words.remove(words[w+1]) # eat the noun (IO or DO) words.remove(words[w]) if len(words) < w+1: return '' # no more words while len(words) > w and words[w] in preps: if len(words) < w+2 or words[w+1] not in nouns: print "..." + words[w] + " WHAT?!?" return '' if words[w+1] == 'it': words[w+1] = self.it if words[w] == 'at': self.atobj = words[w+1] elif words[w] == 'in' or words[w] == 'into' or words[w] == 'from': self.inobj = words[w+1] elif words[w] == 'with': self.withobj = words[w+1] elif words[w] == 'to': self.toobj = words[w+1] else: print "ERROR: unknown preposition " + words[w] self.it = words[w+1] # eat the object of the preposition words.remove(words[w+1]) if len(words) < w+2 or words[w+1] not in preps: w = w+1 if len(words) < w+1: return '' # no more words while words[w] in conjs: # V {N {N}} Conj # eat conjunction words.remove(words[w]) # if next word is a verb, eat everything up to that if words[w] in verbs: words = words[w:] # if next word is anything except another conjunction, return if len(words) < w+1: return '' # no more words if words[w] not in conjs: return string.join(words) # after verb and nouns: nothing if len(words) < w+1: return '' # no more words print 'Warning: unknown word '+words[w] # class variables -- dictionary items, etc. verbs = ['drop','get','go','i','inv','look','take','eat','give','put','use'] nouns = ['it','box','rock','bird'] conjs = ['and','then'] preps = ['at','in','into','with','from','to'] garbs = ['the','a'] # test function def test(): p = Parser() while 1: str = raw_input('>') while str: str = p.Parse(str) print [p.verb, p.dirobj, p.toobj, p.inobj, p.atobj, p.withobj]