Google Translate Python API

 

Image by Christina Morillo from pexels.com
Images by Christina Morillo from pexels.com

There are many translation services, and Google's is one of the most commonly used services. Let's try out its Python API.

The package is googletrans. The documentation can be found at py-googletrans.pdf.

Supported Languages

import googletrans

print(googletrans.LANGUAGES)
print(len(googletrans.LANGUAGES))

lists 107 supported languages.


Language Detection

from googletrans import Translator

translator = Translator()
english_text = "Tyson Fury got up twice from the canvas to win it."
dt_en = translator.detect(english_text)
print(dt_en)

gives us Detected(lang=en, confidence=1)

translator = Translator()

japanese_text = "タイソンフューリーはそれを獲得するためにキャンバスから2回起きました."
dt_ja = translator.detect(japanese_text)
print(dt_ja)

gives us Detected(lang=ja, confidence=1)

These are too easy for googletrans :-) Let's try more complicated things.

translator = Translator()
mixed_text = (
    "タイソンフューリーはそれを獲得するためにキャンバスから2回起きました. Tyson Fury got up twice from the canvas to win it."
)
dt = translator.detect(mixed_text)
print(dt)

We provide Japanese and English statements and we get Detected(lang=ja, confidence=1). The Japanese language is detected with a full confidence score!

Let's reduce the Japanese statement to a few words.

translator = Translator()
mixed_text = "きました. Tyson Fury got up twice from the canvas to win it."
dt = translator.detect(mixed_text)
print(dt)

and we get Detected(lang=en, confidence=0.7228199). This is more logical.


Language Translation

Next, we look at this translation service.

translator = Translator()
japanese_text = "タイソンフューリーはそれを獲得するためにキャンバスから2回起きました."
tlr_ja = translator.translate(japanese_text)
print(tlr_ja)

By default, the destination translated language is en. And, we get good results.

Translated(src=ja, dest=en, text=Tyson Fury woke up twice from the canvas to win it., pronunciation=None, extra_data="{'translat...")

Let's test English to Japanese translation.

translator = Translator()
english_text = "Tyson Fury got up twice from the canvas to win it."
tlr_en = translator.translate(english_text, dest="ja")
print(tlr_en)

Once again, the result is good.

Translated(src=en, dest=ja, text=タイソンフューリーはそれを獲得するためにキャンバスから2回起きました。, pronunciation=Taisonfu~yūrī wa sore o kakutoku suru tame ni kyanbasu kara 2-kai okimashita., extra_data="{'translat...")

Let's use the list of words for translation.

translator = Translator()
list_words = ["こんにちは", "すみません"]
tlr_ja = translator.translate(list_words)
for tlr in tlr_ja:
    print(tlr)

And, we get a list of good results.

Translated(src=ja, dest=en, text=Hello, pronunciation=None, extra_data="{'translat...")
Translated(src=ja, dest=en, text=excuse me, pronunciation=None, extra_data="{'translat...")

Conclusion

Using these API is almost effortless. And the results are good.

I was using googletrans version 3.0.0, and it is not good as I ran into a bug. Please avoid this version.

Traceback (most recent call last):
  File "main.py", line 9, in <module>
    tlr_en = translator.translate(english_text, dest="ja")
  File "/Users/veseah/experiments/googletrans/.venv/lib/python3.8/site-packages/googletrans/client.py", line 182, in translate
    data = self._translate(text, dest, src, kwargs)
  File "/Users/veseah/experiments/googletrans/.venv/lib/python3.8/site-packages/googletrans/client.py", line 78, in _translate
    token = self.token_acquirer.do(text)
  File "/Users/veseah/experiments/googletrans/.venv/lib/python3.8/site-packages/googletrans/gtoken.py", line 194, in do
    self._update()
  File "/Users/veseah/experiments/googletrans/.venv/lib/python3.8/site-packages/googletrans/gtoken.py", line 62, in _update
    code = self.RE_TKK.search(r.text).group(1).replace('var ', '')
AttributeError: 'NoneType' object has no attribute 'group'



Comments

Popular posts from this blog

OpenAI: Functions Feature in 2023-07-01-preview API version

Storing embedding in Azure Database for PostgreSQL

Happy New Year, 2024 from DALL-E