安装Django
pip安装最方便
pip install Django
创建BleuTest项目
django-admin startproject BleuTest
编写简单的HTML页面
在BleuTest主目录下创建templates文件夹,将htmlwen文件放在这个文件夹下;
修改BleuTest–>BleuTest目录下setting.py文件,将TEMPLATES下的’DIRS’值改为:[BASE_DIR+"/templates",]
html页面传入两份文件,一份待测译文,一份标准译文,点击确定后台计算bleu,计算结束后将结果返回到{{ rlt }}显示:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>BLEU</title>
</head>
<body>
<h2>计算BLEU</h2>
<hr><br>
<form id="upload-form" action="/bleu" method="post" enctype="multipart/form-data" >
<b>标准译文:</b>
<input type="file" id="upload1" name="upload1" /> <br />
<b>翻译结果:</b>
<input type="file" id="upload2" name="upload2" /> <br />
<input type="submit" value="计算BLEU" />
</form>
<p>{{ rlt }}</p><br>
<hr>
<p><small><i>注:上传标准译文文档及翻译文档,系统自动返回BLEU值。</i></small></p>
</body>
</html>
修改视图脚本view.py
这里导入计算函数(compute_bleu)时要从根目录开始,否则会找不到函数
import chardet
from django.shortcuts import render
from django.views.decorators import csrf
from django.views.decorators.csrf import csrf_exempt
from BleuTest.bleu_script.nltk_bleu import compute_bleu
# 接收POST请求数据
@csrf_exempt
def bleu(request):
ctx ={}
if request.method == "POST":
myfile1 = request.FILES.get("upload1", None)
myfile2 = request.FILES.get("upload2", None)
if not (myfile1 and myfile2):
ctx['rlt'] = "请上传文档!"
else:
context1, context2 = "", ""
for c in myfile1.chunks():
context1 += c.decode('utf-8')
for c in myfile2.chunks():
context2 += c.decode('utf-8')
ctx['rlt'] = "%s & %s BLEU: %.2f" % (myfile1.name, myfile2.name,
compute_bleu(context1, context2)*100)
return render(request, "bleu.html", ctx)
编写计算BLEU的脚本
这里用到的是NLTK的bleu计算函数,用到了平滑函数,否则测试语句长度小于n-gram(默认为4)时会计算不准确。
import nltk
from nltk.translate.bleu_score import corpus_bleu, SmoothingFunction
def compute_bleu(reference=None, translation=None):
if reference and translation:
reference = reference.split('\n')
translation = translation.split('\n')
inf_tok = [[nltk.tokenize.word_tokenize(i)] for i in reference]
tra_tok = [nltk.tokenize.word_tokenize(t) for t in translation]
chencherry = SmoothingFunction()
return corpus_bleu(inf_tok, tra_tok, smoothing_function=chencherry.method7)
else:
return "文件内容为空!"
修改URL脚本
from django.urls import path
from . import view
urlpatterns = [
path('bleu', view.bleu),
]
启动服务
python manage.py runserver 0.0.0.0:8000
在浏览器中输入http://localhost:8000/bleu就可以进入页面
计算示例: