用于自动摘要的完整ROUGE度量的快速python实现。

rouge-metric的Python项目详细描述


胭脂公制

PyPIUnitTestcodecovLicense: MIT

用于自动摘要评估的完整ROUGE度量的快速Python实现。官方的ROUGE-1.5.5.plPerl脚本的Python包装器也可用。在

特点

对于Perl脚本包装器:

  • 易于安装:无需手动下载和配置Perl脚本。只要安装了Perl,它就可以工作。在
  • 跨平台:支持Linux、macOS和Windows机器。在
  • 优雅的CLI和API:提供了用户友好的API和命令行工具

对于Python实现:

  • 完全胭脂支持:实现胭脂-N,胭脂-L,胭脂-W,胭脂-S和胭脂-SU指标,并支持多参考评估。在
  • 高速:纯Python实现,无需调用另一个进程。在
  • 正确性:在单个文档场景中生成与ROUGE-1.5.5.pl相同的结果。多文档结果可能略有不同,因为我们不采用引导重采样。在
  • 灵活和多语言:我们只关注语言不可知标记,并将一个句子视为一系列标记。语言感知的预处理和标记化是用户实现的自由。您可以使用不同的方法标记不同的语言,例如nltk表示英语,jieba表示中文。在

安装

从PyPI安装一个稳定的版本。在

pip install rouge-metric

或者从GitHub安装最新版本。在

^{pr2}$

对于希望使用ROUGE-1.5.5.pl脚本的Windows用户,请安装Strawberry Perl,并将其二进制文件夹添加到PATH。在

快速入门

使用命令行工具

基本用法:

rouge-metric [options] hypothesis reference [reference ...]

其中的选项几乎与ROUGE-1.5.5.pl脚本相同。运行rouge-metric -h获取更多详细信息。在

对于具有单个引用的单个文档,请指定两个文件。在

rouge-metric sample/hypotheses/summary1.txt sample/references/summary1.1.txt -n 2 -w 1.2 -U -2 4

对于具有多个引用的单个文档,请指定一个假设文件和多个引用文件。在

rouge-metric sample/hypotheses/summary1.txt sample/references/summary1.1.txt sample/references/summary1.2.txt -n 2 -w 1.2 -U -2 4

对于具有多个引用的多个文档,请指定两个文件夹。在

rouge-metric sample/hypotheses/ sample/references/ -n 2 -w 1.2 -U -2 4

它直接调用ROUGE-1.5.5.pl脚本并获得原始输出。在

---------------------------------------------
A ROUGE-1 Average_R: 0.51822 (95%-conf.int. 0.42105 - 0.61538)
A ROUGE-1 Average_P: 0.55556 (95%-conf.int. 0.44444 - 0.66667)
A ROUGE-1 Average_F: 0.53622 (95%-conf.int. 0.43243 - 0.64000)
---------------------------------------------
A ROUGE-2 Average_R: 0.19519 (95%-conf.int. 0.11765 - 0.27273)
A ROUGE-2 Average_P: 0.21250 (95%-conf.int. 0.12500 - 0.30000)
A ROUGE-2 Average_F: 0.20346 (95%-conf.int. 0.12121 - 0.28572)
---------------------------------------------
A ROUGE-L Average_R: 0.51822 (95%-conf.int. 0.42105 - 0.61538)
A ROUGE-L Average_P: 0.55556 (95%-conf.int. 0.44444 - 0.66667)
A ROUGE-L Average_F: 0.53622 (95%-conf.int. 0.43243 - 0.64000)
---------------------------------------------
A ROUGE-W-1.2 Average_R: 0.33608 (95%-conf.int. 0.26618 - 0.40599)
A ROUGE-W-1.2 Average_P: 0.47348 (95%-conf.int. 0.38525 - 0.56172)
A ROUGE-W-1.2 Average_F: 0.39308 (95%-conf.int. 0.31483 - 0.47132)
---------------------------------------------
A ROUGE-S4 Average_R: 0.25495 (95%-conf.int. 0.13846 - 0.37143)
A ROUGE-S4 Average_P: 0.29167 (95%-conf.int. 0.15000 - 0.43333)
A ROUGE-S4 Average_F: 0.27200 (95%-conf.int. 0.14400 - 0.40000)
---------------------------------------------
A ROUGE-SU4 Average_R: 0.31495 (95%-conf.int. 0.19512 - 0.43478)
A ROUGE-SU4 Average_P: 0.35527 (95%-conf.int. 0.21053 - 0.50000)
A ROUGE-SU4 Average_F: 0.33382 (95%-conf.int. 0.20253 - 0.46511)

