Exemplo n.º 1
0
def action_decoder(state, action_index, action_vocab):
    domains = [
        'Attraction', 'Hospital', 'Hotel', 'Restaurant', 'Taxi', 'Train',
        'Police'
    ]
    delex_action = action_vocab.get_action(action_index)
    action = {}

    for act in delex_action:
        domain, act_type = act.split('-')
        if act_type == 'Request':
            action[act] = []
            for slot in delex_action[act]:
                action[act].append([slot, '?'])
        elif act == 'Booking-Book':
            action['Booking-Book'] = [["Ref", generate_ref_num(8)]]
        elif domain not in domains:
            action[act] = [['none', 'none']]
        else:
            if act == 'Taxi-Inform':
                for info_slot in ['leaveAt', 'arriveBy']:
                    if info_slot in state['belief_state']['taxi']['semi'] and \
                        state['belief_state']['taxi']['semi'][info_slot] != "":
                        car = generate_car()
                        phone_num = generate_ref_num(11)
                        action[act] = []
                        action[act].append(['Car', car])
                        action[act].append(['Phone', phone_num])
                        break
                else:
                    action[act] = [['none', 'none']]
            elif act in ['Train-Inform', 'Train-NoOffer', 'Train-OfferBook']:
                for info_slot in ['departure', 'destination']:
                    if info_slot not in state['belief_state']['train']['semi'] or \
                        state['belief_state']['train']['semi'][info_slot] == "":
                        action[act] = [['none', 'none']]
                        break
                else:
                    for info_slot in ['leaveAt', 'arriveBy']:
                        if info_slot in state['belief_state']['train']['semi'] and \
                            state['belief_state']['train']['semi'][info_slot] != "":
                            _domain_fill(delex_action, state, action, act)
                            break
                    else:
                        action[act] = [['none', 'none']]
            elif domain in domains:
                _domain_fill(delex_action, state, action, act)

    return action
Exemplo n.º 2
0
    def decode(self, action_index, state):
        domains = ['Attraction', 'Hospital', 'Hotel', 'Restaurant', 'Taxi', 'Train', 'Police']
        delex_action = self.action_vocab.get_action(action_index)
        action = {}

        for act in delex_action:
            domain, act_type = act.split('-')
            if domain in domains:
                self.current_domain = domain
            if act_type == 'Request':
                action[act] = []
                for slot in delex_action[act]:
                    action[act].append([slot, '?'])
            elif act == 'Booking-Book':
                constraints = []
                for slot in state['belief_state'][self.current_domain.lower()]['semi']:
                    if state['belief_state'][self.current_domain.lower()]['semi'][slot] != "":
                        constraints.append([slot, state['belief_state'][self.current_domain.lower()]['semi'][slot]])
                kb_result = query(self.current_domain.lower(), constraints)
                if len(kb_result) == 0:
                    action[act] = [['none', 'none']]
                else:
                    if "Ref" in kb_result[0]:
                        action[act] = [["Ref", kb_result[0]["Ref"]]]
                    else:
                        action[act] = [["Ref", "N/A"]]
            elif domain not in domains:
                action[act] = [['none', 'none']]
            else:
                if act == 'Taxi-Inform':
                    for info_slot in ['leaveAt', 'arriveBy']:
                        if info_slot in state['belief_state']['taxi']['semi'] and \
                                state['belief_state']['taxi']['semi'][info_slot] != "":
                            car = generate_car()
                            phone_num = generate_ref_num(11)
                            action[act] = []
                            action[act].append(['Car', car])
                            action[act].append(['Phone', phone_num])
                            break
                    else:
                        action[act] = [['none', 'none']]
                elif act in ['Train-Inform', 'Train-NoOffer', 'Train-OfferBook']:
                    for info_slot in ['departure', 'destination']:
                        if info_slot not in state['belief_state']['train']['semi'] or \
                                state['belief_state']['train']['semi'][info_slot] == "":
                            action[act] = [['none', 'none']]
                            break
                    else:
                        for info_slot in ['leaveAt', 'arriveBy']:
                            if info_slot in state['belief_state']['train']['semi'] and \
                                    state['belief_state']['train']['semi'][info_slot] != "":
                                self.domain_fill(delex_action, state, action, act)
                                break
                        else:
                            action[act] = [['none', 'none']]
                elif domain in domains:
                    self.domain_fill(delex_action, state, action, act)

        return action
Exemplo n.º 3
0
def _domain_fill(delex_action, state, action, act):
    domain, act_type = act.split('-')
    constraints = []
    for slot in state['belief_state'][domain.lower()]['semi']:
        if state['belief_state'][domain.lower()]['semi'][slot] != "":
            constraints.append(
                [slot, state['belief_state'][domain.lower()]['semi'][slot]])
    if act_type in ['NoOffer',
                    'OfferBook']:  # NoOffer['none'], OfferBook['none']
        action[act] = []
        for slot in constraints:
            action[act].append(
                [REF_USR_DA[domain].get(slot[0], slot[0]), slot[1]])
    elif act_type in ['Inform', 'Recommend',
                      'OfferBooked']:  # Inform[Slot,...], Recommend[Slot, ...]
        kb_result = query(domain.lower(), constraints)
        # print("Policy Util")
        # print(constraints)
        # print(len(kb_result))
        if len(kb_result) == 0:
            action[act] = [['none', 'none']]
        else:
            action[act] = []
            for slot in delex_action[act]:
                if slot == 'Choice':
                    action[act].append([slot, len(kb_result)])
                elif slot == 'Ref':
                    action[act].append(["Ref", generate_ref_num(8)])
                else:
                    try:
                        action[act].append([
                            slot,
                            kb_result[0][REF_SYS_DA[domain].get(slot, slot)]
                        ])
                    except:
                        action[act].append([slot, "N/A"])
            if len(action[act]) == 0:
                action[act] = [['none', 'none']]
    elif act_type in ['Select']:  # Select[Slot]
        kb_result = query(domain.lower(), constraints)
        if len(kb_result) < 2:
            action[act] = [['none', 'none']]
        else:
            slot = delex_action[act][0]
            action[act] = []
            action[act].append(
                [slot, kb_result[0][REF_SYS_DA[domain].get(slot, slot)]])
            action[act].append(
                [slot, kb_result[1][REF_SYS_DA[domain].get(slot, slot)]])
    else:
        print('Cannot decode:', str(delex_action))
        action[act] = [['none', 'none']]