aitech-sd-lab/DST_DP_lab_9-10/DST.py

60 lines
2.0 KiB
Python
Raw Normal View History

2022-05-23 21:07:01 +02:00
from dialogue_state import default_state
import json
from convlab2.dst.dst import DST as CL2DST
from convlab2.dst.rule.multiwoz.dst_util import normalize_value
from convlab2.util.multiwoz.multiwoz_slot_trans import REF_SYS_DA
# Monitor stanu dialogu
class DST(CL2DST):
def __init__(self):
CL2DST.__init__(self)
self.state = default_state()
self.value_dict = json.load(open('value_dict.json'))
def update(self, user_act=None):
for intent, domain, slot, value in user_act:
domain = domain.lower()
intent = intent.lower()
if domain in ['unk', 'general', 'booking']:
continue
if intent == 'inform':
k = REF_SYS_DA[domain.capitalize()].get(slot, slot)
if k is None:
continue
domain_dic = self.state['belief_state'][domain]
if k in domain_dic['semi']:
nvalue = normalize_value(self.value_dict, domain, k, value)
self.state['belief_state'][domain]['semi'][k] = nvalue
elif k in domain_dic['book']:
self.state['belief_state'][domain]['book'][k] = value
elif k.lower() in domain_dic['book']:
self.state['belief_state'][domain]['book'][k.lower()] = value
elif intent == 'request':
k = REF_SYS_DA[domain.capitalize()].get(slot, slot)
if domain not in self.state['request_state']:
self.state['request_state'][domain] = {}
if k not in self.state['request_state'][domain]:
self.state['request_state'][domain][k] = 0
return self.state
def init_session(self):
self.state = default_state()
# Przykładowe uruchomienie dla kodu jeszcze bez zmian pod rezerwację biletów kinowych
"""
dst = DST()
print(dst.state)
dst.update([['Inform', 'Hotel', 'Price', 'cheap'], ['Inform', 'Hotel', 'Parking', 'yes']])
print(dst.state['belief_state']['hotel'])
"""