目录
一、前言
二、服务开通
三、Java调用接口实例演示
1.文本翻译
1.代码展示
2.术语文本翻译
3.对照文本翻译
4.注意事项
2.文档翻译
1.调用流程示意图
2.官网Demo详情(示例只有上传并翻译流程的)
3.注意(这里很容易出错)
4.官方开发文档
四、SDK的Java调用接口实例演示
小牛翻译提供文本翻译+文档翻译的API接口:
支持454种语言互译(包括常见的中英日韩俄等语种)
支持多种常见文档类型(Word、PPT、PDF(不支持扫描件)、Excel)
另:每天提供免费的20万字符流量+100页文档页数(调用时需要)
首先需要在官网注册一个账号,获取我们后续需要的【API-KEY】和【APPID】。
注:
调用文本翻译只需用【API-KEY】即可,
调用文档翻译是【API-KEY】和【APPID】都需要。
获取方式:
public static void main(String[] args) {
RestTemplate restTemplate = new RestTemplate();
String url = "https://api.niutrans.com/NiuTransServer/translation";//小牛api地址
Map map = new HashMap();
map.put("from","auto");//源语言设置为自动识别
map.put("to","en");//目标语言
map.put("apikey","这里要换成自己的apikey");//api密钥
map.put("src_text","作业好难");//翻译文本
ResponseEntity stringResponseEntity = restTemplate.postForEntity(url, map, String.class);
System.out.println(stringResponseEntity.getBody());//得到翻译内容
}
得出翻译结果
{
“tgt_text”:”Homework is so difficult”,
“from”:”zh”;
“to”:”en”;
}
如果不想添加术语库,但是还想用术语校正,也可以在翻译的时候把术语传进去
public static void main(String[] args) {
RestTemplate restTemplate = new RestTemplate();
String url = "https://api.niutrans.com/NiuTransServer/translation";
Map map = new HashMap();
map.put("from","zh");
map.put("to","en");
map.put("apikey","这里要换成自己的apikey");
map.put("src_text","你好世界");
map.put("dictflag",1);
map.put("dict","{\"你好\":\"nihao\"}");
ResponseEntity stringResponseEntity = restTemplate.postForEntity(url, map, String.class);
System.out.println(stringResponseEntity.getBody());
}
得到的结果就是把“你好”翻译成想要的字符串“nihao”啦
{
"tgt_text": "Nihao World",
"from": "zh",
"to": "en",
"align": {
"0": {
"0": {
"tgt": "Nihao World ",
"src": "你好世界"
}
}
}
}
如果需要原文译文对照接口,就调用下面的接口
public static void main(String[] args) {
RestTemplate restTemplate = new RestTemplate();
String url = "https://api.niutrans.com/NiuTransServer/translationAlign";
Map map = new HashMap();
map.put("from","zh");
map.put("to","en");
map.put("apikey","你的apikey");
map.put("src_text","你好世界!我爱祖国");
ResponseEntity stringResponseEntity = restTemplate.postForEntity(url, map, String.class);
System.out.println(stringResponseEntity.getBody());
}
得出结果:
{
"tgt_text": "Hello world! I love my motherland",
"from": "zh",
"to": "en",
"align": {
"0": {
"0": {
"tgt": "Hello world! ",
"src": "你好世界!"
},
"1": {
"tgt": "I love my motherland ",
"src": "我爱祖国"
}
}
}
}
每次文本请求的长度不要超过5000,不然会截取前5000个字符。
小牛翻译的文档翻译接口有两种方式:
①上传文件后,直接翻译
②先上传文件,获取文件页数,然后再用返回的fileNo调用翻译接口进行翻译
public class NiuTransDocumentApiDemo {
public static String BASE_URL = "https://api-doc.niutrans.com";
public static String UPLOAD_TRANS_URL = BASE_URL + "/documentTransApi/uploadAndTrans";//上传并翻译接口
public static String GET_INFO_URL = BASE_URL + "/documentTransApi/getInfo";//获取进度接口
public static String INTERRUPT_URL = BASE_URL + "/documentTransApi/interrupt";// 终止翻译接口
public static String DOWNLOAD_URL = BASE_URL + "/documentTransApi/download";//下载文件接口
public static String from = "原语语种,例如:zh";
public static String to ="目标语语种,例如:en";
public static String appId = "您的appId";//应用唯一标识,在'控制台->个人中心'中查看
public static String apikey = "您的apikey";//在'控制台->个人中心'中查看
public static String filePath = "C:\\Test.docx";//替换为你要翻译的文件路径
public static String saveFilePath = "C:\\";
//上传并翻译文件
public static String uploadAndTrans(){
TreeMap requestParamsMap = new TreeMap();
requestParamsMap.put("from",from);
requestParamsMap.put("to",to);
authStrGenerate(requestParamsMap);
requestParamsMap.put("file",new File(filePath));
String response = null;
try {
response = HttpUtil.post(UPLOAD_TRANS_URL, requestParamsMap);
} catch (Exception e) {
e.printStackTrace();
log.error("上传文档并翻译异常");
}
log.info("上传并翻译结果为:{}",response);
JSONObject jsonObject = JSONUtil.parseObj(response);
JSONObject dataObject = JSONUtil.parseObj(jsonObject.get("data"));
return dataObject.get("fileNo").toString();
}
//获取文件信息
public static JSONObject getDocumentInfo(String fileNo) {
TreeMap requestParamsMap = new TreeMap<>();
requestParamsMap.put("fileNo",fileNo);
authStrGenerate(requestParamsMap);
String response = null;
try {
response = HttpUtil.get(GET_INFO_URL, requestParamsMap);
} catch (Exception e) {
e.printStackTrace();
log.error("获取文档信息异常");
}
log.info("文档信息为:{}",response);
JSONObject jsonObject = JSONUtil.parseObj(response);
return JSONUtil.parseObj(JSONUtil.toJsonStr(jsonObject.get("data")));
}
//终止翻译中的文件
public static void interruptTrans(String fileNo) {
TreeMap requestParamsMap = new TreeMap<>();
requestParamsMap.put("fileNo",fileNo);
authStrGenerate(requestParamsMap);
String response = null;
try {
response = HttpUtil.get(INTERRUPT_URL, requestParamsMap);
} catch (Exception e) {
e.printStackTrace();
}
}
//下载文件
public static void downloadFile(String fileNo) {
TreeMap requestParamsMap = new TreeMap();
requestParamsMap.put("fileNo",fileNo);
requestParamsMap.put("type",1);
authStrGenerate(requestParamsMap);
String url = DOWNLOAD_URL + "?" +HttpUtil.toParams(requestParamsMap);
HttpUtil.downloadFile(url, FileUtil.file(saveFilePath));
}
//生成权限字符串
public static void authStrGenerate(TreeMap requestParamsMap) {
requestParamsMap.put("appId",appId);
requestParamsMap.put("apikey",apikey);
requestParamsMap.put("timestamp",System.currentTimeMillis()+"");
StringBuilder requestParamsStr = new StringBuilder("");
Set keys = requestParamsMap.keySet();
Iterator iterator = keys.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
requestParamsStr.append("&").append(key).append("=").append(requestParamsMap.get(key));
}
String paramsStr = requestParamsStr.toString().replaceFirst("&", "");
requestParamsMap.put("authStr",SecureUtil.md5(paramsStr));
requestParamsMap.remove("apikey");
}
public static void main(String[] args) {
String fileNo = uploadAndTrans();
JSONObject documentInfo = getDocumentInfo(fileNo);
while (Integer.parseInt(documentInfo.get("transStatus").toString()) == 103){
documentInfo = getDocumentInfo(fileNo);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(Integer.parseInt(documentInfo.get("transStatus").toString()) == 105){
downloadFile(fileNo);
}
}
}
在接口调用时,计算authstr权限字符串的时候比较容易出错,担心出错就用Demo中的方法就可以了。
如果没有你所用语言的Demo,要仔细阅读authstr生成规则及注意事项~
文档翻译Demo直通车:小牛翻译开放平台 - 机器翻译找小牛
如果你使用的是java、c#,python ,官方有现成的例子,可以复制使用,也可以下载示例文件到本地。
小牛翻译的文档翻译API接口也有java SDK的调用方式,不过目前是内部测试阶段,还没正式上线,需要拿到安装包后手动引入到自己的项目里,可以联系客服。
下面是一个调用SDK的参考示例:
public class TransNiuUtil {
public static void main(String[] args) {
SendRequestClient sendRequestClient = new DefaultSendRequestClient("appid","aopikey");
UploadTransRequestBo uploadTransRequestBo = new UploadTransRequestBo();
uploadTransRequestBo.setFile(new File("文件地址"));
uploadTransRequestBo.setFrom("原语语种");
uploadTransRequestBo.setTo("目标语语种");
BaseResultVo baseResultVo = sendRequestClient.uploadTrans(uploadTransRequestBo);
System.out.println(JSONUtil.toJsonStr(baseResultVo));
BaseResultVo documentInfo = sendRequestClient.getDocumentInfo(baseResultVo.getData().getFileNo());
System.out.println(JSONUtil.toJsonStr(documentInfo));
while (documentInfo.getData().getTransStatus() != 105){
documentInfo = sendRequestClient.getDocumentInfo(baseResultVo.getData().getFileNo());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(JSONUtil.toJsonStr(documentInfo));
}
DownloadRequestBo downloadRequestBo = new DownloadRequestBo();
downloadRequestBo.setFileNo(baseResultVo.getData().getFileNo());
downloadRequestBo.setType(1);
downloadRequestBo.setSaveFilePath("F:\\");
sendRequestClient.downloadFileOrBytes(downloadRequestBo);
sendRequestClient.downloadFile(downloadRequestBo);
System.out.println(System.currentTimeMillis());
}
}