Translator in Python from Visual Studio 2017 (Microsoft Translator Text API)

I tried to develop an application in Python that translates with the Microsoft Translator Text API.

environment

Windows 7 Professional SP1 64bit Visual Studio 2017 Community 15.2 (26430.6) Python 3.6.0 mstranslator 0.3.2

Azure account creation

Create an account from the Microsoft Azure site. You will need your mobile phone and credit card to create an account. https://azure.microsoft.com/ja-jp

This explanation is easy to understand. https://www.ipentec.com/document/document.aspx?page=microsoft-azure-sign-up

You can do a lot for free at first, but be aware that you will be charged if you change to a pay-as-you-go subscription with the free period ending and you still have paid services.

Even after the free period ends, if you just use the Microsoft Translator Text API, you can use up to 2 million characters a month for free.

Get Microsoft Translator Text API access key

Sign in to the Azure portal. https://portal.azure.com/

After signing in, select New from the menu on the left. aaa.png

Search for "Translator Text API" in the text box. bbb.png

If you find the Translator Text API, select it. ccc.png

The description of the Translator Text API is displayed on the right, so select Create. ddd.png

Create the Translator Text API. I entered the name and resource group appropriately, and chose F0 because the price is free. eee.png

When the Translator Text API is created, a summary will be displayed. Select "Show access key ...". fff.png

The access key will be displayed, so make a note of it. This time, only "Key 1" is used. ggg.png

Project creation

Create a "Python application" from a new project. hhh.png

Create a virtual environment for your project. iii.png

A dialog will be displayed. Select the Python version used in the project and create a virtual environment. jjj.png

mstranslator package installation (failed procedure)

From the project's Python environment, right-click "env (Python 3.6 (64bit))" and select "Install Python Package ...". kkk.png

If you enter "mstranslator" in the text box and select "Install mstranslator (0.3.2)", the package will not be installed ... lll.png

I get a UnicodeDecodeError and it doesn't install properly.

output


----- 'mstranslator==0.3.2'Is installed-----
Collecting mstranslator==0.3.2
  Using cached mstranslator-0.3.2.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\xxxxxx\AppData\Local\Temp\pip-build-tohptics\mstranslator\setup.py", line 12, in <module>
        long_description=open('README.rst').read() + '\n\n' +
    UnicodeDecodeError: 'cp932' codec can't decode byte 0xef in position 1138: illegal multibyte sequence
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in C:\Users\xxxxxx\AppData\Local\Temp\pip-build-tohptics\mstranslator\
----- 'mstranslator==0.3.2'Could not be installed-----

The point of introducing Python external modules on Windows http://qiita.com/yukinoi/items/1fe023408d3e684da983

As mentioned in this article, it seems that an error occurs with an external package that does not support the Windows character code problem.

mstranslator package installation (successful procedure)

The character code of the README.rst file of the mstranslator package is UTF-8, and the character code of Windows is CP932 (Shift JIS), so it seems that an error will occur.

In this case, you cannot install the package with the function of Visual Studio, so you need to download the package yourself and make the source code compatible with Windows before installing it.

Download "mstranslator-0.3.2.tar.gz" from here. https://pypi.python.org/pypi/mstranslator

This time, I downloaded it to the env folder under the project folder and unzipped it.

aaa.png

Modify setup.py in the unzipped mstranslator-0.3.2 folder as follows. The README.rst file will now be opened in UTF-8.

setup.py(Before correction)


    long_description=open('README.rst').read() + '\n\n' +

setup.py(Revised)


    long_description=open('README.rst', encoding='utf-8').read() + '\n\n' +

Open a command prompt from your project. bbb.png

Install the package with the following command from the command prompt.

command prompt


> cd env\mstranslator-0.3.2
> python setup.py install

The mstranslator package and its dependent packages are installed in the env virtual environment. ccc.png

Since input completion (intellisense) cannot be used as it is, add the unzipped mstranslator-0.3.2 folder to the search path. ddd.png

Once the folder is added to the search path, you will be able to use intellisense. eee.png

translate

Write the program to translate in Translator.py. Specify the acquired access key in ACCESS_KEY.

Translator.py


from mstranslator import Translator

ACCESS_KEY = 'xxxxxxxx'

translator = Translator(ACCESS_KEY)

#Translate from Japanese to English
print(translator.translate(text='Can you translate it properly?', lang_from='ja', lang_to='en'))

#Translate multiple sentences
print(translator.translate_array(['Apple', 'Mandarin orange', 'Grape'], lang_from='ja', lang_to='en'))

When translated properly, it is complete.

Recommended Posts

Translator in Python from Visual Studio 2017 (Microsoft Translator Text API)
Python development in Visual Studio 2017
Python development in Visual Studio
App development to tweet in Python from Visual Studio 2017
Extract text from images in Python
Run Python in C ++ on Visual Studio 2017
Run Python YOLOv3 in C ++ on Visual Studio 2017
Settings for Python coding in Visual Studio Code
Evernote API in Python
Text processing in Python
C API in Python 3
[Python] Get the text of the law from the e-GOV Law API
Japanese output when dealing with python in visual studio
AWS SDK for Python (Boto3) development in Visual Studio 2017
Hit Mastodon's API in Python
UTF8 text processing in python
Install numpy in Visual Studio 2019
OCR from PDF in Python
Bottle Pug in Visual studio 2019
Speech to speech in python [text to speech]
Blender Python API in Houdini (Python 3)
Use e-Stat API from Python
I tried to create API list.csv in Python from swagger.yaml
Get your heart rate from the fitbit API in Python!
How to debug the Python standard library in Visual Studio
Do something like a Python interpreter in Visual Studio Code
Predict gender from name using Gender API and Pykakasi in Python
Getting the arXiv API in Python
GOTO in Python with Sublime Text 3
Get data from Quandl in Python
Hit the Sesami API in Python
Bash, Python, Javascript, code command, etc. in Visual Studio Code on Mac
Create Gmail in Python without API
Sort large text files in Python
Use kabu StationĀ® API from Python
Hit the web API in Python
Use the Flickr API from Python
Remote debugging in Visual Studio (Linux)
Get upcoming weather from python weather api
Quickly implement REST API in Python
Run Ansible from Python using API
Reading and writing text in Python
Access the Twitter API in Python
Use Google Analytics API from Python
Handle SOAP API from Python (Zeep)
A memo for those who use Python in Visual Studio (me)
Extract strings from files in Python
How to hide the command prompt when running python in visual studio 2015
Reading from text files and SQLite in Python (+ Pandas), R, Julia (+ DataFrames)
From re-environment construction of Python to graph drawing (on visual studio code)
Detect Japanese characters from images using Google's Cloud Vision API in Python
Installation of Visual studio code and installation of python
Collecting information from Twitter with Python (Twitter API)
Mouse operation using Windows API in Python
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Python: Reading JSON data from web API
Revived from "no internet access" in Python
Prevent double boot from cron in Python
Tweet using the Twitter API in Python
Get Google Fit API data in Python