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

67 lines
2.2 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
2022-05-25 10:44:21 +02:00
# from convlab2.util.multiwoz.multiwoz_slot_trans import REF_SYS_DA
2022-05-23 21:07:01 +02:00
2022-05-25 10:44:21 +02:00
REF_SYS_DA = {
'Cinema': {
'Type': 'type','Price': 'price','Stars': 'stars',
'Name': 'name','Day': 'day','People': 'people','Movie': 'movie',
'E-mail': 'e-mail', 'none': None
},
}
2022-05-23 21:07:01 +02:00
# 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)
2022-05-25 10:44:21 +02:00
dst.update([['Inform', 'Cinema', 'Price', '15 zł'], ['Inform', 'Cinema', 'Movie', 'Batman']])
print(dst.state['belief_state']['cinema'])
2022-05-23 21:07:01 +02:00
"""