python - Rouge Score 附加列表

标签 python nlp text-mining summarization rouge

我有一个文本摘要项目。在这个项目中,我确保按顺序总结数百篇文本。我还得到了这些摘要的 Rouge 分数。不过,我必须先将 Rouge 分数保留在列表中,然后才能进行统计。我不知道该怎么做。你能帮我吗?

from rouge_score import rouge_scorer
scorer = rouge_scorer.RougeScorer(['rouge1'])
scorer.score(hyp,ref)
scores.append(scorer.score(hyp,ref))

示例结果:

[{'rouge1': Score(precision=0.46017699115044247, recall=0.45217391304347826, 
fmeasure=0.45614035087719296)},
{'rouge1': Score(precision=0.1693121693121693, recall=0.2831858407079646, 
fmeasure=0.21192052980132448)}]

当然,我无法直接访问结果。

最佳答案

如果您想直接访问 Score 对象,您应该定义字典的键 ('rouge1')。
因此, scores.append(scorer.score(hyp,ref)) 将更改为 scores.append(scorer.score(hyp,ref)['rouge1']) .

以下代码是一个更通用的版本,用于计算每个文档的 ROUGE 指标并将结果分别记录在单个字典中:

# importing the native rouge library
from rouge_score import rouge_scorer

# a list of the hypothesis documents
hyp = ['This is the first sample', 'This is another example']
# a list of the references documents
ref = ['This is the first sentence', 'It is one more sentence']

# make a RougeScorer object with rouge_types=['rouge1']
scorer = rouge_scorer.RougeScorer(['rouge1'])

# a dictionary that will contain the results
results = {'precision': [], 'recall': [], 'fmeasure': []}

# for each of the hypothesis and reference documents pair
for (h, r) in zip(hyp, ref):
    # computing the ROUGE
    score = scorer.score(h, r)
    # separating the measurements
    precision, recall, fmeasure = score['rouge1']
    # add them to the proper list in the dictionary
    results['precision'].append(precision)
    results['recall'].append(recall)
    results['fmeasure'].append(fmeasure)

输出将如下所示:

{'fmeasure': [0.8000000000000002, 0.22222222222222224],
 'precision': [0.8, 0.2],
 'recall': [0.8, 0.25]}

此外,我会建议 rouge library这是 ROUGE paper 的另一个实现。结果可能略有不同,但它将引入一些有用的功能,包括通过传递整个文本文档来计算粗略指标并计算所有文档的平均结果的可能性。

关于python - Rouge Score 附加列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67390427/

相关文章:

r - 使用 R - "inverse"拼写检查器搜索字符向量中单词的拼写错误

R-Project 没有适用于 'meta' 的方法应用于类 "character"的对象

python - ManyToMany 字段默认选择所有模型实例/更改默认 get_queryset

python - 如何从列表列表中过滤特定的 POS 标签到单独的列表?

java - 无法使用 LanguageTool Java API 正确进行拼写检查

python - 解析 penn 语法树以提取其语法规则

string - 获取字符串向量元素之间的最小共享部分

python - 多索引数据框的 lexsort_depth 到底是什么?

python - 区域边界的 Numpy 检测

nlp - BERT - 是否需要添加新的 token 以在特定领域的环境中进行训练?