{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from os import walk\n", "import random \n", "import json" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "path = 'C:/Users/User/VisualStudio/UserSimulater/AMUseBot'" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "class NLGRule():\n", " def __init__(self,recipe,seed=100) -> None:\n", " self.recipe = recipe\n", " self.seed = seed\n", "\n", "\n", " def respond(self,intent,value):\n", " answer= ''\n", " recipe= next(walk(f'{path}/recipe'), (None, None, []))[2] \n", " step = 0\n", " if not self.recipe:\n", " if intent=='req_title':\n", " answer +=f'I recomend {self.recomendRecipe()} or {self.recomendRecipe()}, tell me what you choose?'\n", " else:\n", " if intent=='req_start':\n", " if value=='next':\n", " answer+=f'{self.instructionFromRecipe(step)}'\n", " if value=='':\n", " answer+=f'First step:{self.instructionFromRecipe(0)}'\n", " if intent =='req_ingredient':\n", " answer+=f'{self.ingredientFromRecipe(step)}'\n", " if intent=='req_ingredient_list':\n", " answer+=f\"Ingredient list:\\n{self.ingredientFromRecipe(-1)}\"\n", " if intent == 'goodbye':\n", " answer+='Bye!'\n", " if intent =='greating':\n", " answer+='Hi. '\n", " if intent == 'req_duration':\n", " answer +='DURATION'\n", " if intent =='req_amount':\n", " answer +='AMOUNT'\n", " \n", " return answer\n", "\n", " def ingredientFromRecipe(self,step):\n", " recipe= next(walk(f'{path}/recipe'), (None, None, []))[2][self.recipe]\n", " f = open(f'{path}/recipe/{recipe}','r',encoding='utf-8')\n", " dict = json.loads(f.read())\n", " ans = ''\n", " for x in dict['content']:\n", " if x['type'] == 'ingredient':\n", " if step == -1:\n", " ans+= x['text']+'\\n'\n", " elif int(x['id'].split('-')[1])==int(step):\n", " return x['text']\n", " \n", " f.close()\n", " return ans\n", "\n", " def instructionFromRecipe(self,step):\n", " recipe= next(walk(f'{path}/recipe'), (None, None, []))[2][self.recipe]\n", " f = open(f'{path}/recipe/{recipe}','r',encoding='utf-8')\n", " dict = json.loads(f.read())\n", " for x in dict['content']:\n", " if x['type'] == 'instruction':\n", " if int(x['text'][0])==step:\n", " return x['text'].split(')')[1]\n", " f.close()\n", " return ''\n", " \n", " def recomendRecipe(self):\n", " rand1 = random.randint(0, 259)\n", " recipe= next(walk(f'{path}/recipe'), (None, None, []))[2] \n", " r1 = recipe[rand1].split('_')[1:]\n", " r=''\n", " for i in r1[:-1]:\n", " r+=i+' '\n", " r += r1[-1].split('.')[0]\n", " return r\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ingredient list:\n", "2 1/2 cups all-purpose flour\n", "1/2 cup white sugar\n", "1 tablespoon baking powder\n", "1/2 teaspoon salt\n", "1 teaspoon ground cinnamon\n", "1/4 teaspoon ground nutmeg\n", "1 cup milk\n", "1 egg, beaten\n", "1/4 cup butter, melted and cooled\n", "2 teaspoons vanilla extract\n", "2 quarts oil for deep frying\n", "1/2 teaspoon ground cinnamon\n", "1/2 cup white sugar\n", "\n" ] } ], "source": [ "NLG =NLGRule(recipe=2)\n", "print(NLG.respond('req_ingredient_list',''))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 1/2 cups all-purpose flour\n" ] } ], "source": [ "print(NLG.respond('req_ingredient',''))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First step: In a large bowl, stir together the flour, 1/2 cup sugar, baking powder, salt, 1 teaspoon of cinnamon and nutmeg.\n" ] } ], "source": [ "\n", "print(NLG.respond('req_start',''))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I recomend Chocolate Cheesecake II or Figs with Goat Cheese Pecans and Bacon, tell me what you choose?\n" ] } ], "source": [ "NLG =NLGRule(recipe=None)\n", "print(NLG.respond('req_title',''))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)]" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "369f2c481f4da34e4445cda3fffd2e751bd1c4d706f27375911949ba6bb62e1c" } } }, "nbformat": 4, "nbformat_minor": 2 }