AWS AI Services Programming Series - Part1 (Polly, Translate & Textract)

   Go back to the Task List

  « 3: Programming with Amazon Polly    5: Programming with Amazon Textract »

4: Programming with Amazon Translate

Amazon Translate is probably one of the simplest machine learning service to use. It is a neural machine translation service to translate text from one language to another. In this task, you will write code to convert an English language text into German language text.

  1. Goto the AWS Cloud9 console and click on the New File option under the File menu.

    Translate

  2. A new Untitled1 file gets created. Write the following code in the Untitled1 file.

    Polly

    import boto3
    
    translate_client = boto3.client('translate')
    
    result = translate_client.translate_text(Text="People love AWS Dojo workshops and exercises.", SourceLanguageCode="en", TargetLanguageCode="de")
    print('TranslatedText: ' + result.get('TranslatedText'))
    

    `

  3. In the code above, you first create client to translate. Then you call translate_text method passing the text to translate, source language code (en) and target language code (de) as the parameters. The source language is English (en) and the target language is German (de). The text is provided in the Text parameter. In the end, you print the TranslatedText from the response of the translate_text method call.

  4. Click on the Save option under the File menu. On the Save As popup, type in dojotranslate.py as the file name and click on the Save button. The code is saved in the file.

    Polly

  5. In the console window, run python dojotranslate.py command to run the dojotranslate.py code. The code execution finishes in no time. You can see the German translation of the text People love AWS Dojo workshops and exercises.

    Polly

  6. As I mentioned earlier, this is the simplest service you can use. Make changes to the code to try with other languages you know or like. The next task is working with Amazon Textract to extracts text and data from the scanned documents.