{ "cells": [ { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "import json\n", "import os\n", "\n", "\n", "class DST():\n", " def __init__(self):\n", " self.state = json.load(open('dictionary.json'))\n", "\n", " def update(self, user_act=None):\n", " for intent, domain, slot, value in user_act:\n", " domain = domain.lower()\n", " intent = intent.lower()\n", " slot = slot.lower()\n", " \n", " if domain not in self.state['belief_state']:\n", " continue\n", "\n", " if intent == 'inform':\n", " if slot == 'none' or slot == '':\n", " continue\n", "\n", " domain_dic = self.state['belief_state'][domain]\n", "\n", " if slot in domain_dic:\n", " self.state['belief_state'][domain][slot] = value\n", "\n", " elif intent == 'request':\n", " if domain not in self.state['request_state']:\n", " self.state['request_state'][domain] = {}\n", " if slot not in self.state['request_state'][domain]:\n", " self.state['request_state'][domain][slot] = 0\n", "\n", " return self.state" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "dst = DST()\n", "user_act = [('inform', 'payment', 'type', 'credit card'), ('inform', 'product', 'name', 'iPhone')]\n", "state = dst.update(user_act)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'payment': {'type': 'credit card', 'amount': '', 'loyalty_card': ''}, 'delivery': {'type': '', 'address': '', 'time': ''}, 'product': {'name': 'iPhone', 'type': '', 'brand': '', 'price': '', 'quantity': '', 'quality': ''}}\n", "{}\n" ] } ], "source": [ "print(state['belief_state'])\n", "print(state['request_state'])" ] } ], "metadata": { "kernelspec": { "display_name": "py38", "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.8.16" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }