zoukankan      html  css  js  c++  java
  • 英汉翻译功能的简单实现

    using System;
    using System.Net;
    using System.Xml;
    using System.IO;
    using ConsoleApplication1;
    static class TranslationSample
    {
        static void Main()
        {
            //AppId 就是 ApplicationId的缩写,这个是必须参数,需要用户自己创建,可能是微软想知道是谁在用他的东西。
            //需要在bing开发中心(http://cn.bing.com/developers)注册并创建AppID那个是英文网站,这里就用我的吧。
            string AppId = "305713700E4A0E87E1BD013C0E784E204E9163B6";
    
            //你要翻译的文字
            string query = "what the fuck is English";
    
            //翻译的原语言,这个不是乱写的,有规定,简体中文zh-cn 中文繁体zh-tw 英式英语en-gb 美式英语en-us
            string soureLanguage = "en-us";
            //翻译的目标语言
            string targetLanguage = "zh-cn";
    
            string requestString = string.Format("http://api.search.live.net/xml.aspx?AppId={0}&Query={1}&Sources=Translation&Translation.SourceLanguage={2}&Translation.TargetLanguage={3}",
                AppId, query, soureLanguage, targetLanguage);
            //其实以上这个URL返回的是一个XML文件,
            //<?xml version="1.0" encoding="utf-8" ?> 
            //  <?pageview_candidate ?> 
            //- <SearchResponse xmlns="http://schemas.microsoft.com/LiveSearch/2008/04/XML/element" Version="2.2">
            //- <Query>
            //  <SearchTerms>what the fuck is English</SearchTerms> 
            //  </Query>
            //- <tra:Translation xmlns:tra="http://schemas.microsoft.com/LiveSearch/2008/04/XML/translation">
            //- <tra:Results>
            //- <tra:TranslationResult>
            //  <tra:TranslatedTerm>他妈的是英语</tra:TranslatedTerm> 
            //  </tra:TranslationResult>
            //  </tra:Results>
            //  </tra:Translation>
            //  </SearchResponse>
    
            //如果换成http://api.search.live.net/json.aspx?这个路径,参数不变。他就会返回一个Json格式的文件。当然还可以返回SOAP格式.
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestString);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            XmlDocument document = new XmlDocument();
            document.Load(response.GetResponseStream());
    
            //读取结果,其实读取结果很复杂,具体要看官方示例。因为返回有时候会有几个列表,或者报错,等等。。这个只是示例,所以只有一句话。
            Console.WriteLine(document.InnerText);
            Console.Read();
        }
     
    }

    引用system.Speech。程序集,还可以让翻译的结果说出来;

    如果再配合钩子函数,完全可以做一个屏幕取词翻译工具,就像金山词霸那样。

  • 相关阅读:
    解析时间parse time
    php一些高级函数方法
    PHP定界符<<<EOF
    linux crontab(定时任务)
    svn提交按钮灰选
    centos官网下载地址
    centos7 yum安装LAMP
    saprfc
    RSA加解密工具类RSAUtils.java,实现公钥加密私钥解密和私钥解密公钥解密
    接入HTTPS协议的CAS登录配置
  • 原文地址:https://www.cnblogs.com/tianyiyi/p/3302722.html
Copyright © 2011-2022 走看看