使用Microsoft Translator进行翻译和文本转语音

本文档详述了如何使用Microsoft Translator API在Java, PHP, Python和Linux环境中进行文本翻译和生成语音文件。首先,需要在Windows Azure Marketplace创建应用并订阅翻译服务。然后,初始化API设置,生成应用令牌,并实现curl请求来调用API。接着,通过Translate和Speak方法进行内容翻译和语音合成。最后,构建前端接口,允许用户输入文本并生成mp3语音文件。注意,Microsoft Translator的免费版对字符数量有限制,适用于较小规模的文本转换需求。" 125552677,280652,Excel 2010-2016:PowerQuery组件的启用与集成,"['大数据', 'excel', 'PowerBI', 'PowerQuery', '数据导入']
摘要由CSDN通过智能技术生成

Text to speech is a popular technique used by many websites to provide their content in an interactive way. The generation of artificial human voice is known as Speech Synthesis. Even though it's highly popular, there are very few speech synthesis services, especially when looking for those free of charge. Microsoft Translator is one of the services we can use to get a speech service with limited features. In this tutorial, we are going to look at how we can use Microsoft Translator API to translate content and then make audio files using said content.

文字转语音是许多网站用来以交互方式提供其内容的一种流行技术。 人工人声的生成称为语音合成。 即使它非常流行,也很少有语音合成服务,尤其是在寻找免费服务时。 Microsoft Translator是我们可以用来获得功能有限的语音服务的服务之一。 在本教程中,我们将研究如何使用Microsoft Translator API来翻译内容,然后使用所述内容制作音频文件。

You can download the entire source code of this project on Github.

您可以在Github上下载该项目的全部源代码。

创建Windows Azure Marketplace应用程序 (Creating Windows Azure Marketplace Application)

First, we have to create an application in Windows Azure Data Marketplace to subscribe to Microsoft Translator API. Let's get started on the application creation process.

首先,我们必须在Windows Azure数据市场中创建一个应用程序以订阅Microsoft Translator API。 让我们开始创建应用程序。

Prerequisites – Microsoft Email Account

先决条件– Microsoft电子邮件帐户

步骤1 –登录您的Azure帐户 (Step 1 – Sign into your Azure Account)

Use the email account and sign into Azure Marketplace.

使用电子邮件帐户并登录到Azure市场

步骤2 –注册应用程序 (Step 2 – Registering the application)

Now we have to create an application to use the translation and speech service. This is similar to the applications we create for popular social networking sites such as Facebook, LinkedIn or Twitter. Click the Register Application link to create a new application. You will get a screen similar to the following.

现在,我们必须创建一个使用翻译和语音服务的应用程序。 这类似于我们为流行的社交网站(如Facebook,LinkedIn或Twitter)创建的应用程序。 单击注册应用程序链接以创建一个新的应用程序。 您将获得类似于以下的屏幕。

alt

Fill all the details of the given application form. You can define your own Client ID to be used for the application. Client Secret will be automatically generated for you. Keep it unchanged and fill the remaining details as necessary.

填写给定申请表的所有详细信息。 您可以定义自己的客户端ID以用于该应用程序。 客户端机密将自动为您生成。 保持不变,并根据需要填写其余详细信息。

Click on the Create button and you will get a list of created applications as shown in the following screen.

单击创建按钮,您将获得已创建应用程序的列表,如下屏幕所示。

alt

Note down the Client ID and Client Secret to be used for the application.

记下要用于该应用程序的客户端ID客户端密钥

步骤3 –订阅翻译服务 (Step 3 – Subscribe to Translation service)

Next, we have to subscribe to Microsoft Translator to use the API. Navigate to Microft Translator and subscribe to one of the packages. First 2,000,000 characters are free and it's obvious that we have to use it for testing purposes.

接下来,我们必须订阅Microsoft Translator才能使用该API。 导航到Microft Translator并订阅其中一个软件包。 前2,000,000个字符是免费的,很明显,我们必须将其用于测试目的。

The following screenshot previews the subscription screen. Subscribe to the free package.

以下屏幕截图预览了订阅屏幕。 订阅免费软件包。

alt

Now we have completed the prerequisites for using the Microsoft Translator API. Let's get started on the development of the text to speech service.

现在,我们已经完成了使用Microsoft Translator API的先决条件。 让我们开始从文本到语音服务的开发。

初始化翻译API设置 (Initializing Translation API Settings)

Let's get started by initializing the necessary settings for using the Translator API. Create a file called translation_api_initializer.php with the following code.

让我们开始初始化使用Translator API的必要设置。 使用以下代码创建一个名为translation_api_initializer.php的文件。

