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"],
"drink": ["pepsi", "cola", "water"],
"food": ["pizza"],
@ -47,5 +49,6 @@
"xl": {
"price_multiplier": 1.4
}
}
},
"order": []
}

View File

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