Compare commits

...

3 Commits

Author SHA1 Message Date
8b55dbf73b Change requirements 2024-06-09 19:51:25 +02:00
ee1d7e45d4 dialog_policy 2024-06-09 19:51:25 +02:00
72e17d2106 Zmiana metody zapisu pamięci w DST 2024-06-09 19:48:26 +02:00
15 changed files with 291 additions and 46 deletions

View File

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

View File

@ -2,5 +2,5 @@ flair==0.13.1
conllu==4.5.3 conllu==4.5.3
pandas==1.5.3 pandas==1.5.3
numpy==1.26.4 numpy==1.26.4
torch==2.3.0 torch==1.13
convlab==3.0.2a0 convlab==3.0.2a0

View File

@ -0,0 +1,4 @@
[
"true",
"false"
]

View File

@ -0,0 +1,5 @@
[
"pepsi",
"cola",
"water"
]

View File

@ -0,0 +1,11 @@
[
{
"name":"pepsi"
},
{
"name":"cola"
},
{
"name":"water"
}
]

View File

@ -0,0 +1,3 @@
[
"pizza"
]

View File

@ -0,0 +1,5 @@
[
"chicken",
"ham",
"tuna"
]

View File

@ -0,0 +1,7 @@
[
"capri",
"margarita",
"hawajska",
"barcelona",
"tuna"
]

View File

@ -0,0 +1,51 @@
[
{
"name": "capri",
"ingredient": [
"tomato",
"ham",
"mushrooms",
"cheese"
],
"price": 25
},
{
"name": "margarita",
"ingredient": [
"tomato",
"cheese"
],
"price": 20
},
{
"name": "hawajska",
"ingredient": [
"tomato",
"pineapple",
"chicken",
"cheese"
],
"price": 30
},
{
"name": "barcelona",
"ingredient": [
"tomato",
"onion",
"ham",
"pepper",
"cheese"
],
"price": 40
},
{
"name": "tuna",
"ingredient": [
"tomato",
"tuna",
"onion",
"cheese"
],
"price": 40
}
]

View File

@ -0,0 +1,4 @@
[
"garlic",
"1000w"
]

View File

@ -0,0 +1,14 @@
[
{
"size": "m",
"price_multiplier": 1
},
{
"size": "l",
"price_multiplier": 1.2
},
{
"size": "xl",
"price_multiplier": 1.4
}
]

91
src/service/dbquery.py Normal file
View File

@ -0,0 +1,91 @@
"""
"""
import json
import os
import random
from fuzzywuzzy import fuzz
from itertools import chain
from copy import deepcopy
class Database(object):
def __init__(self):
super(Database, self).__init__()
# loading databases
domains = ['restaurant', 'hotel', 'attraction', 'train', 'hospital', 'taxi', 'police']
self.dbs = {}
for domain in domains:
with open(os.path.join(os.path.dirname(
os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))),
'data/restaurant/db/{}_db.json'.format(domain))) as f:
self.dbs[domain] = json.load(f)
def query(self, domain, constraints, ignore_open=False, soft_contraints=(), fuzzy_match_ratio=60):
"""Returns the list of entities for a given domain
based on the annotation of the belief state"""
# query the db
if domain == 'taxi':
return [{'taxi_colors': random.choice(self.dbs[domain]['taxi_colors']),
'taxi_types': random.choice(self.dbs[domain]['taxi_types']),
'taxi_phone': ''.join([str(random.randint(1, 9)) for _ in range(11)])}]
if domain == 'police':
return deepcopy(self.dbs['police'])
if domain == 'hospital':
department = None
for key, val in constraints:
if key == 'department':
department = val
if not department:
return deepcopy(self.dbs['hospital'])
else:
return [deepcopy(x) for x in self.dbs['hospital'] if x['department'].lower() == department.strip().lower()]
constraints = list(map(lambda ele: ele if not(ele[0] == 'area' and ele[1] == 'center') else ('area', 'centre'), constraints))
found = []
for i, record in enumerate(self.dbs[domain]):
constraints_iterator = zip(constraints, [False] * len(constraints))
soft_contraints_iterator = zip(soft_contraints, [True] * len(soft_contraints))
for (key, val), fuzzy_match in chain(constraints_iterator, soft_contraints_iterator):
if val == "" or val == "dont care" or val == 'not mentioned' or val == "don't care" or val == "dontcare" or val == "do n't care":
pass
else:
try:
record_keys = [k.lower() for k in record]
if key.lower() not in record_keys:
continue
if key == 'leaveAt':
val1 = int(val.split(':')[0]) * 100 + int(val.split(':')[1])
val2 = int(record['leaveAt'].split(':')[0]) * 100 + int(record['leaveAt'].split(':')[1])
if val1 > val2:
break
elif key == 'arriveBy':
val1 = int(val.split(':')[0]) * 100 + int(val.split(':')[1])
val2 = int(record['arriveBy'].split(':')[0]) * 100 + int(record['arriveBy'].split(':')[1])
if val1 < val2:
break
# elif ignore_open and key in ['destination', 'departure', 'name']:
elif ignore_open and key in ['destination', 'departure']:
continue
elif record[key].strip() == '?':
# '?' matches any constraint
continue
else:
if not fuzzy_match:
if val.strip().lower() != record[key].strip().lower():
break
else:
if fuzz.partial_ratio(val.strip().lower(), record[key].strip().lower()) < fuzzy_match_ratio:
break
except:
continue
else:
res = deepcopy(record)
res['Ref'] = '{0:08d}'.format(i)
found.append(res)
return found
if __name__ == '__main__':
db = Database()
print(db.query("train", [['departure', 'cambridge'], ['destination','peterborough'], ['day', 'tuesday'], ['arriveBy', '11:15']]))

