gummy.translators module

These classes translate English(from_lang) into Japanese(to_lang) using an external translation service (website).

Currently, Translation-Gummy supports the following services:

You can easily get (import) Translator Class by the following ways.

>>> from gummy import translators
>>> translator = translators.get("google")
>>> translator
<gummy.translators.GoogleTranslator at 0x129890160>
>>> from gummy.translators import GoogleTranslator
>>> google = GoogleTranslator()
>>> google
<gummy.translators.GoogleTranslator at 0x129890700>
>>> translator = translators.get(google)
>>> id(google) == id(translator)
True
class gummy.translators.GummyAbstTranslator(driver=None, maxsize=5000, interval=1, trials=30, verbose=False, use_cache=True, specialize=True, from_lang='en', to_lang='ja')[source]

Bases: object

property class_name

Same as self.__class__.__name__.

property name

Translator service name.

property driver_info

Return driver_info

Examples

>>> from gummy.utils import get_driver
>>> from gummy.translators import GoogleTranslator
>>> with get_driver() as driver:
...     translator = GoogleTranslator(driver=driver)
...     print(translator.driver_info)
{'session_id': '3e869e62f06c5f7d938831179ad3c50e', 'browserName': 'chrome'}
>>> translator = GoogleTranslator(driver=None)
>>> print(translator.driver_info)
{}
check_driver(driver=None)[source]

If the driver does not exist, use get_driver to get the driver.

Parameters

driver (WebDriver) – Selenium WebDriver.

Returns

Selenium WebDriver.

Return type

WebDriver

setup(specialize=True, from_lang='en', to_lang='ja')[source]

Setup an instance by defining a required translation methods.

Parameters
  • specialize (bool) – Whether to support multiple languages or specialize. (default= True) If you want to specialize in translating between specific languages, set from_lang and to_lang arguments.

  • from_lang (str) – Language before translation.

  • to_lang (str) – Language after translation.

abstract property supported_langs

Supported language codes.

abstract specialize2langs(from_lang, to_lang, **kwargs)[source]

Create the functions and variables needed to translate from from_lang to to_lang

Parameters

specialize (bool) – Whether to support multiple languages or specialize. (default= True) If you want to specialize in translating between specific languages, set from_lang and to_lang arguments.

Returns

Tuple of elements required in register_method

Return type

tuple (func, func, func, str)

Method

Description

find_translated_bulk

A function to find translated text from soup

find_translated_corr

A function to find translated text from soup , and driver

is_translated_properly

A function to check if the acquired translated_text is appropriate.

url_fmt

An url format ( "{query}" must be included.)

generate_lang_pairs()[source]

Generator to generate all translation pairs.

register_method(from_lang, to_lang, **kwargs)[source]

Register Methods which translate from_lang to to_lang

Parameters
  • from_lang (str) – Language before translation.

  • to_lang (str) – Language after translation.

  • kwargs (dict) – kwargs required for specialize2langs.

translate(query, driver=None, barname=None, from_lang='en', to_lang='ja', correspond=False)[source]

Translate Query string.

Parameters
  • query (str) – Query to be translated.

  • driver (WebDriver) – Selenium WebDriver.

  • barname (str) – Bar name for ProgressMonitor.

  • from_lang (str) – Language before translation.

  • to_lang (str) – Language after translation.

  • correspond (bool) – Whether to correspond the location of from_lang correspond to that of to_lang.

Returns

Translated string.

Return type

str

Examples

