调用有道云翻译接口 翻译类

package com.yangchong.fanyi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.ConnectException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Scanner;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class Fanyi {
// 创建json解析对象
private static JsonParser jp = new JsonParser();

public static void main(String[] args) throws IOException {
System.out.println("请输入要翻译的文本:");
Scanner sc = new Scanner(System.in);
String nr = sc.next();
fanyi(nr);
}

public static void fanyi(String nr) throws IOException {
File file = new File("F:\\" + nr + ".json");
if (file.exists()) {
return;
}

String abc = nr.matches("^[\u4e00-\u9fa5]{0,}$") ? URLEncoder.encode(nr, "utf-8") : nr;

// 调用有道接口的字符串
String jk = "http://fanyi.youdao.com/openapi.do?" + "keyfrom=yangchong&key=520150590&"
+ "type=data&doctype=json&version=1.1&q=" + abc;

// 创建RUL对象
URL url = new URL(jk);

// 打开连接
URLConnection urlConn = url.openConnection();

// 通过字节流获取网络数据
try {
InputStream is = urlConn.getInputStream();
FileOutputStream fos = new FileOutputStream("F:\\" + nr + ".json");
byte[] b = new byte[1024];
int i;
while ((i = is.read(b)) != -1) {
fos.write(b, 0, i);
}

// 关闭资源
fos.close();
is.close();
} catch (ConnectException e) {
System.err.println("访问超时!!");
}
}

public static String parserChinese(String nr) throws FileNotFoundException, UnsupportedEncodingException {
FileInputStream fis = new FileInputStream("F:\\" + nr + ".json");
InputStreamReader isr = new InputStreamReader(fis, "utf-8");// 使用字符流转码
// 获得要解析的数据
JsonObject jo = (JsonObject) jp.parse(isr);
StringBuffer sb = new StringBuffer();

// 单词
sb.append(jo.get("query").getAsString() + " ");

// 有道翻译
JsonArray ja = jo.get("translation").getAsJsonArray();
for (int i = 0; i < ja.size(); i++) {
sb.append(ja.get(i).getAsString());
}
sb.append("\n");

// 音标
JsonObject py = (JsonObject) jo.get("basic");
sb.append("拼音:" + py.get("phonetic").getAsString() + "\n");

// 翻译
JsonArray fy = py.get("explains").getAsJsonArray();
for (int i = 0; i < fy.size(); i++) {
sb.append(fy.get(i).getAsString() + "\n");
}

return sb.toString();
}

/**
* 英译中
*
* @param nr
* 翻译的内容
* @return
* @throws FileNotFoundException
* @throws UnsupportedEncodingException
*/
public static String parserEnglish(String nr) throws FileNotFoundException, UnsupportedEncodingException {

// 获取要解析的资源文件
// FileReader fr = new FileReader("F:\\"+nr+".json");
FileInputStream fis = new FileInputStream("F:\\" + nr + ".json");
InputStreamReader isr = new InputStreamReader(fis, "utf-8");// 使用字符流转码
// 获得要解析的数据
JsonObject jo = (JsonObject) jp.parse(isr);
StringBuffer sb = new StringBuffer();

// 单词
sb.append(jo.get("query").getAsString() + " ");

// 有道翻译
JsonArray ja = jo.get("translation").getAsJsonArray();
for (int i = 0; i < ja.size(); i++) {
sb.append(ja.get(i).getAsString());
}
sb.append("\n");

// 音标
JsonObject yb = (JsonObject) jo.get("basic");
String ybStr = "英[" + yb.get("uk-phonetic").getAsString() + "] " + "美[" + yb.get("us-phonetic").getAsString()
+ "]\n";
sb.append(ybStr);

// 翻译
JsonArray fy = yb.get("explains").getAsJsonArray();
for (int i = 0; i < fy.size(); i++) {
sb.append(fy.get(i).getAsString() + "\n");
}

return sb.toString();
}

}

转载于:https://www.cnblogs.com/yueguanguanyun/p/6625093.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值