rouge与pyrouge使用事项

1.rouge介绍

ROUGE评价方法与pyramid,BLUE方法一起作为评价自动摘要质量的内部评价方法的三大中流砥柱。

  • ROUGE:recall-oriented understand for gisting evalution
  • 2004年,Chin-Yew Lin 提出
  • 基本思想
    由多个专家分别生成人工摘要,构成标准摘要集,将系统生成的自动摘要与人工生成的标准摘要相比较,通过统计二者之间重叠的基本单元(n元语法,词序列和词对)的数目,来评价摘要的质量。通过多专家人工摘要的对比,提高评价系统的稳定性和健壮性。
    这个方法已经成为评价摘要技术的通用标准之一。

2.评价标准

  • ROUGE-N
  • ROUGE-L
  • ROUGE-S
  • ROUGE-W
  • ROUGE-SU

3. ROUGE-N(N-gram Co-Occurrence Statistics)

  • N-gram模型


    n-gram模型.png

句子S由词序列[图片上传失败...(image-a49417-1542860196809)]组成,计算句子S出现的概率 [图片上传失败...(image-31d2bd-1542860196809)])最简单,最直接的方法是计数后做除法,也就是最大似然估计(MLE),但是这样做会面临数据稀疏严重和参数空间巨大的问题,导致无法实用。于是一般采用n-gram模型,n-gram模型基于马尔科夫假设,他认为,一个词的出现仅仅依赖于他前面出现的有限的一个或者几个词。


rouge-n介绍.png

其中分母是n-gram的个数,分子是参考摘要和自动摘要共有的n-gram的个数。举例说明一下:

自动摘要Y(一般是自动生成的):
the cat was found under the bed
参考摘要,X1 (gold standard ,人工生成的):
the cat was under the bed
summary的1-gram、2-gram如下,N-gram以此类推:



rouge_1(X1,Y)= 6/6=1.0,分子是待评测摘要和参考摘要都出现的1-gram的个数,分子是参考摘要的1-gram个数。(其实分母也可以是待评测摘要的,但是在精确率和召回率之间,我们更关心的是召回率Recall,同时这也和上面ROUGN-N的公式相同)
同样,Rouge_2(X1,Y)=4/5=0.8

4. ROUGE-L




image.png


5.ROUGE-W

ROUGE-W是ROUGW-L的改进版,例如下面这种情况



图中,X 是参考文摘,Y1,Y2是两个待评测文摘,明显Y1 要优于Y2 ,因为Y1 可以和参考摘要X 连续匹配,但是Rouge_L(X,Y1)=Rouge_L(X,Y2) ,针对这个问题论文作者提出了改进的方案—加权最长公共子序列(Weighted Longest Common Subsequence)。



6.ROUGE-S

即使用了skip-grams,在参考摘要和待评测摘要进行匹配时,不要求gram之间必须是连续的,可以“跳过”几个单词,比如skip-bigram,在产生grams时,允许最多跳过两个词。比如“cat in the hat”的 skip-bigrams 就是 “cat in, cat the, cat hat, in the, in hat, the hat”.

7.总结

8.rouge与pyrouge的安装

使用pyrouge前,需要安装好rouge.
下面两个链接有相应的安装工具和教程

https://blog.csdn.net/qq_32458499/article/details/78994388

https://blog.csdn.net/Hay54/article/details/78744912

注意:Github上的ROUGE已经不可以用了。

9.使用

def rouge(ref, hyp, log_path):
    assert len(ref) == len(hyp)
    ref_dir = log_path + 'reference/'
    cand_dir = log_path + 'candidate/'
    if not os.path.exists(ref_dir):
        os.mkdir(ref_dir)
    if not os.path.exists(cand_dir):
        os.mkdir(cand_dir)
    for i in range(len(ref)):
        with codecs.open(ref_dir+"%06d_reference.txt" % i, 'w', 'utf-8') as f:
            f.write(" ".join(ref[i]).replace(' ', '') + '\n')
        with codecs.open(cand_dir+"%06d_candidate.txt" % i, 'w', 'utf-8') as f:
            f.write(" ".join(hyp[i]).replace(' ', '').replace('<unk>', 'UNK') + '\n')

    r = pyrouge.Rouge155()
    r.model_filename_pattern = '#ID#_reference.txt'
    r.system_filename_pattern = '(\d+)_candidate.txt'
    r.model_dir = ref_dir
    r.system_dir = cand_dir
    logging.getLogger('global').setLevel(logging.WARNING)
    rouge_results = r.convert_and_evaluate()
    scores = r.output_to_dict(rouge_results)
    recall = [round(scores["rouge_1_recall"] * 100, 2),
              round(scores["rouge_2_recall"] * 100, 2),
              round(scores["rouge_l_recall"] * 100, 2)]
    precision = [round(scores["rouge_1_precision"] * 100, 2),
                 round(scores["rouge_2_precision"] * 100, 2),
                 round(scores["rouge_l_precision"] * 100, 2)]
    f_score = [round(scores["rouge_1_f_score"] * 100, 2),
               round(scores["rouge_2_f_score"] * 100, 2),
               round(scores["rouge_l_f_score"] * 100, 2)]
    print("F_measure: %s Recall: %s Precision: %s\n"
              % (str(f_score), str(recall), str(precision)))
    
    with codecs.open(ref_dir+"rougeScore", 'w+', 'utf-8') as f:
        f.write("F_measure: %s Recall: %s Precision: %s\n"
              % (str(f_score), str(recall), str(precision)))
    return f_score[:], recall[:], precision[:]

首先记得:import pyrouge
这里的ref是生成的摘要,hyp是系统参考摘要
regerence文件夹下,文件名为reference00.txt, 00代表数字编号

一定要记住
文件中都是一行一个句子!。
TXT文件中好像不允许出现'<'符号,例如'<unk>',如果有可能会报错!

参考

https://blog.csdn.net/lime1991/article/details/42521029
https://blog.csdn.net/qq_25222361/article/details/78694617

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转载自http://www.cnblogs.com/Determined22/ 两周以前读了些文档自动摘要的论文,...
    nightwish夜愿阅读 10,655评论 0 6
  • 分手时 没什么情景。 只是我这么多年一直在想,一个人想分手,另一个人说不同意。 说我不同意到底有什么用呢。
    摄影师柳丁阅读 195评论 3 1
  • 美娜美阅读 90评论 0 1