<?php

        class TranslationApiInitializer {

            private $clientID;
            private $clientSecret;
            private $authUrl;
            private $grantType;
            private $scopeUrl;

            public function __construct() {
                $this->clientID = "Client ID";

                $this->clientSecret = "Client Secret";
                //OAuth Url.
                $this->authUrl = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/";
                //Application Scope Url
                $this->scopeUrl = "http://api.microsofttranslator.com";
                //Application grant type
                $this->grantType = "client_credentials";
            }
        }    
    ?>

Here, we have configured the necessary settings for using the translation service. You have to use the Client ID and Client Secret generated in the application registration process. Other parameters contain the authentication URL and grant type. We can keep it as is for every application, unless it's changed officially by Microsoft.

在这里,我们已经配置了使用翻译服务的必要设置。 您必须使用在应用程序注册过程中生成的客户端ID和客户端密钥。 其他参数包含身份验证URL和授予类型。 除非Microsoft对其进行了正式更改,否则我们可以将其保留在每个应用程序中。

生成应用程序令牌 (Generating Application Tokens)

The next task is to generate tokens to access the Translator service. Tokens have a limited life time and we have to generate them regularly. Let's take a look at the implementation of the token generation function.

下一个任务是生成令牌以访问翻译器服务。 令牌的使用寿命有限,我们必须定期生成令牌。 让我们看一下令牌生成功能的实现。

/*
     * 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();
        }
    }

Here, we are using a function called getTokens, which accepts all the settings as parameters. Inside the function, we make a curl request to the defined authentication URL by passing the remaining parameters. On successful execution, we can access the token using$objResponse->access_token.

在这里,我们使用一个名为getTokens的函数,该函数接受所有设置作为参数。 在函数内部,我们通过传递其余参数向定义的身份验证URL发出curl请求。 成功执行后,我们可以使用$objResponse->access_token访问令牌。

实施可重用的卷曲请求 (Implementing Reusable Curl Request)

Once an access token is retrieved, we can access the translation functions by authorizing the request with the access token. Generally, we use curl for making requests to APIs, so let's implement a reusable function for our curl request as shown in the following code.

检索访问令牌后,我们可以通过使用访问令牌授权请求来访问翻译功能。 通常,我们使用curl来向API发出请求,因此让我们为curl请求实现可重用的函数,如以下代码所示。

/*
     * 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, $postData=''){
        //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);
        if ($postData) {
            //Set HTTP POST Request.
            curl_setopt($ch, CURLOPT_POST, TRUE);
            //Set data to POST in HTTP "POST" Operation.
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        }
        //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;
    }

This function takes a request URL, request header and post data as the parameters and returns the curl response or error. Now all the necessary functions for accessing the translator service are ready.

此函数将请求URL,请求标头和发布数据作为参数,并返回curl响应或错误。 现在,已经准备好用于访问翻译器服务的所有必要功能。

翻译内容 (Translating Content)

Microsoft Translator API provides a wide range of methods for translation related functionality. In this tutorial, we will be using Translate and Speak methods. You can look at the complete set of API methods at http://msdn.microsoft.com/en-us/library/ff512419.aspx. Let's get started with the implementation of the translate function.

Microsoft Translator API提供了多种与翻译相关的功能的方法。 在本教程中,我们将使用TranslateSpeak方法。 您可以在以下位置查看完整的API方法集 http://msdn.microsoft.com/zh-CN/library/ff512419.aspx 。 让我们开始执行translate函数。

/*
     * Get the translated text
     * 
     * @param   string $text Text content for translation
     * @param   string $from_lang   Language of the text
     * @param   string $to_lang  Translation language
     * @return  array  Result set
     */

    public function textTranslate($text, $from_lang, $to_lang) {


        try {
            //Get the Access token.
            $accessToken = $this->getTokens($this->grantType, $this->scopeUrl, $this->clientID, $this->clientSecret, $this->authUrl);

            //Create the authorization Header string.
            $authHeader = "Authorization: Bearer " . $accessToken;

            //Input String.
            $inputStr = urlencode($text);

            $from = $from_lang;
            $to = $to_lang;

            //HTTP Detect Method URL.
            $detectMethodUrl = "http://api.microsofttranslator.com/V2/Http.svc/Translate?text=" . urlencode($inputStr) . 
            "&from=" . $from . "&to=" . $to."&contentType=text/plain";

            //Call the curlRequest.
            $strResponse = $this->curlRequest($detectMethodUrl, $authHeader);

            //Interprets a string of XML into an object.
            $xmlObj = simplexml_load_string($strResponse);
            foreach ((array) $xmlObj[0] as $val) {
                $translated_str = $val;
            }


            return array("status" => "success", "msg" => $translated_str);

        } catch (Exception $e) {
            return array("status" => "error", "msg" => $e->getMessage());
        }
    }

