lab9-10 solution templates
This commit is contained in:
parent
17b941219c
commit
ad9233a887
70
DST_DP_lab_9-10/DP.py
Normal file
70
DST_DP_lab_9-10/DP.py
Normal file
@ -0,0 +1,70 @@
|
||||
from collections import defaultdict
|
||||
from copy import deepcopy
|
||||
from convlab2.policy.policy import Policy
|
||||
from convlab2.util.multiwoz.dbquery import Database
|
||||
from convlab2.util.multiwoz.multiwoz_slot_trans import REF_SYS_DA
|
||||
from convlab2.dialog_agent import PipelineAgent
|
||||
from DST import DST
|
||||
|
||||
|
||||
# Taktyka prowadzenia dialogu
|
||||
class DP(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, intent)].append((slot, 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 ['Stay', 'Day', '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()]['semi'].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:
|
||||
for slot in user_action[user_act]:
|
||||
kb_slot_name = REF_SYS_DA[domain].get(slot[0], slot[0])
|
||||
|
||||
if kb_slot_name in self.results[0]:
|
||||
system_action[(domain, 'Inform')].append([slot[0], self.results[0].get(kb_slot_name, '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']])
|
||||
|
||||
|
||||
# Przykładowe uruchomienie dla kodu jeszcze bez zmian pod rezerwację biletów kinowych
|
||||
"""
|
||||
dst = DST()
|
||||
dp = DP()
|
||||
agent = PipelineAgent(nlu=None, dst=dst, policy=dp, nlg=None, name='sys')
|
||||
print(agent.response([['Inform', 'Hotel', 'Price', 'cheap'], ['Inform', 'Hotel', 'Parking', 'yes']]))
|
||||
"""
|
59
DST_DP_lab_9-10/DST.py
Normal file
59
DST_DP_lab_9-10/DST.py
Normal file
@ -0,0 +1,59 @@
|
||||
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
|
||||
from convlab2.util.multiwoz.multiwoz_slot_trans import REF_SYS_DA
|
||||
|
||||
|
||||
# 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)
|
||||
|
||||
dst.update([['Inform', 'Hotel', 'Price', 'cheap'], ['Inform', 'Hotel', 'Parking', 'yes']])
|
||||
print(dst.state['belief_state']['hotel'])
|
||||
"""
|
89
DST_DP_lab_9-10/dialogue_state.py
Executable file
89
DST_DP_lab_9-10/dialogue_state.py
Executable file
@ -0,0 +1,89 @@
|
||||
def default_state():
|
||||
state = dict(user_action=[],
|
||||
system_action=[],
|
||||
belief_state={},
|
||||
request_state={},
|
||||
terminated=False,
|
||||
history=[])
|
||||
state['belief_state'] = {
|
||||
"police": {
|
||||
"book": {
|
||||
"booked": []
|
||||
},
|
||||
"semi": {}
|
||||
},
|
||||
"hotel": {
|
||||
"book": {
|
||||
"booked": [],
|
||||
"people": "",
|
||||
"day": "",
|
||||
"stay": ""
|
||||
},
|
||||
"semi": {
|
||||
"name": "",
|
||||
"area": "",
|
||||
"parking": "",
|
||||
"pricerange": "",
|
||||
"stars": "",
|
||||
"internet": "",
|
||||
"type": ""
|
||||
}
|
||||
},
|
||||
"attraction": {
|
||||
"book": {
|
||||
"booked": []
|
||||
},
|
||||
"semi": {
|
||||
"type": "",
|
||||
"name": "",
|
||||
"area": ""
|
||||
}
|
||||
},
|
||||
"restaurant": {
|
||||
"book": {
|
||||
"booked": [],
|
||||
"people": "",
|
||||
"day": "",
|
||||
"time": ""
|
||||
},
|
||||
"semi": {
|
||||
"food": "",
|
||||
"pricerange": "",
|
||||
"name": "",
|
||||
"area": "",
|
||||
}
|
||||
},
|
||||
"hospital": {
|
||||
"book": {
|
||||
"booked": []
|
||||
},
|
||||
"semi": {
|
||||
"department": ""
|
||||
}
|
||||
},
|
||||
"taxi": {
|
||||
"book": {
|
||||
"booked": []
|
||||
},
|
||||
"semi": {
|
||||
"leaveAt": "",
|
||||
"destination": "",
|
||||
"departure": "",
|
||||
"arriveBy": ""
|
||||
}
|
||||
},
|
||||
"train": {
|
||||
"book": {
|
||||
"booked": [],
|
||||
"people": ""
|
||||
},
|
||||
"semi": {
|
||||
"leaveAt": "",
|
||||
"destination": "",
|
||||
"day": "",
|
||||
"arriveBy": "",
|
||||
"departure": ""
|
||||
}
|
||||
}
|
||||
}
|
||||
return state
|
4337
DST_DP_lab_9-10/value_dict.json
Executable file
4337
DST_DP_lab_9-10/value_dict.json
Executable file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user