微信公众平台消息接口开发(13)多语种互译

意译API是一个英语到英语的机器翻译系统,rephrases英语句子英语。该API使用微软翻译API使用翻译相同的架构。的释义API的目的是不失其意义或想法,改写或重新表述的内容。意译API使用使用GET通过HTTP的REST服务。它使用简单的XML和JSON格式。

端点

的释义API的端点可以是下列其中一项:

  • http://api.microsofttranslator.com/v3/json/paraphrase
  • http://api.microsofttranslator.com/v3/rest/paraphrase
参数

Parameter Description
appId Required. A string containing the access token or application ID.
language Required. A string representing the language code. This parameter supports only ENGLISH with en as the language name.
Category Optional. A string containing the category or domain of the translation. This parameter supports only the default option general.
sentence Required. A sentence that you want to paraphrase. This parameter supports only one sentence as the input. Multiple sentences will fail, and an error code with the message will be displayed.
maxTranslations Optional. The number of paraphrased sentences for the sentence that you specified in the sentence parameter. The API will return as many paraphrased sentences as the number that you specify in the maxTranslations parameter.

Return value

The return value provides as many paraphrased sentences as the number specified in the maxTranslations request parameter.

JSON
ParaphraseResponse
{
     int errorCode;  // A positive number representing an error condition
     string errorMessage; // A descriptive error message 
     string[] paraphrases; // all paraphrases found
}

Error Messages

The following are the most common error messages.

  • ErrorNoParaphrasesFound – “The system did not find any paraphrase for the input sentence.”
  • ErrorInvalidAppid – “The AppId X is invalid.”
  • ErrorInvalidNumberOfSentences – “Only one sentence is supported. The request contains X sentences.”
  • ErrorInvalidCategory – “Category X is invalid.”
  • ErrorInvalidLanguage – “Language X is not supported.”

Example

PHP

has some issues !!!

复制代码
<?php

class AccessTokenAuthentication {
    /*
     * Get the access token.
     *
     * @param string $grantType    Grant type.
     * @param string $scopeUrl     Application Scope URL.
     * @param string $clientID     Application client ID.
     * @param string $clientSecret Application client ID.
     * @param string $authUrl      Oauth Url.
     *
     * @return string.
     */
    function getTokens($grantType, $scopeUrl, $clientID, $clientSecret, $authUrl){
        try {
            //Initialize the Curl Session.
            $ch = curl_init();
            //Create the request Array.
            $paramArr = array (
                 'grant_type'    => $grantType,
                 'scope'         => $scopeUrl,
                 'client_id'     => $clientID,
                 'client_secret' => $clientSecret
            );
            //Create an Http Query.//
            $paramArr = http_build_query($paramArr);
            //Set the Curl URL.
            curl_setopt($ch, CURLOPT_URL, $authUrl);
            //Set HTTP POST Request.
            curl_setopt($ch, CURLOPT_POST, TRUE);
            //Set data to POST in HTTP "POST" Operation.
            curl_setopt($ch, CURLOPT_POSTFIELDS, $paramArr);
            //CURLOPT_RETURNTRANSFER- TRUE to return the transfer as a string of the return value of curl_exec().
            curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
            //CURLOPT_SSL_VERIFYPEER- Set FALSE to stop cURL from verifying the peer's certificate.
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            //Execute the  cURL session.
            $strResponse = curl_exec($ch);
            //Get the Error Code returned by Curl.
            $curlErrno = curl_errno($ch);
            if($curlErrno){
                $curlError = curl_error($ch);
                throw new Exception($curlError);
            }
            //Close the Curl Session.
            curl_close($ch);
            //Decode the returned JSON string.
            $objResponse = json_decode($strResponse);
            if ($objResponse->error){
                throw new Exception($objResponse->error_description);
            }
            return $objResponse->access_token;
        } catch (Exception $e) {
            echo "Exception-".$e->getMessage();
        }
    }
}

/*
 * Class:HTTPTranslator
 *
 * Processing the translator request.
 */
