예제 #1
0
def calc_True_Reward(action_sequence, qa_info, adaptive_flag = False):
    entity_mask = qa_info['entity_mask'] if 'entity_mask' in qa_info.keys() else {}
    relation_mask = qa_info["relation_mask"] if 'relation_mask' in qa_info.keys() else {}
    type_mask = qa_info['type_mask'] if 'type_mask' in qa_info.keys() else {}
    int_mask = qa_info['int_mask'] if 'int_mask' in qa_info.keys() else {}
    # Update(add) elements in dict.
    masking_elements = {**entity_mask, **relation_mask, **type_mask, **int_mask}
    new_action = list()
    # Default separator of split() method is any whitespace.
    mask_name_list = ["ENTITY", "RELATION", "TYPE", "INT"]
    for act in action_sequence:
        # Unmasking.
        if any(name in str(act) for name in mask_name_list):
            correct_flag = False
            for k, v in masking_elements.items():
                if act == v:
                    act = k
                    correct_flag = True
                    break
            # If the action could not find the corresponding mask in the dict,
            # then the predicted action is not semantically correct, the reward should be returned as -1.0.
            if not correct_flag:
                # print('%s: %s is wrong!' % (str(qa_info['qid']), str(action_sequence)))
                return -1.0
        new_action.append(act)
    symbolic_seq = list2dict(new_action)
    # print (symbolic_seq)
    symbolic_exe = Symbolics(symbolic_seq)
    answer = symbolic_exe.executor()
    if adaptive_flag:
        return calc_adaptative_reward(answer, qa_info)
    else:
        return calc_01_reward(answer, qa_info)
예제 #2
0
def calc_True_Reward(action_sequence, qa_info):
    entity_mask = qa_info['entity_mask'] if 'entity_mask' in qa_info.keys() else {}
    relation_mask = qa_info["relation_mask"] if 'relation_mask' in qa_info.keys() else {}
    type_mask = qa_info['type_mask'] if 'type_mask' in qa_info.keys() else {}
    # Update(add) elements in dict.
    masking_elements = {**entity_mask, **relation_mask, **type_mask}
    new_action = list()
    # Default separator of split() method is any whitespace.
    for act in action_sequence:
        for k, v in masking_elements.items():
            if act == v:
                act = k
                break
        new_action.append(act)
    symbolic_seq = list2dict(new_action)
    # print (symbolic_seq)
    symbolic_exe = Symbolics(symbolic_seq)
    answer = symbolic_exe.executor()
    return calc_adaptative_reward(answer, qa_info)
예제 #3
0
# -*- coding: utf-8 -*-
# @Time    : 2019/09/02 20:25
# @Author  : Devin Hua
'''
input:
<class 'list'>: ['A2', '(', 'Q6619679', 'P674', 'Q838948', ')', 'A2', '(', 'Q6619679', '-', 'P674', 'Q36649', ')', 'A6', '(', 'Q944203', ')', 'A11', '(', ')']
output:
<class 'list'>: [{'A2': ['Q3895768', 'P1441', 'Q3895768']}, {'A2': ['Q15961987', '-P1441', 'Q95074']}, {'A15': ['Q347947', '', '']}]
'''
from SymbolicExecutor.transform_util import list2dict

if __name__ == "__main__":
    list_temp = [
        'A2', '(', ')', ')', '(', 'Q6619679', '-', 'P674', 'Q36649', 'A6',
        'Q944203'
    ]
    print(list2dict(list_temp))
    print(len(""))