[PYTHON] Machine translation by Amazon Translate

Last timeLast time tried Amazon Rekognition (image recognition service) This time, I tried a machine translation service called Amazon Translate.

Also, in Rekognition, I tried using the image file I want to recognize as an argument when executing the program, but in Translate, it is somewhat impossible to use the sentence I want to translate as an argument when executing the program, so this time Flask I created a simple UI using.

What is Amazon Translate?

Amazon Translate is a language translation service that uses deep learning models to provide more accurate and natural translations than traditional statistics-based and rule-based translation algorithms. With Amazon Translate, you can incorporate machine learning into your application without deep machine learning skills, and you can use machine learning from the API just by preparing the data.

Execution environment

OS:Ubuntu 16.04.2 Language: Python3.6.2


Advance preparation

Set the following authentication information in AWS CLI (aws configure).

AWS Access Key ID AWS Secret Access Key Default region name Default output format

Source code (translate.py)


from flask import *
import boto3

app = Flask(__name__)

def index():
    return render_template('translate.html')

def translate():

    #Get the translation source Japanese
    txt1 = request.form['txt1']

    if txt1 == '':
        return render_template('translate.html')

    #Create a Translate client
    translate = boto3.client('translate')

    # translate_Run text(Translated by: Japanese, Translated by: English)
    result = translate.translate_text(Text=txt1, SourceLanguageCode='ja', TargetLanguageCode='en')

    #Hand over the translation source Japanese and the translation destination English to html
    return render_template('translate.html',txt1=txt1,txt2=result['TranslatedText'])

if __name__ == "__main__":

Source code (translate.html)


<!DOCTYPE html>
        <meta charset="UTF-8">
Translate from Japanese to English.
        <form action="/translate" method="post">
            <!--If txt1 and txt2 are linked from python, display it in TextArea-->
            <textarea name="txt1" rows="10" cols="50">{% if txt1 %}{{txt1}}{% endif %}</textarea>
            <textarea name="txt2" rows="10" cols="50">{% if txt2 %}{{txt2}}{% endif %}</textarea>
            <input type="submit" value="Run">

Brief commentary

The outline is as follows.

(1) Obtain the Japanese translation source entered from the screen. (2) Execute Translate_text of Translate with the Japanese of (1) above as an argument. (Translation source: Japanese, translation destination: English.) ③ Hand over the Japanese translation source and the English translation destination to html. ④ Redisplay the above ③ on the screen.

Execution result


python translate.py



Reference (google translate)


I think the result is almost the same as google translate.


Translate, like Rekognition, is a convenient service that allows you to use machine learning from the API. This time, the translation source was fixed to Japanese and the translation destination was fixed to English, but of course, the language itself is also an argument of the API, and it seems that the supported languages exceed 50. Also, there seems to be Hotels.com as a user case.

Recommended Posts

Machine translation by Amazon Translate
Machine Sommelier by Keras-
Chainer, RNN and machine translation
4 [/] Four Arithmetic by Machine Learning
Same person detection by Amazon Rekognition
100 Language Processing Knock 2020 Chapter 10: Machine Translation (90-98)