Class HTTPTranslator {
    /*
     * Create and execute the HTTP CURL request.
     *
     * @param string $url        HTTP Url.
     * @param string $authHeader Authorization Header string.
     *
     * @return string.
     *
     */
    function curlRequest($url, $authHeader){
        //Initialize the Curl Session.
        $ch = curl_init();
        //Set the Curl url.
        curl_setopt ($ch, CURLOPT_URL, $url);
        //Set the HTTP HEADER Fields.
        curl_setopt ($ch, CURLOPT_HTTPHEADER, array($authHeader));
        //CURLOPT_RETURNTRANSFER- TRUE to return the transfer as a string of the return value of curl_exec().
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
        //CURLOPT_SSL_VERIFYPEER- Set FALSE to stop cURL from verifying the peer's certificate.
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, False);
        //Execute the  cURL session.
        $curlResponse = curl_exec($ch);
        //Get the Error Code returned by Curl.
        $curlErrno = curl_errno($ch);
        if ($curlErrno) {
            $curlError = curl_error($ch);
            throw new Exception($curlError);
        }
        //Close a cURL session.
        curl_close($ch);
        return $curlResponse;
    }
}

try {
    //Client ID of the application.
    $clientID       = "clientId";
    //Client Secret key of the application.
    $clientSecret = "ClientSecret";
    //OAuth Url.
    $authUrl      = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/";
    //Application Scope Url
    $scopeUrl     = "http://api.microsofttranslator.com";
    //Application grant type
    $grantType    = "client_credentials";

    //Create the AccessTokenAuthentication object.
    $authObj      = new AccessTokenAuthentication();
    //Get the Access token.
    $accessToken  = $authObj->getTokens($grantType, $scopeUrl, $clientID, $clientSecret, $authUrl);
    //Create the authorization Header string.
    $authHeader = "Authorization: Bearer ". $accessToken;

    //Set the params.
    $sentence      = "rephrasing is a hard problem for the computer.";
    $language      = "en-us";
    $category        = "general";
    $maxParaphrase = '6';
    
    $params = "sentence=".urlencode($sentence)."&language=$language&category=$category&maxParaphrases=$maxParaphrase";
    
    //HTTP paraphrase URL.
    $paraphraseUrl = "http://api.microsofttranslator.com/v3/json/paraphrase?$params";
    
    //Create the Translator Object.
    $translatorObj = new HTTPTranslator();
    
    //Call the curlRequest.
    echo $curlResponse = $translatorObj->curlRequest($paraphraseUrl, $authHeader);
    
} catch (Exception $e) {
    echo "Exception: " . $e->getMessage() . PHP_EOL;
}

/*
 * Create and execute the HTTP CURL request.
 *
 * @param string $url        HTTP Url.
 * @param string $authHeader Authorization Header string.
 * @param string $postData   Data to post.
 *
 * @return string.
 *
 */
function curlRequest($url, $authHeader) {
    //Initialize the Curl Session.
    $ch = curl_init();
    //Set the Curl url.
    curl_setopt ($ch, CURLOPT_URL, $url);
    //Set the HTTP HEADER Fields.
    curl_setopt ($ch, CURLOPT_HTTPHEADER, array($authHeader,"Content-Type: text/xml"));
    //CURLOPT_RETURNTRANSFER- TRUE to return the transfer as a string of the return value of curl_exec().
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
    //CURLOPT_SSL_VERIFYPEER- Set FALSE to stop cURL from verifying the peer's certificate.
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, False);
    //Execute the  cURL session.
    $curlResponse = curl_exec($ch);
    //Get the Error Code returned by Curl.
    $curlErrno = curl_errno($ch);
    if ($curlErrno) {
        $curlError = curl_error($ch);
        throw new Exception($curlError);
    }
    //Close a cURL session.
    curl_close($ch);
    return $curlResponse;
}
复制代码

 

 

 

 

Translator Language Codes

Microsoft Translator continually adds to the list of supported languages for the Translation and Text to Speech methods.

You can always obtain the current list of available language codes using the GetLanguagesForTranslate() or GetLanguagesForSpeak() methods.

These methods will return a language code. You can translate that language code into a friendly name in any of the supported languages using the GetLanguageNames() method. Below are the friendly names in English - you can retrieve them in any of the listed languages using GetLanguageNames().

 

 

Here is the list (as of January 2012):

 

Language Code

English Name

ar Arabic
bg Bulgarian
ca Catalan
zh-CHS Chinese (Simplified)
zh-CHT Chinese (Traditional)
cs Czech
da Danish
nl Dutch
en English
et Estonian
fa Persian (Farsi)
fi Finnish
fr French
de German
el Greek
ht Haitian Creole
he Hebrew
hi Hindi
hu Hungarian
id Indonesian
it Italian
ja Japanese
ko Korean
lv Latvian
lt Lithuanian
ms Malay
mww Hmong Daw
no Norwegian
pl Polish
pt Portuguese
ro Romanian
ru Russian
sk Slovak
sl Slovenian
es Spanish
sv Swedish
th Thai
tr Turkish
uk Ukrainian
ur Urdu
vi Vietnamese

 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值