Translate using googletrans in Python

Overview

I made a program to display Google Translate and the original English sentence side by side using googletrans, a package for using Google Translate in Python.

Introduction

I wanted to make a file that arranged English and Japan translated by Google. I wanted to try it easily, so I decided to make it in Python.

Investigation

When I searched, I found such a site. Python – I tried googletrans. | Developers.IO

Published on PyPI. googletrans · PyPI

Googletrans is a free and unlimited python library that implemented Google Translate API. This uses the Google Translate Ajax API to make calls to such methods as detect and translate.

For some reason, it's free. Is that okay? There seem to be some restrictions.

By the way, Google's Translation API is charged. Cloud Translation Docs | Google Cloud

environment

I was able to install it with the following command.

~$ pip3 install googletrans

Each version of the implementation environment is as follows.

~$ python3 --version
Python 3.5.3
~$ pip3 list | grep google
googletrans (2.4.0)

program

I made the following program. Input an English text file with command line arguments and output the source text and translation results to standard output.

translate.py


from googletrans import Translator
import sys

args= sys.argv
if len(args) < 2:
    print('Command should be like')
    print('python3 translate.py textfile.txt')
else:
    print('open '+args[1])
    f = open(args[1])
    lines = f.readlines()
    f.close()

    translator = Translator()
    for line in lines:
        translated = translator.translate(line, dest="ja");
        print(line) # English
        print(translated.text) # Japanese
        print()
    print('finished')

Run

You can input a file written in English with the following command and output a file written in English and Japanese.

python3 translate.py file_en.txt > file_jp.txt

The input file and its result are as follows.

file_en.txt


Hello,
World.

file_jp.txt


file_en.txt
Hello,

Hello,

World.

world.

finished

When I checked other sentences, the result of translation using the browser's Google Translate add-in was different from the result of translation using googletrans.

in conclusion

It was easy to make. Python, Google and googletrans are great. In the future, I would like to compare by doing similar things within the free frame of the original API. It seems to be free up to 500,000 characters / month. See the site below. Cloud Translation  |  Google Cloud Free translation API in 3 minutes with Google Apps Script --Qiita How to make Google Translate API for free --Qiita

Recommended Posts

Translate using googletrans in Python
Using Python mode in Processing
GUI programming in Python using Appjar
Precautions when using pit in Python
[Python3] Google translate google translate without using api
Try using LevelDB in Python (plyvel)
Using global variables in python functions
Let's see using input in python
Infinite product in Python (using functools)
Edit videos in Python using MoviePy
Handwriting recognition using KNN in Python
Try using Leap Motion in Python
Depth-first search using stack in Python
When using regular expressions in Python
GUI creation in python using tkinter 2
Inflating text data by retranslation using google translate in Python
Quadtree in Python --2
CURL in python
Mouse operation using Windows API in Python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Try using the Wunderlist API in Python
Get Suica balance in Python (using libpafe)
(Bad) practice of using this in Python
Slowly hash passwords using bcrypt in Python
Meta-analysis in Python
Try using the Kraken API in Python
Using venv in Windows + Docker environment [Python]
Unittest in python
[FX] Hit oanda-API in Python using Docker
Tweet using the Twitter API in Python
Discord in Python
[Python] [Windows] Serial communication in Python using DLL
Start using Python
I tried using Bayesian Optimization in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Get Youtube data in Python using Youtube Data API
Plink in Python
Constant in python
Scraping a website using JavaScript in Python
Develop slack bot in python using chat.postMessage
Lifegame in Python.
Sqlite in python
StepAIC in Python
Write python modules in fortran using f2py
Draw a tree in Python 3 using graphviz
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Disease classification in Random Forest using Python
Constant in python
nCr in Python.
format in python