model.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:SGAITagger 作者: zhiweiuu 项目源码 文件源码
def run_conlleval(X_words_test, y_test, y_pred, index2word, index2chunk, pad_id=0):
    '''
    Runs the conlleval script for evaluation the predicted IOB-tags.
    '''
    url = 'http://www.cnts.ua.ac.be/conll2000/chunking/conlleval.txt'
    path = get_file('conlleval',
                    origin=url,
                    md5_hash='61b632189e5a05d5bd26a2e1ec0f4f9e')

    p = Popen(['perl', path], stdout=PIPE, stdin=PIPE, stderr=STDOUT)

    y_true = np.squeeze(y_test, axis=2)

    sequence_lengths = np.argmax(X_words_test == pad_id, axis=1)
    nb_samples = X_words_test.shape[0]
    conlleval_input = []
    for k in range(nb_samples):
        sent_len = sequence_lengths[k]
        words = list(map(lambda idx: index2word[idx], X_words_test[k][:sent_len]))
        true_tags = list(map(lambda idx: index2chunk[idx], y_true[k][:sent_len]))
        pred_tags = list(map(lambda idx: index2chunk[idx], y_pred[k][:sent_len]))
        sent = zip(words, true_tags, pred_tags)
        for row in sent:
            conlleval_input.append(' '.join(row))
        conlleval_input.append('')
    print()
    conlleval_stdout = p.communicate(input='\n'.join(conlleval_input).encode())[0]
    print(blue(conlleval_stdout.decode()))
    print()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号