有一个用到翻译引擎的项目需要对自研产品和友商进行效果比对测试。提供的参考数据为各语种方向的测试文本和标准答案。形式如下
工作思路是读取excel内容,分别请求两个产品的相关翻译接口,把两者结果输出到excel中后,人工进行主观体验效果好坏。
两者翻译接口都比较容易。
自研接口
友商接口
接下来直接上代码
#coding=utf-8
import xlrd
import xlwt
import requests
import json
import time
def readxsl(path,out_path):
##读取xlsx内容
wb = xlrd.open_workbook(path)
sheet_name_list = wb.sheet_names()
print (sheet_name_list)
#time.sleep(1000)
wr = xlwt.Workbook(encoding='utf-8')
#for i in range(4):
for i in range(len(sheet_name_list)):
print (sheet_name_list[i])
aim_sheet = wb.sheet_by_name(sheet_name_list[i])
#aim_sheet = wb.sheet_by_index(0)
#print (aim_sheet.name,aim_sheet.ncols,aim_sheet.nrows)
###准备写入新的sheet
worksheet = wr.add_sheet(sheet_name_list[i])
######访问自研和友商翻译引擎服务
ifly_host = "http://ipport/tuling/itrans/v2/single"
niu_host = "http://ipport/NiuTransServer/translation"
#for i in range(aim_sheet.nrows):
for j in range(20):
ifly_body ={
"params":"type=kocn;outfmt=txt",
"txt":aim_sheet.col_values(0)[j]
}
niu_body={
"from":sheet_name_list[i],
"to":"zh",
"src_text":aim_sheet.col_values(0)[j],
"apikey":"xfzf"
}
#ifly_res = requests.get(url=ifly_host,params=ifly_body)
niu_res = requests.get(url=niu_host,params=niu_body)
#ifly_json_itrans=json.loads(ifly_res.text)
niu_json_itrans = json.loads(niu_res.text)
#print (ifly_json_itrans["body"]["target"])
print (niu_json_itrans["tgt_text"])
##将结果写入sheet
worksheet.write(j, 0, aim_sheet.col_values(0)[j])
worksheet.write(j, 1, aim_sheet.col_values(1)[j])
#worksheet.write(j, 2, ifly_json_itrans["body"]["target"])
worksheet.write(j,3,niu_json_itrans["tgt_text"])
##保存xls
wr.save(out_path)
if __name__ =="__main__":
##测试 传入源文件和写入文件路径
readxsl("输入.xlsx",'输出.xls')
#writexls('D:/test_xlwt.xls')
# wr = xlwt.Workbook(encoding="utf-8")
# for i in range(10):
# name ="name"+str(i)
# webbok = wr.add_sheet(name)
# webbok.write(1,1,"hello")
# wr.save("d:/1.xls")
由于源excel是多个sheet页的,所以已sheet_name进行了循环处理,同样输入了多个sheet页。结果如下,自研的服务暂未开发好所以暂时没有调用。