All Products
Search
Document Center

Machine Translation:Python SDK

Last Updated:Jun 20, 2023

The GitHub address of the Machine Translation SDK is as follows:

https://github.com/aliyun/aliyun-openapi-python-sdk/tree/master/aliyun-python-sdk-alimt. You also need to download the Alibaba Cloud aliyun-python-sdk-core.

Installation guide

The Alibaba Cloud SDK for Python supports Python 2.6.x, 2.7.x, 3.x, and later. You can install the Alibaba Cloud SDK for Python by using pip or GitHub.

  • Install the SDK by using pip (recommended). Run the following command to install the SDK by using pip:

pip install alibabacloud_alimt20181012==1.1.0
Note

If you are using python3.x, change the pip install aliyun-python-sdk-core to pip install aliyun-python-sdk-core-v3.

  • Download the source code from GitHub

Run the following command to install the Python SDK by using GitHub:

git clone https://github.com/aliyun/aliyun-openapi-python-sdk.git
# Install the core library for Alibaba Cloud SDK.
cd aliyun-openapi-python-sdk/aliyun-python-sdk-core
python setup.py install
# Install the Alibaba Cloud alimt SDK.
cd ../aliyun-python-sdk-alimt
python setup.py install
   

Procedure

To get started with the Machine Translation Python SDK, perform the following steps:

Step 1 Create an Alibaba Cloud account

For more information, see Alibaba Cloud account registration process.

We recommend that you complete real-name verification at your earliest opportunity. Otherwise, you cannot use some Alibaba Cloud services.

Step 2: Obtain an Alibaba Cloud AccessKey pair

To use the Machine Translation Python SDK, you must apply for the AccessKey pair.

Log on to the KMS page. Select an AccessKey pair for AIRec SDK for Java. If no AccessKey pair is available, create an AccessKey pair and make sure that the pair is in the enabled state. Keys refer to Access Key ID and Access Key Secret.

The AccessKey pair will be used in the following steps and must be kept confidential.

Step 3 Start a Python program

The following sample code shows how to use Log Service SDK for Python. Use any text editor or Python IDE to run the following sample code to interact with the Machine Translation server and obtain the corresponding output.

Dedicated version call DEMO example

#coding:utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkalimt.request.v20181012 import TranslateRequest
import json

# Create an AcsClient instance.
client = AcsClient(
   "<your-access-key-id>", # The AccessKey ID of your Alibaba Cloud account.
   "<your-access-key-secret>", # AccessKey Secret
   "cn-hangzhou" # The ID of the region.
);
# Create an API request and set the request parameters.
request = TranslateRequest.TranslateRequest()
request.set_SourceLanguage("en") # source language
request.set_Scene("title") # Specify the scenario. Product title: title, product description: description, and product communication:
request.set_SourceText("hello") # original
request.set_FormatType("text")  
request.set_TargetLanguage("zh")  
request.set_method("POST")  
# Initiate the API request and obtain the response.
response = client.do_action_with_exception(request)
print(json.loads(response))

General Version Call DEMO Example

#coding:utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkalimt.request.v20181012 import TranslateGeneralRequest
import json
# Create an AcsClient instance.
client = AcsClient(
   "<your-access-key-id>",  #AccessKey ID
   "<your-access-key-secret>",# Alibaba Cloud account Access Key Secret
   "cn-hangzhou" # The ID of the region.
);
# Create an API request and set the request parameters.
request = TranslateGeneralRequest.TranslateGeneralRequest()
request.set_SourceLanguage("en")
request.set_SourceText("hello")
request.set_FormatType("text")
request.set_TargetLanguage("zh")
request.set_method("POST")
# Initiate the API request and obtain the response.
response = client.do_action_with_exception(request)
print(json.loads(response))