Compare commits

..

2 Commits

Author SHA1 Message Date
dbdf14bf20 Change requirements 2024-06-09 19:50:58 +02:00
1cd62db966 dialog_policy 2024-06-09 15:57:31 +02:00
3 changed files with 38 additions and 37 deletions

View File

@ -1,4 +1,6 @@
{ {
"address": null,
"order-complete": false,
"dough": ["thick"], "dough": ["thick"],
"drink": ["pepsi", "cola", "water"], "drink": ["pepsi", "cola", "water"],
"food": ["pizza"], "food": ["pizza"],
@ -47,5 +49,6 @@
"xl": { "xl": {
"price_multiplier": 1.4 "price_multiplier": 1.4
} }
} },
"order": []
} }

View File

@ -1,5 +1,6 @@
from src.model.frame import Frame from src.model.frame import Frame
from convlab.dst.dst import DST from convlab.dst.dst import DST
import json
import copy import copy
@ -9,27 +10,19 @@ def normalize(value):
class DialogStateMonitor(DST): class DialogStateMonitor(DST):
domain = 'restaurant' def __init__(self, initial_state_file: str = '../attributes.json'):
def __init__(self):
DST.__init__(self) DST.__init__(self)
self.__initial_state = dict(user_action=[], with open(initial_state_file) as file:
system_action=[], self.__initial_state = json.load(file)
belief_state={ self.__memory = copy.deepcopy(self.__initial_state)
'order': [],
'address': {}, # def __access_memory__(self, path: str) -> str | int | float | None:
'order-complete': False, # result = self.state['memory']
'phone': {}, # for segment in path.split('.'):
'delivery': {}, # if segment not in result:
'payment': {}, # return None
'time': {}, # result = result[segment]
'name': {}, # return result
},
booked={},
request_state={},
terminated=False,
history=[])
self.state = copy.deepcopy(self.__initial_state)
def update(self, frame: Frame): def update(self, frame: Frame):
if frame.source != 'user': if frame.source != 'user':
@ -38,27 +31,30 @@ class DialogStateMonitor(DST):
new_order = dict() new_order = dict()
for slot in frame.slots: for slot in frame.slots:
new_order[slot.name] = normalize(slot.value) new_order[slot.name] = normalize(slot.value)
self.state['belief_state']['order'].append(new_order) self.__memory['order'].append(new_order)
elif frame.act == 'inform/address': elif frame.act == 'inform/address':
for slot in frame.slots: for slot in frame.slots:
self.state['belief_state']['address'][slot.name] = normalize(slot.value) self.__memory['address'][slot.name] = normalize(slot.value)
elif frame.act == 'inform/phone': elif frame.act == 'inform/phone':
for slot in frame.slots: for slot in frame.slots:
self.state['belief_state']['phone'][slot.name] = normalize(slot.value) self.__memory['phone'][slot.name] = normalize(slot.value)
elif frame.act == 'inform/order-complete': elif frame.act == 'inform/order-complete':
self.state['belief_state']['order-complete'] = True self.__memory['order-complete'] = True
elif frame.act == 'inform/delivery': elif frame.act == 'inform/delivery':
for slot in frame.slots: for slot in frame.slots:
self.state['belief_state']['delivery'][slot.name] = normalize(slot.value) self.__memory['delivery'][slot.name] = normalize(slot.value)
elif frame.act == 'inform/payment': elif frame.act == 'inform/payment':
for slot in frame.slots: for slot in frame.slots:
self.state['belief_state']['payment'][slot.name] = normalize(slot.value) self.__memory['payment'][slot.name] = normalize(slot.value)
elif frame.act == 'inform/time': elif frame.act == 'inform/time':
for slot in frame.slots: for slot in frame.slots:
self.state['belief_state']['time'][slot.name] = normalize(slot.value) self.__memory['time'][slot.name] = normalize(slot.value)
elif frame.act == 'inform/name': elif frame.act == 'inform/name':
for slot in frame.slots: for slot in frame.slots:
self.state['belief_state']['name'][slot.name] = normalize(slot.value) self.__memory['name'][slot.name] = normalize(slot.value)
def read(self) -> dict:
return self.__memory
def reset(self): def reset(self):
self.state = copy.deepcopy(self.__initial_state) self.__memory = copy.deepcopy(self.__initial_state)

View File

@ -4,15 +4,17 @@ from src.model.slot import Slot
dst = DialogStateMonitor() dst = DialogStateMonitor()
dst.update(Frame('user', 'inform/order', [Slot('pizza', 'margaritta'), Slot('sauce', 'ketchup')])) assert dst.read()['pizza']['capri']['price'] == 25
dst.update(Frame('user', 'inform/order', [Slot('pizza', 'carbonara')]))
dst.update(Frame('user', 'inform/order', [Slot('B-pizza', 'margaritta'), Slot('B-sauce', 'ketchup')]))
dst.update(Frame('user', 'inform/order', [Slot('B-pizza', 'carbonara')]))
dst.update(Frame('user', 'inform/order-complete', [])) dst.update(Frame('user', 'inform/order-complete', []))
assert dst.state['belief_state']['order'][0]['pizza'] == 'margaritta' assert dst.read()['order'][0]['B-pizza'] == 'margaritta'
assert dst.state['belief_state']['order'][0]['sauce'] == 'ketchup' assert dst.read()['order'][0]['B-sauce'] == 'ketchup'
assert dst.state['belief_state']['order-complete'] == True assert dst.read()['order-complete'] == True
dst.reset() dst.reset()
assert dst.state['belief_state']['order'] == [] assert dst.read()['order'] == []
assert dst.state['belief_state']['order-complete'] == False assert dst.read()['order-complete'] == False