View File

@ -1,8 +1,59 @@
from model.frame import Frame from collections import defaultdict
import copy
import json
from copy import deepcopy
class DialogPolicy: from convlab.policy.policy import Policy
def next_dialogue_act(self, frames: list[Frame]) -> Frame: from dbquery import Database
if frames[-1].act == "welcomemsg":
return Frame("system", "welcomemsg", []) class SimpleRulePolicy(Policy):
def __init__(self):
Policy.__init__(self)
self.db = Database()
def predict(self, state):
self.results = []
system_action = defaultdict(list)
user_action = defaultdict(list)
for intent, domain, slot, value in state['user_action']:
user_action[(domain.lower(), intent.lower())].append((slot.lower(), value))
for user_act in user_action:
self.update_system_action(user_act, user_action, state, system_action)
# Reguła 3
if any(True for slots in user_action.values() for (slot, _) in slots if slot in ['book stay', 'book day', 'book people']):
if self.results:
system_action = {('Booking', 'Book'): [["Ref", self.results[0].get('Ref', 'N/A')]]}
system_acts = [[intent, domain, slot, value] for (domain, intent), slots in system_action.items() for slot, value in slots]
state['system_action'] = system_acts
return system_acts
def update_system_action(self, user_act, user_action, state, system_action):
domain, intent = user_act
constraints = [(slot, value) for slot, value in state['belief_state'][domain.lower()].items() if value != '']
self.results = deepcopy(self.db.query(domain.lower(), constraints))
# Reguła 1
if intent == 'request':
if len(self.results) == 0:
system_action[(domain, 'NoOffer')] = []
else: else:
return Frame("system", "canthelp", []) for slot in user_action[user_act]:
if slot[0] in self.results[0]:
system_action[(domain, 'Inform')].append([slot[0], self.results[0].get(slot[0], 'unknown')])
# Reguła 2
elif intent == 'inform':
if len(self.results) == 0:
system_action[(domain, 'NoOffer')] = []
else:
system_action[(domain, 'Inform')].append(['Choice', str(len(self.results))])
choice = self.results[0]
if domain in ["hotel", "attraction", "police", "restaurant"]:
system_action[(domain, 'Recommend')].append(['Name', choice['name']])
dialogPolicy = SimpleRulePolicy()

View File

@ -1,6 +1,5 @@
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
@ -10,19 +9,27 @@ def normalize(value):
class DialogStateMonitor(DST): class DialogStateMonitor(DST):
def __init__(self, initial_state_file: str = '../attributes.json'): domain = 'restaurant'
DST.__init__(self)
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: def __init__(self):
# result = self.state['memory'] DST.__init__(self)
# for segment in path.split('.'): self.__initial_state = dict(user_action=[],
# if segment not in result: system_action=[],
# return None belief_state={
# result = result[segment] 'order': [],
# return result 'address': {},
'order-complete': False,
'phone': {},
'delivery': {},
'payment': {},
'time': {},
'name': {},
},
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':
@ -31,30 +38,27 @@ 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.__memory['order'].append(new_order) self.state['belief_state']['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.__memory['address'][slot.name] = normalize(slot.value) self.state['belief_state']['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.__memory['phone'][slot.name] = normalize(slot.value) self.state['belief_state']['phone'][slot.name] = normalize(slot.value)
elif frame.act == 'inform/order-complete': elif frame.act == 'inform/order-complete':
self.__memory['order-complete'] = True self.state['belief_state']['order-complete'] = True
elif frame.act == 'inform/delivery': elif frame.act == 'inform/delivery':
for slot in frame.slots: for slot in frame.slots:
self.__memory['delivery'][slot.name] = normalize(slot.value) self.state['belief_state']['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.__memory['payment'][slot.name] = normalize(slot.value) self.state['belief_state']['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.__memory['time'][slot.name] = normalize(slot.value) self.state['belief_state']['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.__memory['name'][slot.name] = normalize(slot.value) self.state['belief_state']['name'][slot.name] = normalize(slot.value)
def read(self) -> dict:
return self.__memory
def reset(self): def reset(self):
self.__memory = copy.deepcopy(self.__initial_state) self.state = copy.deepcopy(self.__initial_state)

View File

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