Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class command:
- ##token class to be executed in the kitchen
- imperative = ""
- imperativequalifier = ""
- ing = ""
- n = -1
- p = -1
- def nth(self, a):
- self.n = a
- def pth(self, a):
- self.p = a
- def ingredient(self, a):
- self.ing = a
- def __str__(self):
- return self.imperative + " " + self.imperativequalifier + " " + self.ing + " " + str(self.n) + " " + str(self.p)
- class cookbook:
- ##Class for parsing statements into command objects
- a = "Take ingredient from refrigerator. \nPut ingredient into nth mixing bowl. \nPut ingredient into mixing bowl. \nAdd ingredient. \nAdd ingredient to mixing bowl. \nAdd ingredient to nth mixing bowl. \nRemove ingredient. \nRemove ingredient from mixing bowl. \nRemove ingredient from nth mixing bowl. \nCombine ingredient. \nCombine ingredient into mixing bowl. \nCombine ingredient into nth mixing bowl. \nDivide ingredient. \nDivide ingredient into mixing bowl. \nDivide ingredient into nth mixing bowl. \nAdd dry ingredients. \nAdd dry ingredients into mixing bowl. \nAdd dry ingredients into nth mixing bowl. \nLiquify ingredient. \nLiquify contents of the mixing bowl. \nLiquify contents of the nth mixing bowl. \nStir mixing bowl for num minutes. \nStir nth mixing bowl for num minutes. \nStir ingredient into the mixing bowl. \nStir ingredient into the nth mixing bowl. \nMix well. \nMix the mixing bowl well. \nMix the nth mixing bowl well. \nClean mixing bowl. \nClean nth mixing bowl. \nPour contents of the mixing bowl into baking dish \nPour contents of the nth mixing bowl the pth baking dish \nVerb the ingredient. \nVerb until verbed. \nVerb the ingredient until verbed. \nSet aside. \nServe with auxiliary-recipe. \nRefrigerate. \nRefrigerate for num hours. \nServes num."
- l = list()
- reserved = ['ingredient', "nth", "pth", "ingredient."]
- ##constructor initializes the list l
- def __init__(self):
- la = self.a.split("\n")
- for each in la:
- self.l.append(each.split())
- ##verifies that a given ingredient has been declared
- def ingredient(self, arg):
- if(arg=="zuchinni"):
- return arg
- ##verifies that a given number is a proper ordinal
- ##returns an integer if it is
- def nth(self, arg):
- tailing = arg[len(arg)-2:len(arg)]
- digits = arg[0:len(arg)-2]
- if(14 > int(digits[len(digits)-2:len(digits)]) > 10):
- if(tailing=="th"):
- return digits
- elif(int(digits[-1])==1):
- if(tailing=="st"):
- return digits
- elif(int(digits[-1])==2):
- if(tailing=="nd"):
- return digits
- elif(int(digits[-1])==3):
- if(tailing=="rd"):
- return digits
- elif(tailing=="th"):
- return digits
- raise exception
- def pth(self, arg):
- return nth(arg)
- def num(self, arg):
- return int(arg)
- ##helper method for test that compares an individual template against the argument
- def testDeep(self, arg, template):
- for y in xrange(0, len(template)):
- if(template[y] in self.reserved):
- func = getattr(self, template[y])
- try:
- arg[y] = func(arg[y])
- except:
- return False
- else:
- if(template[y] != arg[y]):
- return False
- return True
- ##helper method for test which converts the result into a command object
- def makeCommand(self, passOn):
- c = command()
- c.imperative = str(passOn[0][0])
- c.imperativequalifier = str(passOn[0][1])
- for x in xrange(0, len(passOn[1])):
- try:
- getattr(c, passOn[1][x])(passOn[0][x])
- except:
- pass
- print c
- return c
- ##turns a valid chef string into a command object
- ##checks syntax against a list of valid template statements
- def test(self, arg):
- testme = arg.split()
- passOn = ()
- for x in xrange(0, len(self.l)):
- template = (self.l[x])
- if(len(testme) == len(template)):
- if(self.testDeep(testme, template)==True):
- ##remove the period from the template (will interfere with getattr(command, s) in case "Add ingredient.")
- template[-1] = template[-1][0:len(template[-1])-1]
- passOn = (testme, template)
- break
- else:
- pass
- m = makeCommand(passOn)
- return m
- c = cookbook()
- print str(c.test("Put zuchinni into 103rd mixing bowl."))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement