中文与西班牙语互相翻译的Java百度翻译API实现
引言:
随着全球化的深入发展,不同语言之间的交流和翻译需求越来越多。本文将介绍如何使用Java开发一个简单的程序,利用百度翻译API实现中文与西班牙语之间的互相翻译。
在pom.xml文件中添加以下依赖项:
<dependencies> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.10</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20180813</version> </dependency> </dependencies>
import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import org.json.JSONObject; public class TranslationClient { private static final String API_URL = "https://fanyi-api.baidu.com/api/trans/vip/translate"; private static final String ACCESS_TOKEN = "YOUR_ACCESS_TOKEN"; private static final String FROM = "zh"; private static final String TO = "es"; public static void main(String[] args) { String text = "你好世界"; try { HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost(API_URL); httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded"); String body = String.format("q=%s&from=%s&to=%s&appid=%s&salt=%s&sign=%s", text, FROM, TO, APP_ID, salt, sign); httpPost.setEntity(new StringEntity(body)); HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); String responseJson = EntityUtils.toString(entity); JSONObject jsonObject = new JSONObject(responseJson); String translation = jsonObject.getJSONArray("trans_result").getJSONObject(0).getString("dst"); System.out.println("Translation: " + translation); } catch (Exception e) { e.printStackTrace(); } } }
请注意,上述代码中的YOUR_ACCESS_TOKEN应该被替换成你自己的Access Token。其中,API_URL是百度翻译API的请求地址;FROM和TO分别代表源语言和目标语言;text是待翻译文本。
import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import org.json.JSONObject; public class TranslationClient { private static final String API_URL = "https://fanyi-api.baidu.com/api/trans/vip/translate"; private static final String ACCESS_TOKEN = "YOUR_ACCESS_TOKEN"; private static final String FROM = "es"; private static final String TO = "zh"; public static void main(String[] args) { String text = "Hola mundo"; try { HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost(API_URL); httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded"); String body = String.format("q=%s&from=%s&to=%s&appid=%s&salt=%s&sign=%s", text, FROM, TO, APP_ID, salt, sign); httpPost.setEntity(new StringEntity(body)); HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); String responseJson = EntityUtils.toString(entity); JSONObject jsonObject = new JSONObject(responseJson); String translation = jsonObject.getJSONArray("trans_result").getJSONObject(0).getString("dst"); System.out.println("Translation: " + translation); } catch (Exception e) { e.printStackTrace(); } } }
以上是中文与西班牙语互相翻译的Java百度翻译API实现的详细内容。更多信息请关注PHP中文网其他相关文章!