中文與西班牙文互相翻譯的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中文網其他相關文章!