예제 #1
0
def evaluate_all(word_dict, template_word_dict, slot_dict, dset, decoder, params):
    results = []
    if params['dia_slot_val'] == 2 or params['dia_slot_val'] == 3:
        inverse_word_dict = {template_word_dict[k]:k for k in template_word_dict.keys()}
    else:
        inverse_word_dict = {word_dict[k]:k for k in word_dict.keys()}
    
    for i, ele in enumerate(dset.diaact_sentence_pairs):
        #pred_ys, pred_words = decoder.forward(inverse_word_dict, ele, params, predict_model=True)
        pred_ys, pred_words = decoder.beam_forward(inverse_word_dict, ele, params, predict_model=True)
        
        pred_template_sentence = ' '.join(pred_words[:-1])
        real_sentence = ' '.join(ele['sentence'].split(' ')[1:-1])
        real_template_sentence = ' '.join(ele['sentence_template'].split(' ')[1:-1])
           
        # replace the slot_val place holder with real value
        sentence = post_process(pred_template_sentence, ele['slotval'], slot_dict)
                
        #results.append({'real':real_template_sentence, 'pred':pred_template_sentence}) 
        results.append({'real':real_sentence, 'pred':sentence}) 
        
        print 'test case', i, "/", len(dset.diaact_sentence_pairs)
        print 'teamplate:', real_template_sentence
        print 'pred_template:', pred_template_sentence
        print 'real:', real_sentence
        print 'pred_sentence:', sentence
        print ''
    
    return results
예제 #2
0
def read_diaact_from_string(text, word_dict, template_word_dict, act_dict,
                            slot_dict, decoder, params):
    if params['dia_slot_val'] == 2 or params['dia_slot_val'] == 3:
        inverse_word_dict = {
            template_word_dict[k]: k
            for k in template_word_dict.keys()
        }
    else:
        inverse_word_dict = {word_dict[k]: k for k in word_dict.keys()}

    annot = text.strip(' ').strip('\n').strip('\r')
    dia_act = parse_str_to_diaact(annot, act_dict, slot_dict)
    dia_act_rep = prepare_diaact_representation(dia_act, word_dict,
                                                template_word_dict, act_dict,
                                                slot_dict, params)

    pred_ys, pred_words = decoder.beam_forward(inverse_word_dict,
                                               dia_act_rep,
                                               params,
                                               predict_model=True)
    pred_sentence = ' '.join(pred_words[:-1])

    # replace the slot_val place holder with real value
    sentence = post_process(pred_sentence, dia_act['inform_slots'], slot_dict)

    return sentence
예제 #3
0
def read_diaact_from_cmd(word_dict, template_word_dict, act_dict, slot_dict, decoder, params):
    if params['dia_slot_val'] == 2 or params['dia_slot_val'] == 3:
        inverse_word_dict = {template_word_dict[k]:k for k in template_word_dict.keys()}
    else:
        inverse_word_dict = {word_dict[k]:k for k in word_dict.keys()}
     
    while True:
        print("Your input: ")
        command = raw_input()
        annot = command.strip(' ').strip('\n').strip('\r')
        if len(annot) > 0: 
            dia_act = parse_str_to_diaact(annot, act_dict, slot_dict)
            dia_act_rep = prepare_diaact_representation(dia_act, word_dict, template_word_dict, act_dict, slot_dict, params)
            
            #pred_ys, pred_words = decoder.forward(inverse_word_dict, dia_act_rep, params, predict_model=True)
            pred_ys, pred_words = decoder.beam_forward(inverse_word_dict, dia_act_rep, params, predict_model=True)
            pred_sentence = ' '.join(pred_words[:-1])
            
            # replace the slot_val place holder with real value
            sentence = post_process(pred_sentence, dia_act['inform_slots'], slot_dict)
                    
            print 'Dia-Act:', annot
            print 'Dia-Act Parsing:', dia_act
            print 'Pred_Template:', pred_sentence 
            print 'NLG:', sentence
        else:
            break
예제 #4
0
def read_diaact_from_file(path, word_dict, template_word_dict, act_dict, slot_dict, decoder, params):
    file = open(path, 'r')
    lines = [line for line in file]
    
    if params['dia_slot_val'] == 2 or params['dia_slot_val'] == 3:
        inverse_word_dict = {template_word_dict[k]:k for k in template_word_dict.keys()}
    else:
        inverse_word_dict = {word_dict[k]:k for k in word_dict.keys()}
     
    for lineindex, l in enumerate(lines):
        l = l.strip('\n')
        dia_act = parse_str_to_diaact(l, act_dict, slot_dict)
        dia_act_rep = prepare_diaact_representation(dia_act, word_dict, template_word_dict, act_dict, slot_dict, params)
        
        #pred_ys, pred_words = decoder.forward(inverse_word_dict, dia_act_rep, params, predict_model=True)
        pred_ys, pred_words = decoder.beam_forward(inverse_word_dict, dia_act_rep, params, predict_model=True)
        pred_sentence = ' '.join(pred_words[:-1])
            
        # replace the slot_val place holder with real value
        sentence = post_process(pred_sentence, dia_act['inform_slots'], slot_dict)
            
        print 'Test case', lineindex
        print 'Dia-Act:', l
        print 'Pred_Template:', pred_sentence 
        print 'NLG:', sentence
