Advertisement
Vermiculus

Untitled

Apr 8th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.75 KB | None | 0 0
  1. class command:
  2.     ##token class to be executed in the kitchen
  3.     imperative = ""
  4.     imperativequalifier = ""
  5.     ing = ""
  6.     n = -1
  7.     p = -1
  8.  
  9.     def nth(self, a):
  10.         self.n = a
  11.     def pth(self, a):
  12.         self.p = a
  13.     def ingredient(self, a):
  14.         self.ing = a
  15.  
  16.     def __str__(self):
  17.         return self.imperative + " " + self.imperativequalifier + " " + self.ing + " " + str(self.n) + " " + str(self.p)
  18.    
  19.  
  20.  
  21.  
  22. class cookbook:
  23.     ##Class for parsing statements into command objects
  24.     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."
  25.     l = list()
  26.     reserved = ['ingredient', "nth", "pth", "ingredient."]
  27.  
  28.     ##constructor initializes the list l
  29.     def __init__(self):
  30.         la = self.a.split("\n")
  31.        
  32.         for each in la:
  33.             self.l.append(each.split())
  34.  
  35.     ##verifies that a given ingredient has been declared
  36.     def ingredient(self, arg):
  37.         if(arg=="zuchinni"):
  38.             return arg
  39.  
  40.     ##verifies that a given number is a proper ordinal
  41.     ##returns an integer if it is
  42.     def nth(self, arg):
  43.         tailing = arg[len(arg)-2:len(arg)]
  44.         digits = arg[0:len(arg)-2]
  45.  
  46.         if(14 > int(digits[len(digits)-2:len(digits)]) > 10):
  47.             if(tailing=="th"):
  48.                 return digits
  49.         elif(int(digits[-1])==1):
  50.             if(tailing=="st"):
  51.                 return digits
  52.         elif(int(digits[-1])==2):
  53.             if(tailing=="nd"):
  54.                 return digits
  55.         elif(int(digits[-1])==3):
  56.             if(tailing=="rd"):
  57.                 return digits
  58.         elif(tailing=="th"):
  59.                 return digits
  60.         raise exception
  61.  
  62.     def pth(self, arg):
  63.         return nth(arg)
  64.  
  65.     def num(self, arg):
  66.         return int(arg)
  67.  
  68.     ##helper method for test that compares an individual template against the argument
  69.     def testDeep(self, arg, template):
  70.         for y in xrange(0, len(template)):
  71.             if(template[y] in self.reserved):
  72.                 func = getattr(self, template[y])
  73.                 try:    
  74.                     arg[y] = func(arg[y])
  75.                 except:
  76.                     return False
  77.             else:
  78.                 if(template[y] != arg[y]):
  79.                     return False
  80.                
  81.         return True
  82.  
  83.     ##helper method for test which converts the result into a command object
  84.     def makeCommand(self, passOn):
  85.         c = command()
  86.         c.imperative = str(passOn[0][0])
  87.         c.imperativequalifier = str(passOn[0][1])
  88.        
  89.         for x in xrange(0, len(passOn[1])):
  90.             try:
  91.                 getattr(c, passOn[1][x])(passOn[0][x])
  92.             except:
  93.                 pass
  94.         print c
  95.         return c
  96.  
  97.     ##turns a valid chef string into a command object
  98.     ##checks syntax against a list of valid template statements
  99.     def test(self, arg):
  100.        
  101.         testme = arg.split()
  102.         passOn = ()
  103.         for x in xrange(0, len(self.l)):
  104.             template = (self.l[x])
  105.             if(len(testme) == len(template)):
  106.                 if(self.testDeep(testme, template)==True):
  107.                     ##remove the period from the template (will interfere with getattr(command, s) in case "Add ingredient.")
  108.                     template[-1] = template[-1][0:len(template[-1])-1]
  109.                     passOn = (testme, template)
  110.                     break
  111.                 else:
  112.                     pass
  113.                    
  114.         m = makeCommand(passOn)
  115.         return m
  116. c = cookbook()
  117. print str(c.test("Put zuchinni into 103rd mixing bowl."))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement