利用有道翻译实现英汉互译

news/2024/7/19 10:51:30 标签: 爬虫, java

以下程序需要google jason jar的辅助,你可以从 http://pan.baidu.com/s/17qSuq 这里下载。

程序如下:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;


public class Main{
    public static void main(String[] args) throws Exception{
        System.out.println(translate("预防登革热。最近格林纳达登革热病例显示多个和病毒变异的趋势和增加的风险"));
System.out.println(translate("Prevention of dengue fever. Grenada dengue cases show multiple recently and the trend of virus variation and increase the risk")); }
public static String translate(String text) throws Exception{ final String Youdao_Url = "http://fanyi.youdao.com/openapi.do?keyfrom=sxt102400&key=1695079984&type=data&doctype=json&version=1.1&q="; StringBuilder YoudaoAPIURL = new StringBuilder(); YoudaoAPIURL.append(Youdao_Url).append(URLEncoder.encode(text, "UTF-8")); HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(YoudaoAPIURL.toString()).openConnection(); String rawData=stream2string(httpURLConnection.getInputStream()); JsonObject jsonObj = new JsonParser().parse(rawData).getAsJsonObject(); if ("0".equals(jsonObj.get("errorCode").toString())) { String finalData = jsonObj.get("translation").getAsString(); return finalData; }else{ return "Error!"; } } /** * Transform stream to String * @param is * @return */ private static String stream2string(InputStream is) { try { BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringWriter writer = new StringWriter(); char[] buffer = new char[10240]; int n; while ((n = reader.read(buffer)) != -1) { writer.write(buffer, 0, n); } reader.close(); return writer.toString(); } catch (Exception e) { throw new RuntimeException(e); } } }

控制台输出如下:

Prevention of dengue fever. Grenada dengue cases show multiple recently and the trend of virus variation and increase the risk
预防登革热。最近格林纳达登革热病例显示多个和病毒变异的趋势,增加风险

 有道的翻译质量还是不错的。


http://www.niftyadmin.cn/n/981518.html

相关文章

POJ1753 Flip Game(bfs、枚举)

链接:http://poj.org/problem?id1753 Flip Game Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying e…

Django模版语言自定义标签-实现前端 关联组合过滤查询

前端关联 组合过滤查询 实现效果如图: models.py 创建表代码from django.db import models# Create your models here.class Level(models.Model):name models.CharField(max_length32)def __str__(self):return self.name# 方向 class Direction(models.Model):na…

SQL Server数据库快照的工作方式

SQL Server数据库快照的工作方式 翻译自:How Database Snapshots Work 最近有一个帖子《errorlog中的异常信息rolled forward 和rolled back》 里面说到: 每周六凌晨1点会出现以下信息,服务器及数据库未出现重启,节点未切换&#…

Java的概述以及语法

Java的语法分为标示符和数据类型 Java的概述: 一些手打的: long l 12345; //隐式转换 int a (int)121234567L; //强制转换 float f 123.45;(提示报错) 默认为int类型,所以无法转换 下面三种为…

磁盘与文件系统管理详解

文章目录磁盘基础硬盘的结构存储容量磁盘接口类型MBR与磁盘分区表示磁盘分区结构文件系统类型xfs文件系统检测并确认新硬盘创建文件系统挂载、卸载文件系统实验partde分区 【超过2T分区】磁盘基础 硬盘是计算机常用的存储设备之一,本节将介绍硬盘的基本知识 硬盘的…

XtremSW缓存技术简介

作者将本文同时发布到:EMC中文支持论坛https://community.emc.com/docs/DOC-26104介绍在EMC闪存发展路线图中,服务器本地存储XtremSF硬件、服务器闪存缓存XtremSW Cache软件、全闪阵列XtremIO和混合阵列四个方面的产品和方案扮演着重要角色,共…

django创建应用程序_Django第1部分:构建投资组合应用程序入门

django创建应用程序Django is a fully featured Python web framework that can be used to build complex web applications. In this tutorial, you’ll jump in and learn Django by example. You’ll follow the steps to create a fully functioning web application and,…

17年计划

1、读完一本restful专业书籍。 2、登五岳或黄山之一。 3、办一张游泳卡,学会游泳。 4、读一本长篇小说。 5、学习一首完整的歌曲。 6、经常和好友聚餐见面。 7、经常写技术博客。写一片超过5千字的新浪博客。 8、试着用心养一颗植物。 9、试着约一下自己暗恋已久的女…