예제 #5
0
def read_diaact_from_string(text, word_dict, template_word_dict, act_dict, slot_dict, decoder, params):
    if params['dia_slot_val'] == 2 or params['dia_slot_val'] == 3:
        inverse_word_dict = {template_word_dict[k]:k for k in template_word_dict.keys()}
    else:
        inverse_word_dict = {word_dict[k]:k for k in word_dict.keys()}
     
    annot = text.strip(' ').strip('\n').strip('\r')
    dia_act = parse_str_to_diaact(annot, act_dict, slot_dict)
    dia_act_rep = prepare_diaact_representation(dia_act, word_dict, template_word_dict, act_dict, slot_dict, params)
    
    pred_ys, pred_words = decoder.beam_forward(inverse_word_dict, dia_act_rep, params, predict_model=True)
    pred_sentence = ' '.join(pred_words[:-1])
    
    # replace the slot_val place holder with real value
    sentence = post_process(pred_sentence, dia_act['inform_slots'], slot_dict)

    return sentence
예제 #6
0
def evaluate(word_dict, template_word_dict, slot_dict, dset, split, decoder,
             params):
    results = []
    if params['dia_slot_val'] == 2 or params['dia_slot_val'] == 3:
        inverse_word_dict = {
            template_word_dict[k]: k
            for k in template_word_dict.keys()
        }
    else:
        inverse_word_dict = {word_dict[k]: k for k in word_dict.keys()}

    for i, ele in enumerate(dset.split[split]):
        #pred_ys, pred_words = decoder.forward(inverse_word_dict, ele, params, predict_model=True)
        pred_ys, pred_words = decoder.beam_forward(inverse_word_dict,
                                                   ele,
                                                   params,
                                                   predict_model=True)

        pred_template_sentence = ' '.join(pred_words[:-1])
        real_sentence = ' '.join(ele['sentence'].split(' ')[1:-1])
        real_template_sentence = ' '.join(
            ele['sentence_template'].split(' ')[1:-1])

        # replace the slot_val place holder with real value
        sentence = post_process(pred_template_sentence, ele['slotval'],
                                slot_dict)

        #results.append({'real':real_template_sentence, 'pred':pred_template_sentence})
        results.append({'real': real_sentence, 'pred': sentence})

        print 'test case', i, "/", len(dset.split[split])
        print 'teamplate:', real_template_sentence
        print 'pred_template:', pred_template_sentence
        print 'real:', real_sentence
        print 'pred_sentence:', sentence
        print ''

    return results
예제 #7
0
def read_diaact_from_cmd(word_dict, template_word_dict, act_dict, slot_dict,
                         decoder, params):
    if params['dia_slot_val'] == 2 or params['dia_slot_val'] == 3:
        inverse_word_dict = {
            template_word_dict[k]: k
            for k in template_word_dict.keys()
        }
    else:
        inverse_word_dict = {word_dict[k]: k for k in word_dict.keys()}

    while True:
        print("Your input: ")
        command = raw_input()
        annot = command.strip(' ').strip('\n').strip('\r')
        if len(annot) > 0:
            dia_act = parse_str_to_diaact(annot, act_dict, slot_dict)
            dia_act_rep = prepare_diaact_representation(
                dia_act, word_dict, template_word_dict, act_dict, slot_dict,
                params)

            #pred_ys, pred_words = decoder.forward(inverse_word_dict, dia_act_rep, params, predict_model=True)
            pred_ys, pred_words = decoder.beam_forward(inverse_word_dict,
                                                       dia_act_rep,
                                                       params,
                                                       predict_model=True)
            pred_sentence = ' '.join(pred_words[:-1])

            # replace the slot_val place holder with real value
            sentence = post_process(pred_sentence, dia_act['inform_slots'],
                                    slot_dict)

            print 'Dia-Act:', annot
            print 'Dia-Act Parsing:', dia_act
            print 'Pred_Template:', pred_sentence
            print 'NLG:', sentence
        else:
            break
예제 #8
0
def read_diaact_from_file(path, word_dict, template_word_dict, act_dict,
                          slot_dict, decoder, params):
    file = open(path, 'r')
    lines = [line for line in file]

    if params['dia_slot_val'] == 2 or params['dia_slot_val'] == 3:
        inverse_word_dict = {
            template_word_dict[k]: k
            for k in template_word_dict.keys()
        }
    else:
        inverse_word_dict = {word_dict[k]: k for k in word_dict.keys()}

    for lineindex, l in enumerate(lines):
        l = l.strip('\n')
        dia_act = parse_str_to_diaact(l, act_dict, slot_dict)
        dia_act_rep = prepare_diaact_representation(dia_act, word_dict,
                                                    template_word_dict,
                                                    act_dict, slot_dict,
                                                    params)

        #pred_ys, pred_words = decoder.forward(inverse_word_dict, dia_act_rep, params, predict_model=True)
        pred_ys, pred_words = decoder.beam_forward(inverse_word_dict,
                                                   dia_act_rep,
                                                   params,
                                                   predict_model=True)
        pred_sentence = ' '.join(pred_words[:-1])

        # replace the slot_val place holder with real value
        sentence = post_process(pred_sentence, dia_act['inform_slots'],
                                slot_dict)

        print 'Test case', lineindex
        print 'Dia-Act:', l
        print 'Pred_Template:', pred_sentence
        print 'NLG:', sentence