为NLP计算标准BLEU分数的Python包装器
bleu的Python项目详细描述
bleu(Python包)
自然语言生成标准BLEU求值(NLG)的Python包装器。在
- GitHub项目:https://github.com/zhijing-jin/bleu。在
- PyPI包:
pip install
^{}
安装
要求:Python 3
选项1:安装pip包
pip install --upgrade bleu
选项2:从源代码构建
^{pr2}$怎么跑
计算BLEU的最标准方法是Moses' script for detokenized BLEU。这个包提供了对它的简单调用。在
函数1:计算列表的BLEU
如果你只想检查一个假设(句子列表):
>>>frombleuimportlist_bleu>>>ref=['it is a white cat .','wow , this dog is huge .']>>>ref1=['This cat is white .','wow , this is a huge dog .']>>>hyp=['it is a white kitten .','wowww , the dog is huge !']>>>hyp1=["it 's a white kitten .",'wow , this dog is huge !']>>>list_bleu([ref],hyp)34.99>>>list_bleu([ref,ref1],hyp1)57.91
如果你想检查多个假设(几个句子列表):
>>>frombleuimportmulti_list_bleu>>>multi_list_bleu([ref,ref1],[hyp,hyp1])[34.99,57.91]
{{cdu}如果你不想使用{cdu},那就使用{cdu}不可取:
>>>list_bleu([ref],hyp,detok=False)39.76# or if you want to test multiple hypotheses>>>multi_list_bleu([ref,ref1],[hyp,hyp1],detok=False)[39.76,47.47]
verbose=True
:如果有意外错误,您可能希望通过verbose=True
检查中间步骤。在
函数2:计算文件的BLEU
如果只想检查一个假设文件:
# if you already have the following files>>>frombleuimportfile_bleu>>>hyp_file='data/hyp0.txt'>>>ref_files=['data/ref0.txt','data/ref1.txt']>>>file_bleu(ref_files,hyp_file)34.99
如果要检查多个假设文件:
>>>frombleuimportmulti_file_bleu>>>hyp_file1='data/hyp1.txt'>>>bleus=multi_file_bleu(ref_files,[hyp_file,hyp_file1])[34.99,57.91]
detok=True
:如果要计算(不推荐)标记化的bleu,请设置它。在
verbose=True
:如果要检查bleu计算是如何进行的,请设置它:
>>>bleu=file_bleu(ref_files,hyp_file,verbose=True)[Info]ValidReferenceFiles:['data/ref0.txt','data/ref1.txt'][Info]ValidHypothesisFiles:['data/hyp0.txt'][Info]#lines in each file: 2[cmd]perldetokenizer.perl-len<data/ref0.txt>data/ref0.detok.txt2>/dev/null[cmd]perldetokenizer.perl-len<data/ref1.txt>data/ref1.detok.txt2>/dev/null[cmd]perldetokenizer.perl-len<data/hyp0.txt>data/hyp0.detok.txt2>/dev/null[cmd]perlmulti-bleu-detok.perldata/ref0.detok.txtdata/ref1.detok.txt<data/hyp0.detok.txt2-refbleufordata/hyp0.detok.txt:34.99>>>bleu34.99
选项3:删除文件
>>>frombleuimportdetok_files>>>detok_ref_files=detok_files(ref_files,tmp_dir='./data',file_prefix='ref_dtk',verbose=True)[cmd]perl./TMP_DIR/detokenizer.perl-len<data/ref0.txt>data/ref_dtk0.txt2>/dev/null[cmd]perl./TMP_DIR/detokenizer.perl-len<data/ref1.txt>data/ref_dtk1.txt2>/dev/null>>>detok_ref_files['data/ref_dtk0.txt','data/ref_dtk1.txt']
如果出现意外输出
检查python文件bleu.py并对其进行调整。在
联系人
如果您还有更多问题,可以查看常见的Q&A,或者提出一个新的GitHub问题。在
如有紧急需要,请联系作者Zhijing Jin (Miss)。在
- 项目
标签: