Skip to main content

Back translation for Natural Language Processing (NLP) using Google Translate

Project description

BackTranslation

version Downloads license

BackTranslation is a python library that implemented to back translate the words among any two languages. This utilizes googletrans library and Baidu Translation API to translate the words.

Since there is an error in current verison of googletrans, you have to create only one instance to do back-translation for your work. Otherwise, it is easy to cause a bug from multi-requests. We will keep implementing this library with other translator libraries soon.

If you face any bug, you can open a issue in Github.

Installation

You can install it from PyPI:

$ pip install BackTranslation

Usage

Backtranslation with googletrans

Translate the original text to other language and translate back to augment the diversity of data in NLP research.

Parameters:

  • text: required. Original text that need to do back translation.
  • src: option. Source language code of original text. If this parameter is None, the method will detect the language of text automatically. (Default: None)
  • tmp: option. Middle language code. If this parameter is None, the method will pick one of two languages which is different from src.
  • sleeping: option. It is a timer to limite the speed of back-translation to avoid the limitation of Google. (Default: 0)

Return parameter: object Translated.

Attributes:

  • source_text: original sentence.
  • src: the language of original sentence
  • tmp: the target language as middle man
  • trans_text: intermediate result
  • back_text: back-tranlsated result
from BackTranslation import BackTranslation
trans = BackTranslation()
result = trans.translate('hello', src='en', tmp = 'zh-cn')
print(result.result_text)
# 'Hello there'

Note: You just need to create one instance of BackTranslation in order to avoid the issue in current version of googletrans.

Search the language code

You may find out your language code with full language name by using this method.

Parameters:

  • language: required. A language name in english.
from BackTranslation import BackTranslation
trans = BackTranslation()
trans.searchLanguage('Chinese')
# {'chinese (simplified)': 'zh-cn', 'chinese (traditional)': 'zh-tw'}

Backtranslation_Baidu with Baidu Translation API

To use this stable translation, you are required to register in Baidu Translation API for getting your own appID. It supports 2 million chacters per day for free. Note: Currently, they only support Chinese phone number to register the accout.

from BackTranslation import BackTranslation_Baidu
trans = BackTranslation_Baidu(appid='YOUR APPID', secretKey='YOUR SECRETKEY')
result = trans.translate('hello', src='auto', tmp='zh')
print(result.result_text)
# 'hello'
trans.closeHTTP()

Seach language code

Since Baidu provides the different language code, it will be updated soon.

Version Information

Version 0.2.1: fix the small bug. From this version, the library googletrans version is 4.0.0rc1.

Version 0.2.0: support back-translation with Baidu API, and fix bugs

Version 0.1.0: support back-translation with googletrans library

reference

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

BackTranslation-0.2.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

BackTranslation-0.2.1-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file BackTranslation-0.2.1.tar.gz.

File metadata

  • Download URL: BackTranslation-0.2.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.20.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.7.0

File hashes

Hashes for BackTranslation-0.2.1.tar.gz
Algorithm Hash digest
SHA256 6e4fd2884ecec30312402e085a9d5d05b017e3c93b3dd4d9d46983fd12cfa959
MD5 6e3bafdbb7a82f1385b2fbe4bb110d5d
BLAKE2b-256 4d95692a4ba4a8f3fe663055668f85cd87a31787c27da9643b731d4d34535964

See more details on using hashes here.

File details

Details for the file BackTranslation-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: BackTranslation-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.20.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.7.0

File hashes

Hashes for BackTranslation-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 448234f0a629cada7b548c4a64ceec859c34e3139dc76a9a2bce3b2b8bbe10b0
MD5 15b78d7ee33ce65527e949970804e7f3
BLAKE2b-256 d179ea391440239d414fbc5b40793d3597fdd22e709e036c1c1358505a68ef45

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page