Google translate python Googletrans library

googletrans is a free, call the Google Translate API python library interface. It means Google Translate Ajax API interface to achieve detection and text translation.

googletrans basic usage can refer to documents connected: http://py-googletrans.rtfd.io/

API features:

  • Fast and reliable - it uses the same server used translate.google.com
  • Automatic language detection
  • Batch translation
  • Customizable service URL
  • Connection pool (the advantages of using requests.Session)
  • HTTP / 2 support

Notes about the library:

  • The maximum character limit of a single text for 15k.
  • Because Google translated version of the page limitation, this API does not always work to ensure the library. (If you do not care about stability, please use this library.)
  • If you want to use a stable API, I strongly recommend that you use the official Google translation API.
  • If you receive an HTTP 5xx error or mistake # 6 and so on, may be because Google has banned your client IP address.

Quick start:

You can install it from PyPI:

$ pip install googletrans

HTTP / 2 support

Which are great for everyone! (In my tests faster 2 times) googletrans if you want to get faster, you should install the super package. Googletrans automatically detects whether the hyper installed, if the hyper installed, it is used http network.

Basic Usage

If you do not provide the source language, Google will attempt to detect the source language translation.

>>> from googletrans import Translator
>>> translator = Translator()
>>> translator.translate('안녕하세요.')
# <Translated src=ko dest=en text=Good evening. pronunciation=Good evening.>

>>> translator.translate('안녕하세요.', dest='ja')
# <Translated src=ko dest=ja text=こんにちは。 pronunciation=Kon'nichiwa.>

>>> translator.translate('veritas lux mea', src='la')
# <Translated src=la dest=en text=The truth is my light pronunciation=The truth is my light>

Custom Service URL

You can use other Google translate translate domain. If more than one URL, the random selection of a domain.

>>> from googletrans import Translator
>>> translator = Translator(service_urls=[
'translate.google.com',
'translate.google.co.kr',
])

Advanced usage (batch)

It is used to convert a string array in a single batch method calls and single HTTP session. Exactly the same manner shown above also applies to arrays. 

>>> translations = translator.translate(['The quick brown fox', 'jumps over', 'the lazy dog'], dest='ko')
>>> for translation in translations:
... print(translation.origin, ' -> ', translation.text)
# The quick brown fox -> 빠른 갈색 여우
# jumps over -> 이상 점프
# the lazy dog -> 게으른 개

Language Detection

As the name suggests, the detection method of identifying a language used in a given sentence. 

>>> translator.detect('이 문장은 한글로 쓰여졌습니다.')
# <Detected lang=ko confidence=0.27041003>
>>> translator.detect('この文章は日本語で書かれました。')
# <Detected lang=ja confidence=0.64889508>
>>> translator.detect('This sentence is written in English.')
# <Detected lang=en confidence=0.22348526>
>>> translator.detect('Tiu frazo estas skribita en Esperanto.')
# <Detected lang=eo confidence=0.10538048>

 

API navigation

googletrans.Translator
class googletrans.Translator(service_urls=None, user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64)', proxies=None, timeout=None)

      Google Translation ajax API implementation class

      You must create a Translator instance can use this API

parameter:

  • service_urls (a series of strings) - Google translation list of URLs. A random URL will be used. E.g. [ 'translate.google.com', 'translate.google.co.kr']
  • user_agent (str) - User-Agent header when a request to send.
  • proxies (dictionary) - proxy configuration. And dictionary mapping protocol or protocol such as the URL to the proxy host { 'http': 'foo.bar: 3128', 'http: //host.name': 'foo.bar: 4012'}
  • Timeout (digital or twice) - Request timeout defined libraries. It will be used for each request.

 

translate (text, dest = 'and' src = 'auto')

       Translate text from the source language to the target language

parameter:

  • text (UTF-8 str; unicode; string sequence (list, tuple, iterator, generator)) - the source text to be translated. By train input supports batch conversion.
  • dest - the source text into a language. The value should be the name of one of one of the languages ​​listed in the language code googletrans.LANGUAGES googletrans.LANGCODES or listed.
  • dest - str; unicode
  • src - the source language text. The value should be the name of one of one of the languages ​​listed in the language code googletrans.LANGUAGES googletrans.LANGCODES or listed. If the language is not specified, the system will automatically try to identify the source language.
  • src – str; unicode

Return type: Translated

Return Type: list (when a list is passed)

Basic usage:

>>> from googletrans import Translator
>>> translator = Translator()
>>> data=translator.translate('이 문장은 한글로 쓰여졌습니다.',dest='zh-cn')
>>> data.src
'ko'
>>> data.dest
'zh-cn'
>>> data.text
'这句话是用韩文写的。'
>>>

Higher-order Usage:

>>> translations = translator.translate(['The quick brown fox', 'jumps over', 'the lazy dog'], dest='ko')
>>> for translation in translations:
... print(translation.origin, ' -> ', translation.text)
The quick brown fox -> 빠른 갈색 여우
jumps over -> 이상 점프
the lazy dog -> 게으른 개

Return Type class googletrans.models.Translated ( the srcdestOrigintextPronunciationextra_data = None )

Attributes:

  • src – source langauge (default: auto)
  • dest – destination language (default: en)
  • origin – original text
  • text – translated text
  • pronunciation – pronunciation

 

detect(text)

     Enter the text language detection

parameter:

  • text (UTF-8 str; unicode; string sequence (list, tuple, iterator, generator)) - To identify its source language text. By train input supports batch testing.

Return Type: Detected
return type: list (when a list is passed )
Basic usage:

>>> from googletrans import Translator
>>> translator = Translator()
>>> translator.detect('이 문장은 한글로 쓰여졌습니다.')
<Detected lang=ko confidence=0.27041003>
>>> translator.detect('この文章は日本語で書かれました。')
<Detected lang=ja confidence=0.64889508>
>>> translator.detect('This sentence is written in English.')
<Detected lang=en confidence=0.22348526>
>>> translator.detect('Tiu frazo estas skribita en Esperanto.') <Detected lang=eo confidence=0.10538048>

Higher-order Usage: 

>>> langs = translator.detect(['한국어', '日本語', 'English', 'le français'])
>>> for lang in langs:
... print(lang.lang, lang.confidence)
ko 1
ja 0.92929292
en 0.96954316 fr 0.043500196

Return Type class googletrans.models.Detected ( langconfidence )

Attributes:

  • lang – detected language
  • confidence – the confidence of detection result (0.00 to 1.00)

Guess you like

Origin www.cnblogs.com/-wenli/p/12286779.html