Generic_DialogSystem/DialogManager.ipynb
2023-05-25 13:40:17 +02:00

6.3 KiB

import json
import os


class DST():
    def __init__(self):
        self.state = json.load(open('dictionary.json'))

    def update(self, user_act=None):
        for intent, domain, slot, value in user_act:
            domain = domain.lower()
            intent = intent.lower()
            slot = slot.lower()
            
            if domain not in self.state['belief_state']:
                continue

            if intent == 'inform':
                if slot == 'none' or slot == '':
                    continue

                domain_dic = self.state['belief_state'][domain]

                if slot in domain_dic:
                    self.state['belief_state'][domain][slot] = value

            elif intent == 'request':
                if domain not in self.state['request_state']:
                    self.state['request_state'][domain] = {}
                if slot not in self.state['request_state'][domain]:
                    self.state['request_state'][domain][slot] = 0
                    
        self.state['user_action'] = user_act
        return self.state
dst = DST()
user_act = [('inform', 'payment', 'type', 'credit card'), ('inform', 'product', 'name', 'iPhone'), ('request', 'product', 'type', 'warzywo')]
state = dst.update(user_act)
print(state['belief_state'])
print(state['request_state'])
dst.state
{'payment': {'type': 'credit card', 'amount': '', 'loyalty_card': ''}, 'delivery': {'type': '', 'address': '', 'time': ''}, 'product': {'name': 'iPhone', 'type': '', 'brand': '', 'price': '', 'quantity': '', 'quality': ''}}
{'product': {'type': 0}}
{'user_act': [],
 'system_act': [],
 'belief_state': {'payment': {'type': 'credit card',
   'amount': '',
   'loyalty_card': ''},
  'delivery': {'type': '', 'address': '', 'time': ''},
  'product': {'name': 'iPhone',
   'type': '',
   'brand': '',
   'price': '',
   'quantity': '',
   'quality': ''}},
 'request_state': {'product': {'type': 0}},
 'terminated': False,
 'history': [],
 'user_action': [('inform', 'payment', 'type', 'credit card'),
  ('inform', 'product', 'name', 'iPhone'),
  ('request', 'product', 'type', 'warzywo')]}
from collections import defaultdict
import copy
import json
from copy import deepcopy



class SimpleRulePolicy():
    def __init__(self):
        self.db = json.load(open('product_db.json'))

    def predict(self, state):
        self.results = []
        system_action = defaultdict(list)
        user_action = defaultdict(list)

        for intent, domain, slot, value in state['user_action']:
            user_action[(domain.lower(), intent.lower())].append((slot.lower(), value))

        for user_act in user_action:
            self.update_system_action(user_act, user_action, state, system_action)

        # Reguła 3
        

        system_acts = [[intent, domain, slot, value] for (domain, intent), slots in system_action.items() for slot, value in slots]
        state['system_action'] = system_acts
        return system_acts

    def update_system_action(self, user_act, user_action, state, system_action):
        domain, intent = user_act

        # Reguła 1
        if intent == 'request':
            if len(self.results) == 0:
                system_action[(domain, 'NoOffer')] = []
            else:
                for slot in user_action[user_act]:                  
                    if slot[0] in self.results[0]:
                        system_action[(domain, 'Inform')].append([slot[0], self.results[0].get(slot[0], 'unknown')])

        # Reguła 2
        elif intent == 'inform':
            if len(self.results) == 0:
                system_action[(domain, 'NoOffer')] = []
            else:
                system_action[(domain, 'Inform')].append(['Choice', str(len(self.results))])
                choice = self.results[0]

                if domain in ["product"]:
                    system_action[(domain, 'Recommend')].append(['Name', choice['name']])
policy = SimpleRulePolicy()
policy.predict(dst.state)
[]