コード例 #1
0
	def parseRecipe (self, itext):
		myRecipe = Recipe();
		sentence = nltk.sent_tokenize(itext);
		for i in sentence:
			myRecipe.addStep(parseStep(i));
コード例 #2
0
class RecipeFlowchart:
	allNodes = [];			# List all the nodes so we can print them
	currentNode = None;		# Where are we in the flowchart
	firstNode = None;
	myrecipe = None;		# The recipe object our flowchart is trying to build
	stringbuffer = "";		# Stores unrecognized strings for later
	ingredients = [];		# A list of ingredient objects we will search for
	actions = [];			# same for actions
	utensils = [];			# and utilities

	def __str__ (self):
		rv = ""
		for n in self.allNodes:
			rv += str(n);
		return rv;

	def __init__ (self):
		self.allNodes = [];
		self.currentNode = None;
		self.firstNode = None;
		self.myrecipe = None;
		self.stringbuffer = "";
		self.ingredients = [];
		self.actions = [];
		self.utensils = [];

	# Once we have our ingredient, utensil, and action lists, we can create the flowchart
	def setupNodes (self):
		stringbuffer = "";

		# Create a new recipe object and set its starting values equal to what we know
		self.myrecipe = Recipe();
		self.myrecipe.allIngredients = self.ingredients;
		self.myrecipe.allUtensils = self.utensils;
		self.startNewRecipeStep("");						# Create a new step
		self.allNodes = [];

		allIngredients = [];
		for i in self.ingredients:
			allIngredients.append(i.name);

		allUtensils = [];
		for u in self.utensils:
			allUtensils.append(u.name);

		# Our flow chart is composed of nodes. Each node has strings that will match it.
		# Each node also has a name, and an action to be performed if triggered
		# Each node has an input node, except the first, and any number of output nodes

		# Start node, the first thing that will be evaluated
		firstNode = Node([""],"first node");
		firstNode.action = "startNewRecipeStep";

		# In Node
		inNode = Node(['in'], "in node");

		# First utensil node
		firstUtensil = Node(allUtensils, "first utensil");
		firstUtensil.action = "addUtensil";
		inNode.addNode(firstUtensil);

		# Cook Node
		cookAction = Node(allCookActions,"cook action");
		cookAction.action = "addAction";
		firstUtensil.addNode(cookAction);

		# Addition Node
		additionWordsAction = Node(['onto','with','together','each'], "addition node");
		cookAction.addNode(additionWordsAction);

		# Measurement Node 1
		measurements1Action = Node(allMeasurements, "0measurement");

		cookAction.addNode(measurements1Action);
		additionWordsAction.addNode(measurements1Action);

		# Ingredient Node 1

		ingredients1Action = Node(allIngredients, "0ingredient");
		ingredients1Action.action = "addIngredient";

		ingredients1Action.addNode(ingredients1Action);
		ingredients1Action.addNode(measurements1Action);

		measurements1Action.addNode(ingredients1Action);
		cookAction.addNode(ingredients1Action);
		additionWordsAction.addNode(ingredients1Action);

		# Utensils Node 1
		uten1Action = Node(allUtensils, "0utensil");
		uten1Action.action = "addUtensil";

		uten1Action.addNode(uten1Action);
		uten1Action.addNode(measurements1Action);

		measurements1Action.addNode(uten1Action);
		cookAction.addNode(uten1Action);
		additionWordsAction.addNode(uten1Action);

		# Addition Node 2
		additionWords2Action = Node(['onto','with','in','on'],"addition node 2");
		ingredients1Action.addNode(additionWords2Action);
		measurements1Action.addNode(additionWords2Action);
		uten1Action.addNode(additionWords2Action);
		cookAction.addNode(additionWords2Action);

		# Measurement Node 2

		measurements2Action = Node(allMeasurements, "1measurement");

		additionWords2Action.addNode(measurements2Action);

		# Ingredient Node 2

		ingredients2Action = Node(allIngredients, "1ingredient");
		ingredients2Action.action = "addIngredient";

		ingredients2Action.addNode(ingredients2Action);
		ingredients2Action.addNode(measurements2Action);

		measurements2Action.addNode(ingredients2Action);
		additionWords2Action.addNode(ingredients2Action);

		# Utensils Node 2
		uten2Action = Node(allUtensils, "1utensil");
		uten2Action.action = "addUtensil";

		uten2Action.addNode(uten2Action);
		uten2Action.addNode(measurements2Action);

		measurements2Action.addNode(uten2Action);
		additionWords2Action.addNode(uten2Action);

		# Until Over Node
		untilOverAction = Node(['until','over'],"until over");

		cookAction.addNode(untilOverAction);
		ingredients1Action.addNode(untilOverAction);
		measurements1Action.addNode(untilOverAction);
		ingredients2Action.addNode(untilOverAction);
		measurements2Action.addNode(untilOverAction);
		uten1Action.addNode(untilOverAction);
		uten2Action.addNode(untilOverAction);

		# Measurement Node 3

		measurements3Action = Node(allMeasurements, "2measurement");

		untilOverAction.addNode(measurements3Action);

		# Ingredient Node 3

		ingredients3Action = Node(allIngredients, "2ingredient");
		ingredients3Action.action = "addIngredient";

		ingredients3Action.addNode(ingredients3Action);
		ingredients3Action.addNode(measurements3Action);

		measurements3Action.addNode(ingredients3Action);
		untilOverAction.addNode(ingredients3Action);

		# Utensils Node 3
		uten3Action = Node(allUtensils, "2utensil");
		uten3Action.action = "addUtensil";

		uten3Action.addNode(uten3Action);
		uten3Action.addNode(measurements3Action);

		measurements3Action.addNode(uten3Action);
		untilOverAction.addNode(uten3Action);

		# Is Are Node
		isAreAction = Node(['is','are'],"is are");

		ingredients3Action.addNode(isAreAction);
		measurements3Action.addNode(isAreAction);
		uten3Action.addNode(isAreAction);

		# To For Node
		toForNode = Node(['to','for'],"to for");

		cookAction.addNode(toForNode);
		ingredients1Action.addNode(toForNode);
		measurements1Action.addNode(toForNode);
		uten1Action.addNode(toForNode);
		uten2Action.addNode(toForNode);
		untilOverAction.addNode(toForNode);

		# Or Node
		orNode = Node(['or'],"or node");

		ingredients2Action.addNode(orNode);
		measurements2Action.addNode(orNode);
		uten2Action.addNode(orNode);

		# Condition Node
		conditionNode = Node(allConditions, "condition node");
		conditionNode.action = "addCondition";

		toForNode.addNode(conditionNode);
		isAreAction.addNode(conditionNode);
		ingredients3Action.addNode(conditionNode);
		uten3Action.addNode(conditionNode);
		measurements3Action.addNode(conditionNode);

		conditionNode.addNode(conditionNode);
		conditionNode.addNode(orNode);
		conditionNode.addNode(untilOverAction);
		conditionNode.addNode(additionWords2Action);

		# And Node
		andNode = Node(['and'],"and node");

		conditionNode.addNode(andNode);

		andNode.addNode(ingredients3Action);
		andNode.addNode(uten3Action);
		andNode.addNode(measurements3Action);
		andNode.addNode(conditionNode);

		# And Node 2
		andNode2 = Node(['and'],"and node 2");

		cookAction.addNode(andNode2);

		andNode2.addNode(cookAction);


		firstNode.addNode(cookAction);
		firstNode.addNode(inNode);
		self.firstNode = firstNode;
		self.currentNode = self.firstNode;

		# Register all the nodes we created so we can print them
		self.allNodes.append(inNode);
		self.allNodes.append(firstUtensil);
		self.allNodes.append(cookAction);
		self.allNodes.append(additionWordsAction);
		self.allNodes.append(ingredients1Action);
		self.allNodes.append(measurements1Action);
		self.allNodes.append(uten1Action);
		self.allNodes.append(additionWords2Action);
		self.allNodes.append(ingredients2Action);
		self.allNodes.append(measurements2Action);
		self.allNodes.append(uten2Action);
		self.allNodes.append(untilOverAction);
		self.allNodes.append(ingredients3Action);
		self.allNodes.append(measurements3Action);
		self.allNodes.append(uten3Action);
		self.allNodes.append(isAreAction);
		self.allNodes.append(toForNode);
		self.allNodes.append(orNode);
		self.allNodes.append(conditionNode);
		self.allNodes.append(andNode);
		self.allNodes.append(andNode2);

	# Call step sequentially on every word in the recipe. It will add the word to the flowchart and decide what to do next
	def step (self, istring):
		#print("\t" + self.currentNode.name + "  " + istring);
		self.stringbuffer = self.stringbuffer + " " + istring;

		# Only advance if the string is accepted as the next node or indicates the start of a new step
		for n in self.currentNode.nextNodes:
			if n.checkNode(istring):
				if "ingredient" in n.name:
					n.doAction(self, istring);
				else:
					n.doAction(self, self.stringbuffer);
				self.currentNode = n;
				if not "measure" in n.name:
					self.stringbuffer = "";
				#print(istring + " " + n.name);
				return;

		# If the string was not accepted, see if this is a new step
		for n in self.firstNode.nextNodes:
			if n.checkNode(istring):
				if self.stringbuffer.endswith(istring):
   					self.stringbuffer = self.stringbuffer[:-len(istring)];
				self.firstNode.doAction(self, self.stringbuffer);
				self.stringbuffer = istring;

				#print("\n\nNew Step");
				n.doAction(self, self.stringbuffer);
				self.stringbuffer = "";
				self.currentNode = n;
				return;
		return;

	def addIngredient (self, iingred):
		self.ingredients.append(iingred);

	def addUtensil (self, iuten):
		self.utensils.append(iuten);

	def startNewRecipeStep (self, iname):
		if self.stringbuffer != "":
			self.addConditionToLastStep(self.stringbuffer);
			self.stringbuffer = "";
		self.myrecipe.addStep(Step("stepName"));

	def addActionToLastStep (self, iname):
		myaction = Action(iname);
		self.myrecipe.getLastStep().addAction(myaction);
		self.myrecipe.allActions.append(iname);

	def addIngredientToLastStep (self, iname):
		for i in self.ingredients:
			if iname in i.name:
				self.myrecipe.getLastStep().addIngredient(i);
				return;

	def addUtensilToLastStep (self, iname):
		myuten = Utensil(iname);
		self.myrecipe.getLastStep().addUtensil(myuten);

	def addConditionToLastStep (self, iname):
		mytime = Time(iname);
		self.myrecipe.getLastStep().addTime(mytime);