使用Perl脚本API

除了命令行工具之外,您还可以通过编程方式使用ROUGE-1.5.5.pl。请注意,它只适用于英语语料库。对于非英语摘要,请改用Python实现,或者在求值之前将标记转换为用空格分隔的整数。在

fromrouge_metricimportPerlRougerouge=PerlRouge(rouge_n_max=3,rouge_l=True,rouge_w=True,rouge_w_weight=1.2,rouge_s=True,rouge_su=True,skip_gap=4)# Load summary results and evaluatehypotheses=['how are you\ni am fine',# document 1: hypothesis'it is fine today\nwe won the football game',# document 2: hypothesis]references=[['how do you do\nfine thanks',# document 1: reference 1'how old are you\ni am three',# document 1: reference 2],['it is sunny today\nlet us go for a walk',# document 2: reference 1'it is a terrible day\nwe lost the game',# document 2: reference 2]]scores=rouge.evaluate(hypotheses,references)print(scores)

输出如下

{
    'rouge-1': {
        'r': 0.51822, 'r_conf_int': (0.42105, 0.61538),
        'p': 0.55556, 'p_conf_int': (0.44444, 0.66667),
        'f': 0.53622, 'f_conf_int': (0.43243, 0.64)
    },
    'rouge-2': {...}, 'rouge-3': {...}, 'rouge-l': {...},
    'rouge-w-1.2': {...}, 'rouge-s4': {...}, 'rouge-su4': {...}
}

您还可以评估现有文件中的摘要。在

fromrouge_metricimportPerlRougehypothesis_dir='sample/hypotheses'reference_dir='sample/references'scores=PerlRouge().evaluate_from_files(hypothesis_dir,reference_dir)print(scores)

使用Python实现

还提供了一个快速的Python实现。它有类似的API,并支持多种语言。在

fromrouge_metricimportPyRouge# Load summary resultshypotheses=['how are you\ni am fine',# document 1: hypothesis'it is fine today\nwe won the football game',# document 2: hypothesis]references=[['how do you do\nfine thanks',# document 1: reference 1'how old are you\ni am three',# document 1: reference 2],['it is sunny today\nlet us go for a walk',# document 2: reference 1'it is a terrible day\nwe lost the game',# document 2: reference 2]]# Evaluate document-wise ROUGE scoresrouge=PyRouge(rouge_n=(1,2,4),rouge_l=True,rouge_w=True,rouge_w_weight=1.2,rouge_s=True,rouge_su=True,skip_gap=4)scores=rouge.evaluate(hypotheses,references)print(scores)

输出如下

{
    'rouge-1': {
        'r': 0.5182186234817814,
        'p': 0.5555555555555556,
        'f': 0.5362379555927943
    },
    'rouge-2': {...}, 'rouge-4': {...}, 'rouge-l': {...},
    'rouge-w-1.2': {...}, 'rouge-s4': {...}, 'rouge-su4': {...}
}

默认情况下,在文档中,句子用'\n'分隔,标记用空格分隔。这个标记化过程可以进一步定制。例如

fromrouge_metricimportPyRouge# Pre-process and tokenize the summaries as you likehypotheses=[['how are you'.split(),'i am fine'.split()],# document 1: hypothesis['it is fine today'.split(),'we won the football game'.split()],# document 2: hypothesis]references=[[['how do you do'.split(),'fine thanks'.split()],# document 1: reference 1['how old are you'.split(),'i am three'.split()],# document 1: reference 2],[['it is sunny today'.split(),'let us go for a walk'.split()],# document 2: reference 1['it is a terrible day'.split(),'we lost the game'.split()],# document 2: reference 2]]# Evaluate on tokenized documentsrouge=PyRouge(rouge_n=(1,2,4),rouge_l=True,rouge_w=True,rouge_w_weight=1.2,rouge_s=True,rouge_su=True,skip_gap=4)scores=rouge.evaluate_tokenized(hypotheses,references)print(scores)

许可证

此项目位于MIT License下。在

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java GridLayout超出了它的大小   java为什么SSHJ的最大写入大小是32KB?   部署后,java无法查看网站。war文件到tomcat   java如何使用gradle将javafx应用程序部署为可执行jar或exe?   java比较HashMap中的键和值   使用注入java和spring boot的RestTemplate类进行单元测试   java如何在eclipse中找到maven项目的原型?   java继承和实用程序方法及变量   java在Joptionpane中显示多行?   当location==null时,java会更改按钮的可见性   java为什么Google Drive getExportLinks返回空值?   java在使用自定义消息完成for循环后引发异常   curl如何使用docker运行undertow java应用程序