예제 #1
0
def run_test(text_sentences, sentences, sentence_pos, trans_probs, em_probs, proto_index, out_file, dictionary=None):
    out_text_sents = []
    correct = 0
    total = 0
    s = 0
    confusion_matrix = numpy.zeros((n_pos, n_pos))
    for sentence, sent_pos in zip(sentences, sentence_pos):
        pred_pos = viterbi(sentence, trans_probs, em_probs, dictionary)
        w = 0
        out_text_sent = []
        for pred, gold, word in zip(pred_pos, sent_pos, sentence):
            total += 1
            if pred == gold:
                correct += 1
            out_text_sent.append((text_sentences[s][w], pos_tags[pred]))
            w += 1
            confusion_matrix[pred, gold] += 1
        out_text_sents.append(out_text_sent)
        s += 1
    print '------------------'
    print 'accuracy:', correct / float(total)
    print 'many to one:', confusion_matrix.max(axis=1).sum() / float(confusion_matrix.sum())
    print '------------------'
    if out_file is not None:
        load_and_save.write_sentences_to_file(out_text_sents, out_file)
예제 #2
0
def run_test(text_sentences,
             sentences,
             sentence_pos,
             trans_probs,
             em_probs,
             proto_index,
             out_file,
             dictionary=None):
    out_text_sents = []
    correct = 0
    total = 0
    s = 0
    confusion_matrix = numpy.zeros((n_pos, n_pos))
    for sentence, sent_pos in zip(sentences, sentence_pos):
        pred_pos = viterbi(sentence, trans_probs, em_probs, dictionary)
        w = 0
        out_text_sent = []
        for pred, gold, word in zip(pred_pos, sent_pos, sentence):
            total += 1
            if pred == gold:
                correct += 1
            out_text_sent.append((text_sentences[s][w], pos_tags[pred]))
            w += 1
            confusion_matrix[pred, gold] += 1
        out_text_sents.append(out_text_sent)
        s += 1
    print '------------------'
    print 'accuracy:', correct / float(total)
    print 'many to one:', confusion_matrix.max(axis=1).sum() / float(
        confusion_matrix.sum())
    print '------------------'
    if out_file is not None:
        load_and_save.write_sentences_to_file(out_text_sents, out_file)