AMUseBot/ai_talks/AMUseBotBackend/utils/main.ipynb
2023-06-05 21:23:33 +02:00

6.1 KiB

from os import walk
import random  
import json
path = 'C:/Users/User/VisualStudio/UserSimulater/AMUseBot'
class NLGRule():
    def __init__(self,recipe,seed=100) -> None:
        self.recipe = recipe
        self.seed = seed


    def respond(self,intent,value):
        answer= ''
        recipe= next(walk(f'{path}/recipe'), (None, None, []))[2] 
        step = 0
        if not self.recipe:
            if intent=='req_title':
                answer +=f'I recomend {self.recomendRecipe()} or {self.recomendRecipe()}, tell me what you choose?'
        else:
            if intent=='req_start':
                if value=='next':
                    answer+=f'{self.instructionFromRecipe(step)}'
                if value=='':
                    answer+=f'First step:{self.instructionFromRecipe(0)}'
            if intent =='req_ingredient':
                answer+=f'{self.ingredientFromRecipe(step)}'
            if intent=='req_ingredient_list':
                answer+=f"Ingredient list:\n{self.ingredientFromRecipe(-1)}"
            if intent == 'goodbye':
                answer+='Bye!'
            if intent =='greating':
                answer+='Hi. '
            if intent == 'req_duration':
                answer +='DURATION'
            if intent =='req_amount':
                answer +='AMOUNT'
            
        return answer

    def ingredientFromRecipe(self,step):
        recipe= next(walk(f'{path}/recipe'), (None, None, []))[2][self.recipe]
        f = open(f'{path}/recipe/{recipe}','r',encoding='utf-8')
        dict = json.loads(f.read())
        ans = ''
        for x in dict['content']:
            if x['type'] == 'ingredient':
                if step == -1:
                    ans+= x['text']+'\n'
                elif int(x['id'].split('-')[1])==int(step):
                    return x['text']
                    
        f.close()
        return ans

    def instructionFromRecipe(self,step):
        recipe= next(walk(f'{path}/recipe'), (None, None, []))[2][self.recipe]
        f = open(f'{path}/recipe/{recipe}','r',encoding='utf-8')
        dict = json.loads(f.read())
        for x in dict['content']:
            if x['type'] == 'instruction':
                if int(x['text'][0])==step:
                    return x['text'].split(')')[1]
        f.close()
        return ''
        
    def recomendRecipe(self):
        rand1 = random.randint(0, 259)
        recipe= next(walk(f'{path}/recipe'), (None, None, []))[2] 
        r1 = recipe[rand1].split('_')[1:]
        r=''
        for i in r1[:-1]:
            r+=i+' '
        r += r1[-1].split('.')[0]
        return r
NLG =NLGRule(recipe=2)
print(NLG.respond('req_ingredient_list',''))
Ingredient list:
2 1/2 cups all-purpose flour
1/2 cup white sugar
1 tablespoon baking powder
1/2 teaspoon salt
1 teaspoon ground cinnamon
1/4 teaspoon ground nutmeg
1 cup milk
1 egg, beaten
1/4 cup butter, melted and cooled
2 teaspoons vanilla extract
2 quarts oil for deep frying
1/2 teaspoon ground cinnamon
1/2 cup white sugar

print(NLG.respond('req_ingredient',''))
2 1/2 cups all-purpose flour

print(NLG.respond('req_start',''))
First step: In a large bowl, stir together the flour, 1/2 cup sugar, baking powder, salt, 1 teaspoon of cinnamon and nutmeg.
NLG =NLGRule(recipe=None)
print(NLG.respond('req_title',''))
I recomend Chocolate Cheesecake II or Figs with Goat Cheese Pecans and Bacon, tell me what you choose?