Translation requires source language, destination language and text to be translated. So we have used them as the parameters of the textTranslate function. Then we use the previously created getToken function to retrieve the token and assign it to the request header using Authorization: Bearer.

翻译需要翻译源语言,目标语言和文本。 因此,我们已将它们用作textTranslate函数的参数。 然后,我们使用先前创建的getToken函数检索令牌,并使用Authorization: Bearer将其分配给请求标头。

Then we set the source and destination languages using $from_lang and $to_lang variables. Also we have to encode the text content using PHP's urlencode function.

然后,使用$from_lang$to_lang变量设置源语言和目标语言。 另外,我们还必须使用PHP的urlencode函数对文本内容进行编码

Now it's time to start the translation process. Translator API provides a method called Translate and we can access it using the URL at http://api.microsofttranslator.com/V2/Http.svc/Translate

现在是时候开始翻译过程了。 Translator API提供了一种称为Translate的方法,我们可以使用以下网址访问它 http://api.microsofttranslator.com/V2/Http.svc/翻译

This method takes appId, text, to language and content type as mandatory parameters. Since we are using the authorization header, it's not a must to specify the appId. So we assign all the necessary parameters into the Translate API URL using $detectMethodUrl variable.

此方法将appId,文本,语言和内容类型作为必需参数。 由于我们使用的是授权标头,因此不必指定appId 。 因此,我们使用$detectMethodUrl变量将所有必需的参数分配给Translate API URL。

Finally, we initialize the curl request by passing translation API URL and the authorization header. On successful execution, we will get the translated data in XML format. So we use simplexml_load_string function to load the XML string and filter the translated text. Now we can translate text between any of the supported languages.

最后,我们通过传递翻译API URL和授权标头来初始化curl请求。 成功执行后,我们将获得XML格式的转换后数据。 因此,我们使用simplexml_load_string函数加载XML字符串并过滤翻译后的文本。 现在,我们可以在任何支持的语言之间翻译文本。

生成语音文件 (Generating Speech Files)

The final task of this tutorial is to generate speech in an mp3 file by using the translated text. We will be using a similar technique to do so. Create a function called textToSpeech with the following code.

本教程的最终任务是使用翻译后的文本在mp3文件中生成语音。 我们将使用类似的技术来做到这一点。 使用以下代码创建一个名为textToSpeech的函数。

/**
     * Returns a stream of a wave-file speaking the passed-in text in the desired language.
     * @param string $text text of language to break
     * @param string $to_lang language of the text
     * @return -
     */
    public function textToSpeech($text, $to_lang) {

        try {

            //Get the Access token.
            $accessToken = $this->getTokens($this->grantType, $this->scopeUrl, $this->clientID, $this->clientSecret, $this->authUrl);

            //Create the authorization Header string.
            $authHeader = "Authorization: Bearer " . $accessToken;

            //Set the params.
            $inputStr = urlencode($text);
            $language = $to_lang;

            $params = "text=$inputStr&language=$language&format=audio/mp3";

            //HTTP Speak method URL.
            $url = "http://api.microsofttranslator.com/V2/Http.svc/Speak?$params";
            //Set the Header Content Type.
            header('Content-Type: audio/mp3');
            header('Content-Disposition: attachment; filename=' . uniqid('SPC_') . '.mp3');

            //Call the curlRequest.
            $strResponse = $this->curlRequest($url, $authHeader);
            echo $strResponse;
        } catch (Exception $e) {
            echo "Exception: " . $e->getMessage() . PHP_EOL;
        }
    }

This function is similar in nature to the translate function. Here we use the Speak method URL athttp://api.microsofttranslator.com/V2/Http.svc/Speak instead of the translate URL. We have to use text, language and format as the necessary parameters. Since we are using the translated text for generating speech, language parameter should be equal to the $to_lang used in textTranslate function.

该功能本质上与翻译功能相似。 在这里,我们使用Speak方法的URL http://api.microsofttranslator.com/V2/Http.svc/而不是翻译URL。 我们必须使用文本,语言和格式作为必要的参数。 由于我们使用翻译后的文本来生成语音,因此language参数应等于textTranslate函数中使用的$to_lang

Then we have to use the necessary headers to automatically download the speech file. Here, we have used audio/mp3 as the content type and the uniqid function is used to generate a unique file name. Now we have both the translate and speech functions ready for use.