コード例 #3
0
class RecipeFlowchart:
    allNodes = []
    # List all the nodes so we can print them
    currentNode = None
    # Where are we in the flowchart
    firstNode = None
    myrecipe = None
    # The recipe object our flowchart is trying to build
    stringbuffer = ""
    # Stores unrecognized strings for later
    ingredients = []
    # A list of ingredient objects we will search for
    actions = []
    # same for actions
    utensils = []
    # and utilities

    def __str__(self):
        rv = ""
        for n in self.allNodes:
            rv += str(n)
        return rv

    def __init__(self):
        self.allNodes = []
        self.currentNode = None
        self.firstNode = None
        self.myrecipe = None
        self.stringbuffer = ""
        self.ingredients = []
        self.actions = []
        self.utensils = []

    # Once we have our ingredient, utensil, and action lists, we can create the flowchart
    def setupNodes(self):
        stringbuffer = ""

        # Create a new recipe object and set its starting values equal to what we know
        self.myrecipe = Recipe()
        self.myrecipe.allIngredients = self.ingredients
        self.myrecipe.allUtensils = self.utensils
        self.startNewRecipeStep("")
        # Create a new step
        self.allNodes = []

        allIngredients = []
        for i in self.ingredients:
            allIngredients.append(i.name)

        allUtensils = []
        for u in self.utensils:
            allUtensils.append(u.name)

        # Our flow chart is composed of nodes. Each node has strings that will match it.
        # Each node also has a name, and an action to be performed if triggered
        # Each node has an input node, except the first, and any number of output nodes

        # Start node, the first thing that will be evaluated
        firstNode = Node([""], "first node")
        firstNode.action = "startNewRecipeStep"

        # In Node
        inNode = Node(['in'], "in node")

        # First utensil node
        firstUtensil = Node(allUtensils, "first utensil")
        firstUtensil.action = "addUtensil"
        inNode.addNode(firstUtensil)

        # Cook Node
        cookAction = Node(allCookActions, "cook action")
        cookAction.action = "addAction"
        firstUtensil.addNode(cookAction)

        # Addition Node
        additionWordsAction = Node(['onto', 'with', 'together', 'each'],
                                   "addition node")
        cookAction.addNode(additionWordsAction)

        # Measurement Node 1
        measurements1Action = Node(allMeasurements, "0measurement")

        cookAction.addNode(measurements1Action)
        additionWordsAction.addNode(measurements1Action)

        # Ingredient Node 1

        ingredients1Action = Node(allIngredients, "0ingredient")
        ingredients1Action.action = "addIngredient"

        ingredients1Action.addNode(ingredients1Action)
        ingredients1Action.addNode(measurements1Action)

        measurements1Action.addNode(ingredients1Action)
        cookAction.addNode(ingredients1Action)
        additionWordsAction.addNode(ingredients1Action)

        # Utensils Node 1
        uten1Action = Node(allUtensils, "0utensil")
        uten1Action.action = "addUtensil"

        uten1Action.addNode(uten1Action)
        uten1Action.addNode(measurements1Action)

        measurements1Action.addNode(uten1Action)
        cookAction.addNode(uten1Action)
        additionWordsAction.addNode(uten1Action)

        # Addition Node 2
        additionWords2Action = Node(['onto', 'with', 'in', 'on'],
                                    "addition node 2")
        ingredients1Action.addNode(additionWords2Action)
        measurements1Action.addNode(additionWords2Action)
        uten1Action.addNode(additionWords2Action)
        cookAction.addNode(additionWords2Action)

        # Measurement Node 2

        measurements2Action = Node(allMeasurements, "1measurement")

        additionWords2Action.addNode(measurements2Action)

        # Ingredient Node 2

        ingredients2Action = Node(allIngredients, "1ingredient")
        ingredients2Action.action = "addIngredient"

        ingredients2Action.addNode(ingredients2Action)
        ingredients2Action.addNode(measurements2Action)

        measurements2Action.addNode(ingredients2Action)
        additionWords2Action.addNode(ingredients2Action)

        # Utensils Node 2
        uten2Action = Node(allUtensils, "1utensil")
        uten2Action.action = "addUtensil"

        uten2Action.addNode(uten2Action)
        uten2Action.addNode(measurements2Action)

        measurements2Action.addNode(uten2Action)
        additionWords2Action.addNode(uten2Action)

        # Until Over Node
        untilOverAction = Node(['until', 'over'], "until over")

        cookAction.addNode(untilOverAction)
        ingredients1Action.addNode(untilOverAction)
        measurements1Action.addNode(untilOverAction)
        ingredients2Action.addNode(untilOverAction)
        measurements2Action.addNode(untilOverAction)
        uten1Action.addNode(untilOverAction)
        uten2Action.addNode(untilOverAction)

        # Measurement Node 3

        measurements3Action = Node(allMeasurements, "2measurement")

        untilOverAction.addNode(measurements3Action)

        # Ingredient Node 3

        ingredients3Action = Node(allIngredients, "2ingredient")
        ingredients3Action.action = "addIngredient"

        ingredients3Action.addNode(ingredients3Action)
        ingredients3Action.addNode(measurements3Action)

        measurements3Action.addNode(ingredients3Action)
        untilOverAction.addNode(ingredients3Action)

        # Utensils Node 3
        uten3Action = Node(allUtensils, "2utensil")
        uten3Action.action = "addUtensil"

        uten3Action.addNode(uten3Action)
        uten3Action.addNode(measurements3Action)

        measurements3Action.addNode(uten3Action)
        untilOverAction.addNode(uten3Action)

        # Is Are Node
        isAreAction = Node(['is', 'are'], "is are")

        ingredients3Action.addNode(isAreAction)
        measurements3Action.addNode(isAreAction)
        uten3Action.addNode(isAreAction)

        # To For Node
        toForNode = Node(['to', 'for'], "to for")

        cookAction.addNode(toForNode)
        ingredients1Action.addNode(toForNode)
        measurements1Action.addNode(toForNode)
        uten1Action.addNode(toForNode)
        uten2Action.addNode(toForNode)
        untilOverAction.addNode(toForNode)

        # Or Node
        orNode = Node(['or'], "or node")

        ingredients2Action.addNode(orNode)
        measurements2Action.addNode(orNode)
        uten2Action.addNode(orNode)

        # Condition Node
        conditionNode = Node(allConditions, "condition node")
        conditionNode.action = "addCondition"

        toForNode.addNode(conditionNode)
        isAreAction.addNode(conditionNode)
        ingredients3Action.addNode(conditionNode)
        uten3Action.addNode(conditionNode)
        measurements3Action.addNode(conditionNode)

        conditionNode.addNode(conditionNode)
        conditionNode.addNode(orNode)
        conditionNode.addNode(untilOverAction)
        conditionNode.addNode(additionWords2Action)

        # And Node
        andNode = Node(['and'], "and node")

        conditionNode.addNode(andNode)

        andNode.addNode(ingredients3Action)
        andNode.addNode(uten3Action)
        andNode.addNode(measurements3Action)
        andNode.addNode(conditionNode)

        # And Node 2
        andNode2 = Node(['and'], "and node 2")

        cookAction.addNode(andNode2)

        andNode2.addNode(cookAction)

        firstNode.addNode(cookAction)
        firstNode.addNode(inNode)
        self.firstNode = firstNode
        self.currentNode = self.firstNode

        # Register all the nodes we created so we can print them
        self.allNodes.append(inNode)
        self.allNodes.append(firstUtensil)
        self.allNodes.append(cookAction)
        self.allNodes.append(additionWordsAction)
        self.allNodes.append(ingredients1Action)
        self.allNodes.append(measurements1Action)
        self.allNodes.append(uten1Action)
        self.allNodes.append(additionWords2Action)
        self.allNodes.append(ingredients2Action)
        self.allNodes.append(measurements2Action)
        self.allNodes.append(uten2Action)
        self.allNodes.append(untilOverAction)
        self.allNodes.append(ingredients3Action)
        self.allNodes.append(measurements3Action)
        self.allNodes.append(uten3Action)
        self.allNodes.append(isAreAction)
        self.allNodes.append(toForNode)
        self.allNodes.append(orNode)
        self.allNodes.append(conditionNode)
        self.allNodes.append(andNode)
        self.allNodes.append(andNode2)

    # Call step sequentially on every word in the recipe. It will add the word to the flowchart and decide what to do next
    def step(self, istring):
        #print("\t" + self.currentNode.name + "  " + istring);
        self.stringbuffer = self.stringbuffer + " " + istring

        # Only advance if the string is accepted as the next node or indicates the start of a new step
        for n in self.currentNode.nextNodes:
            if n.checkNode(istring):
                if "ingredient" in n.name:
                    n.doAction(self, istring)
                else:
                    n.doAction(self, self.stringbuffer)
                self.currentNode = n
                if not "measure" in n.name:
                    self.stringbuffer = ""
                #print(istring + " " + n.name);
                return

        # If the string was not accepted, see if this is a new step
        for n in self.firstNode.nextNodes:
            if n.checkNode(istring):
                if self.stringbuffer.endswith(istring):
                    self.stringbuffer = self.stringbuffer[:-len(istring)]
                self.firstNode.doAction(self, self.stringbuffer)
                self.stringbuffer = istring

                #print("\n\nNew Step");
                n.doAction(self, self.stringbuffer)
                self.stringbuffer = ""
                self.currentNode = n
                return
        return

    def addIngredient(self, iingred):
        self.ingredients.append(iingred)

    def addUtensil(self, iuten):
        self.utensils.append(iuten)

    def startNewRecipeStep(self, iname):
        if self.stringbuffer != "":
            self.addConditionToLastStep(self.stringbuffer)
            self.stringbuffer = ""
        self.myrecipe.addStep(Step("stepName"))

    def addActionToLastStep(self, iname):
        myaction = Action(iname)
        self.myrecipe.getLastStep().addAction(myaction)
        self.myrecipe.allActions.append(iname)

    def addIngredientToLastStep(self, iname):
        for i in self.ingredients:
            if iname in i.name:
                self.myrecipe.getLastStep().addIngredient(i)
                return

    def addUtensilToLastStep(self, iname):
        myuten = Utensil(iname)
        self.myrecipe.getLastStep().addUtensil(myuten)

    def addConditionToLastStep(self, iname):
        mytime = Time(iname)
        self.myrecipe.getLastStep().addTime(mytime)
コード例 #4
0
 def parseRecipe(self, itext):
     myRecipe = Recipe()
     sentence = nltk.sent_tokenize(itext)
     for i in sentence:
         myRecipe.addStep(parseStep(i))