I tried to develop an application in Python that translates with the Microsoft Translator Text API.
Windows 7 Professional SP1 64bit Visual Studio 2017 Community 15.2 (26430.6) Python 3.6.0 mstranslator 0.3.2
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.
Sign in to the Azure portal. https://portal.azure.com/
After signing in, select New from the menu on the left.
Search for "Translator Text API" in the text box.
If you find the Translator Text API, select it.
The description of the Translator Text API is displayed on the right, so select Create.
Create the Translator Text API. I entered the name and resource group appropriately, and chose F0 because the price is free.
When the Translator Text API is created, a summary will be displayed. Select "Show access key ...".
The access key will be displayed, so make a note of it. This time, only "Key 1" is used.
Create a "Python application" from a new project.
Create a virtual environment for your project.
A dialog will be displayed. Select the Python version used in the project and create a virtual environment.
From the project's Python environment, right-click "env (Python 3.6 (64bit))" and select "Install Python Package ...".
If you enter "mstranslator" in the text box and select "Install mstranslator (0.3.2)", the package will not be installed ...
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.
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.
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.
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.
Since input completion (intellisense) cannot be used as it is, add the unzipped mstranslator-0.3.2 folder to the search path.
Once the folder is added to the search path, you will be able to use intellisense.
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