in utils/compute_score.py [0:0]
def compute_text_generation(file_path):
references = []
candidates = []
sub_task_references = {}
sub_task_candidates = {}
with open(file_path, 'r', encoding='utf-8') as input_file:
print("Reading\t" + file_path)
sample_list = json.load(input_file)
sample_list = pd.json_normalize(sample_list, 'outputs')
for _, line in sample_list.iterrows():
if line['__raw.task'] == '金融文本生成':
# compute all score
if not line['response']:
line['response'] = 'None'
references.append(" ".join(jieba.lcut(line['__raw.output'])))
candidates.append(" ".join(jieba.lcut(line['response'])))
# compute sub_task score
sub_task = line['__raw.sub_task']
if sub_task not in sub_task_references:
sub_task_references[sub_task] = []
sub_task_candidates[sub_task] = []
sub_task_references[sub_task].append(" ".join(jieba.lcut(line['__raw.output'])))
sub_task_candidates[sub_task].append(" ".join(jieba.lcut(line['response'])))
# result
print(f"task: 金融文本生成")
_, _, rougel = rouge_score(references, candidates)
bert = bert_score(references, candidates)
print('\n')
sub_task_tg, sub_task_tg_bert = {}, {}
for sub_task, refs in sub_task_references.items():
print(f"Sub-task: {sub_task}")
candidates = sub_task_candidates[sub_task]
_, _, rouge_l = rouge_score(refs, candidates)
bert_sub_task = bert_score(refs, candidates)
sub_task_tg[sub_task] = rouge_l
sub_task_tg_bert[sub_task] = bert_sub_task
print('\n')
return rougel, sub_task_tg, bert, sub_task_tg_bert