使用FilesRouge()评估机器生成的摘要与人工摘要的相似度
发布时间:2023-12-17 02:21:29
要使用FilesRouge()评估机器生成的摘要与人工摘要的相似度,首先需要安装rouge库。可以使用以下命令进行安装:
!pip install rouge
然后,导入必要的库和模块,创建一个Rouge对象,并使用FilesRouge()方法进行评估。下面是一个使用示例,对于机器生成的摘要和人工摘要列表,计算其相似度:
from rouge import Rouge
from rouge import FilesRouge
# 创建Rouge对象
rouge = Rouge()
# 机器生成的摘要
machine_summaries = [
"This is a machine-generated summary.",
"The machine generated this summary automatically."
]
# 人工摘要
human_summaries = [
"This is a human-created summary.",
"A human being wrote this summary."
]
# 保存机器生成的摘要和人工摘要到文件
with open("machine_summaries.txt", "w") as file:
for summary in machine_summaries:
file.write(summary + "
")
with open("human_summaries.txt", "w") as file:
for summary in human_summaries:
file.write(summary + "
")
# 创建FilesRouge对象
files_rouge = FilesRouge()
# 评估机器生成的摘要与人工摘要的相似度
scores = files_rouge.get_scores("machine_summaries.txt", "human_summaries.txt")
# 打印相似度评分
for score in scores:
print(f"Rouge-1: {score['rouge-1']['f']}")
print(f"Rouge-2: {score['rouge-2']['f']}")
print(f"Rouge-L: {score['rouge-l']['f']}")
在该示例中,我们使用了两个机器生成的摘要和两个人工摘要进行评估,可以根据需要更改这些摘要列表的内容。
FilesRouge()方法根据机器生成的摘要和人工摘要的文件进行评估。通过调用get_scores()方法,我们可以获得Rouge-1,Rouge-2和Rouge-L的相似度评分。接下来,我们打印每个评分的结果。
请注意,要使用FilesRouge()方法进行评估,摘要需要保存在文本文件中,每个摘要一行。在上述示例中,我们将机器生成的摘要保存在machine_summaries.txt文件中,将人工摘要保存在human_summaries.txt文件中。