然后,我们必须使用必要的标头来自动下载语音文件。 在这里,我们使用audio/mp3作为内容类型,并且使用uniqid函数生成唯一的文件名。 现在,我们已经可以使用翻译和语音功能了。

实施前端接口 (Implementing Frontend Interface)

So far, we implemented translate and speech functions in the backend of the application. Now we have to build a simplified frontend interface to access the speech file generation service. Let's create a file called index.php with basic HTML form.

到目前为止,我们在应用程序的后端中实现了翻译和语音功能。 现在,我们必须构建一个简化的前端接口来访问语音文件生成服务。 让我们用基本HTML格式创建一个名为index.php的文件。

<?php
        $bing_language_codes = array('da' => 'Danish',
        'de' =>'German','en'=> 'English','fi'=> 'Finnish',
        'fr'=>'French','nl'=>'Dutch','ja'=> 'Japanese','pl'=> 'Polish',                    'es'=> 'Spanish','ru'=> 'Russian',);

    ?>
    <form action='' method='POST' >
    <table>
        <tr><td>Select From Language</td>
        <td><select name='from_lang' >
        <?php foreach($bing_language_codes as $code=>$lang){ ?>
                                                    <option value='<?php echo $code; ?>'><?php echo $lang; ?></option>
        <?php } ?>
            </select>
            </td>
        </tr>
        <tr><td>Select To Language</td>
        <td><select name='to_lang' >
        <?php foreach($bing_language_codes as $code=>$lang){ ?>
            <option value='<?php echo $code; ?>'><?php echo $lang; ?></option>
        <?php } ?>
        </select>
        </td>
        </tr>
        <tr><td>Text</td>
        <td><textarea cols='50' name='text' ></textarea>
        </td>
        </tr>
        <tr><td></td>
        <td><input type='submit' value='Submit' />
        </td>
        </tr>
    </table>
    </form>

This form allows users to select the preferred source and destination languages and type the content to be converted into speech. I have used a few of the supported languages in an array called $bing_language_codes. Now we can move to the form submission handling process to generate the speech file as shown below.

这种形式允许用户选择首选的源语言和目标语言,并键入要转换为语音的内容。 我在名为$bing_language_codes的数组中使用了一些受支持的语言。 现在,我们可以进入表单提交处理过程来生成语音文件,如下所示。

<?php

    include_once "translation_api_initializer.php";


    $bing_language_codes = array('da' => 'Danish',
    'de' => 'German','en'=> 'English','fi'=> 'Finnish',
    'fr'=> 'French', 'nl'=> 'Dutch','ja'=>'Japanese',
    'pl'=> 'Polish','es'=> 'Spanish','ru'=> 'Russian');

    if($_POST){

        $bing = new TranslationApiInitializer();

    $from_lang = isset($_POST['from_lang']) ? $_POST['from_lang'] : 'en';
    $to_lang = isset($_POST['to_lang']) ? $_POST['to_lang'] : 'fr';
    $text = isset($_POST['text']) ? $_POST['text'] : '';

    $result = $bing->textTranslate($text, $from_lang, $to_lang);
                                                          $bing->textToSpeech($result['msg'],$to_lang);

    }

    ?>

We include the TranslationApiInitializer file created earlier in the process and execute the translation and speech function respectively to generate the audio file. Once the code is completed, you will be able to generate audio files using this service.

我们包括在过程中较早创建的TranslationApiInitializer文件,并分别执行翻译和语音功能以生成音频文件。 代码完成后,您将可以使用此服务生成音频文件。

You can take a look at a live demo at http://www.innovativephp.com/demo/spc_text_to_speech

您可以在观看现场演示 http://www.innovativephp.com/demo/spc_text_to_speech

结语 (Wrap Up)

Throughout this tutorial we implemented translate and speech generation using Microsoft Translator API. Even though it's easy to use and free to some extent, there are also limitations. The speech service is only provided for a limited number of characters, roughly around 2000. So it's not possible use this service for a larger block of text. I recommend you use Acapela for large scale speech generation services.

在本教程中,我们使用Microsoft Translator API实现了翻译和语音生成。 即使它易于使用且在一定程度上免费,但也存在一些限制。 仅提供有限数量的字符(大约2000个左右)的语音服务。因此,不可能将此服务用于较大的文本块。 我建议您将Acapela用于大规模语音生成服务。

Hope you enjoyed the tutorial and looking forward to your comments and suggestions.

希望您喜欢本教程,并期待您的意见和建议。

翻译自: https://www.sitepoint.com/translation-text-speech-microsoft-translator/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值