File size: 1,454 Bytes
86ecd3d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import json
import argparse
import sys
import numpy as np
import jieba
from nltk.translate.bleu_score import sentence_bleu, SmoothingFunction
from nltk import ngrams
def load_file(filename):
data = []
with open(filename, "r") as f:
for line in f.readlines():
data.append(json.loads(line))
f.close()
return data
def proline(line):
return " ".join([w for w in jieba.cut("".join(line.strip().split()))])
def compute(golden_file, pred_file, return_dict=True):
golden_data = load_file(golden_file)
pred_data = load_file(pred_file)
if len(golden_data) != len(pred_data):
raise RuntimeError("Wrong Predictions")
num = 0
for g, p in zip(golden_data, pred_data):
if isinstance(g["label"], str):
l = int(g["label"].strip())
elif isinstance(g["label"], int):
l = g["label"]
else:
raise Exception("Data type error")
if isinstance(p["label"], str):
p = int(p["label"].strip())
elif isinstance(p["label"], int):
p = p["label"]
else:
raise Exception("Data type error")
if l == p:
num += 1
return {'accuracy': float(num)/len(golden_data)}
def main():
argv = sys.argv
print("预测结果:{}, 测试集: {}".format(argv[1], argv[2]))
print(compute(argv[2], argv[1]))
if __name__ == '__main__':
main()
|