aitech-sd-lab/modules/DST.py

72 lines
2.3 KiB
Python
Raw Normal View History

from modules.dialogue_state import default_state
import re
# Monitor stanu dialogu
class DST:
def __init__(self):
self.state = default_state()
def txt2num(self, text):
mapping = {'jeden': '1', 'dwa': '2', 'trzy': '3', 'cztery': '4', 'pięć': '5', 'sześć': '6', 'siedem': '7', 'osiem': '8', 'dziewięć': '9', 'dziesięć': '10'}
for key in mapping:
if key in text:
text = text.replace(key, mapping[key])
return text
def update(self, user_act=None):
for intent, domain, slot, value in user_act:
domain = domain.lower()
intent = intent.lower()
slot = slot.lower()
k = slot
if intent == 'inform':
if k is None:
continue
domain_dic = self.state['belief_state'][domain]
if k in domain_dic['semi']:
if k == 'quantity':
value = self.txt2num(value)
try:
self.state['belief_state'][domain]['semi'][k] += int(re.sub('[^0-9]', '', value))
except:
pass
elif k == 'tickettype':
self.state['belief_state'][domain]['semi'][k] += ' ' + value
else:
self.state['belief_state'][domain]['semi'][k] = value
elif k in domain_dic['book']:
if k == 'seat' or k == 'row':
self.state['belief_state'][domain]['book'][k] += ' ' + value
else:
self.state['belief_state'][domain]['book'][k] = value
if intent == 'request':
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
self.state['user_action'].append([intent, domain, slot, value])
return self.state
def init_session(self):
self.state = default_state()
# Przykładowe uruchomienie dla kodu w izolacji
"""
dst = DST()
print(dst.state)
dst.update([['hello_inform', 'Cinema', 'Price', '15 zł'], ['Inform', 'Cinema', 'Movie', 'Batman']])
print(dst.state)
"""