>>> from gummy import translators
>>> #=== Support multiple languages ===
>>> translator = translators.get("deepl", specialize=False, verbose=True)
>>> # Translate from english to Japanese (Success)
>>> ja = translator.translate(query="This is a pen.", from_lang="en", to_lang="ja")
DeepLTranslator (query1) 02/30[#-------------------]  6.67% - 2.173[s]   translated: これはペン
>>> print(ja)
これはペンです。
>>> # Translate from english to French (Success)
>>> fr = translator.translate(query="This is a pen.", from_lang="en", to_lang="fr")
DeepLTranslator (query1) 01/30[--------------------]  3.33% - 1.086[s]   translated: C'est
>>> print(fr)
C'est un stylo.
>>> #=== Specialize in specific languages ===
>>> translator = translators.get("deepl", specialize=True, from_lang="en", to_lang="ja", verbose=True)
>>> # Translate from english to Japanese (Success)
>>> ja = translator.translate(query="This is a pen.")
DeepLTranslator (query1) 02/30[#-------------------]  6.67% - 2.149[s]   translated: これはペン
>>> print(ja)
これはペンです。
>>> # Translate from english to French (Fail)
>>> fr = translator.translate(query="This is a pen.", from_lang="en", to_lang="fr")
DeepLTranslator (query1) 03/30 [##------------------] 10.00% - 3.220[s]
>>> print(fr)
これはペンです。
translate_wrapper(query, driver=None, barname=None, from_lang='en', to_lang='ja', correspond=True)[source]

Wrapper function for translate

Parameters
  • query (str) – Query to be translated.

  • driver (WebDriver) – Selenium WebDriver.

  • barname (str) – Bar name for ProgressMonitor.

  • from_lang (str) – Language before translation.

  • to_lang (str) – Language after translation.

  • correspond (bool) – Whether to correspond the location of from_lang correspond to that of to_lang.

Returns

SourceSentences ( list ) , TargetSentences ( list ) .

Return type

tuple

Examples

>>> from gummy import translators
>>> translator = translators.get("deepl", specialize=False, verbose=True)
>>> en, ja = translator.translate_wrapper(query="This is a pen.", from_lang="en", to_lang="ja", correspond=True)
>>> en
['This is a pen.']
>>> ja
['これはペンです。']
abstract static find_translated_bulk(soup)[source]

Find translated Translated text from soup

Parameters

soup (bs4.BeautifulSoup) – A data structure representing a parsed HTML or XML document.

Returns

Translated text.

Return type

str

find_translated_corr(driver)[source]

Find translated Translated text from soup

Parameters
  • soup (bs4.BeautifulSoup) – A data structure representing a parsed HTML or XML document.

  • driver (WebDriver) – Selenium WebDriver.

Returns

source_sentences ( list ) , target_sentences ( list )

Return type

tuple

is_translated_properly(translated_text)[source]

Check if the acquired translated_text is appropriate.

Parameters

translated_text (str) – Translated text.

Examples

>>> from gummy import translators
>>> translator = translators.get("google", specialize=True, from_lang="en", to_lang="ja")
>>> translator.is_translated_properly("")
False
>>> translator.is_translated_properly("日本語")
True
>>> translator.cache = "日本語"
>>> translator.is_translated_properly("日本語")
False
class gummy.translators.DeepLTranslator(driver=None, maxsize=5000, interval=1, trials=30, verbose=False, use_cache=True, specialize=True, from_lang='en', to_lang='ja')[source]

Bases: gummy.translators.GummyAbstTranslator

DeepL is a deep learning company that develops artificial intelligence systems for languages. See https://www.deepl.com/en/home for more info.

property supported_langs

Supported language codes.

static find_translated_bulk(soup)[source]

Find translated Translated text from soup

Parameters

soup (bs4.BeautifulSoup) – A data structure representing a parsed HTML or XML document.

Returns

Translated text.

Return type

str

static find_translated_corr(soup, driver)[source]

Find translated Translated text from soup

Parameters
  • soup (bs4.BeautifulSoup) – A data structure representing a parsed HTML or XML document.

  • driver (WebDriver) – Selenium WebDriver.

Returns

source_sentences ( list ) , target_sentences ( list )

Return type

tuple

specialize2langs(from_lang, to_lang, **kwargs)[source]

Create the functions and variables needed to translate from from_lang to to_lang

Parameters

specialize (bool) – Whether to support multiple languages or specialize. (default= True) If you want to specialize in translating between specific languages, set from_lang and to_lang arguments.

Returns

Tuple of elements required in register_method

Return type

tuple (func, func, func, str)

Method

Description

find_translated_bulk

A function to find translated text from soup

find_translated_corr

A function to find translated text from soup , and driver

is_translated_properly

A function to check if the acquired translated_text is appropriate.

url_fmt

An url format ( "{query}" must be included.)

is_translated_properly(translated_text)[source]

Deepl represents the character being processed as [...], so make sure it has not completed.

Examples

>>> from gummy import translators
>>> translator = translators.get("deepl")
>>> translator.is_translated_properly("日本語 [...]")
False
>>> translator.is_translated_properly("[...]日本語")
True
>>> translator.is_translated_properly("日本語[...]日本語")
True
class gummy.translators.GoogleTranslator(driver=None, maxsize=5000, interval=1, trials=30, verbose=False, use_cache=True, specialize=True, from_lang='en', to_lang='ja')[source]

Bases: gummy.translators.GummyAbstTranslator

Google Translate is a free multilingual neural machine translation service developed by Google, to translate text and websites from one language into another. See https://translate.google.com/ for more info.

property supported_langs

Supported language codes.

static find_translated_bulk(soup)[source]

Find translated Translated text from soup

Parameters

soup (bs4.BeautifulSoup) – A data structure representing a parsed HTML or XML document.

Returns

Translated text.

Return type

str

static find_translated_corr(soup, driver)[source]

Find translated Translated text from soup

Parameters
  • soup (bs4.BeautifulSoup) – A data structure representing a parsed HTML or XML document.

  • driver (WebDriver) – Selenium WebDriver.

Returns

source_sentences ( list ) , target_sentences ( list )

Return type

tuple

specialize2langs(from_lang, to_lang, **kwargs)[source]

Create the functions and variables needed to translate from from_lang to to_lang

Parameters

specialize (bool) – Whether to support multiple languages or specialize. (default= True) If you want to specialize in translating between specific languages, set from_lang and to_lang arguments.

Returns

Tuple of elements required in register_method

Return type

tuple (func, func, func, str)

Method

Description

find_translated_bulk

A function to find translated text from soup

find_translated_corr

A function to find translated text from soup , and driver

is_translated_properly

A function to check if the acquired translated_text is appropriate.

url_fmt

An url format ( "{query}" must be included.)