µ±Ç°Î»Öà ²©ÎÄÊ×Ò³ > LYµÄ²©¿Í£º°áÔ˹ȸè·ÒëGoogletrans python api
ÔÎÄÍøÖ· :https://py-googletrans.readthedocs.io/en/latest/
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.
You can install it from?PyPI:
$ pip install googletrans
This is a great deal for everyone! (up to 2x times faster in my test) If you want to get googletrans faster you should install?hyper?package. Googletrans will automatically detect if hyper is installed and if so, it will be used for http networking.
If source language is not given, google translate attempts to detect the source language.
>>> 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>
You can use another google translate domain for translation. If multiple URLs are provided it then randomly chooses a domain.
>>> from googletrans import Translator >>> translator = Translator(service_urls=[ 'translate.google.com', 'translate.google.co.kr', ])
Array can be used to translate a batch of strings in a single method call and a single HTTP session. The exact same method shown above work for arrays as well.
>>> 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 -> ??? ?
The detect method, as its name implies, identifies the language used in a given sentence.
>>> translator.detect('? ??? ??? ??????.') # <Detected lang=ko confidence=0.27041003> >>> translator.detect('¤³¤ÎÎÄÕ¤ÏÈÕ±¾ÕZ¤Ç•ø¤«¤ì¤Þ¤·¤¿¡£') # <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>
class?googletrans.
Translator
(service_urls=None,?user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64)',?proxies=None,?timeout=None)
Google Translate ajax API implementation class
You have to create an instance of Translator to use this API
Parameters: |
|
---|
translate
(text,?dest='en',?src='auto')
Translate text from source language to destination language
Parameters: |
|
---|---|
Return type: | Translated |
Return type: |
|
Basic usage:
>>> 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>
Advanced 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 -> ??? ?
detect
(text)
Detect language of the input text
Parameters: | text?(UTF-8?str ;?unicode ; string sequence (list, tuple, iterator, generator)) ¨C The source text(s) whose language you want to identify. Batch detection is supported via sequence input. |
---|---|
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('¤³¤ÎÎÄÕ¤ÏÈÕ±¾ÕZ¤Ç•ø¤«¤ì¤Þ¤·¤¿¡£') <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>
Advanced usage:
>>> langs = translator.detect(['???', 'ÈÕ±¾ÕZ', 'English', 'le fran?ais']) >>> for lang in langs: ... print(lang.lang, lang.confidence) ko 1 ja 0.92929292 en 0.96954316 fr 0.043500196
class?googletrans.models.
Translated
(src,?dest,?origin,?text,?pronunciation,?extra_data=None)
Translate result object
Parameters: |
|
---|
class?googletrans.models.
Detected
(lang,?confidence)
Language detection result object
Parameters: |
|
---|
Hint
This is for internal use only to generate a valid token to access translate.google.com ajax API.
class?googletrans.gtoken.
TokenAcquirer
(tkk='0',?session=None,?host='translate.google.com')
Google Translate API token generator
translate.google.com uses a token to authorize the requests. If you are not Google, you do have this token and will have to pay for use. This class is the result of reverse engineering on the obfuscated and minified code used by Google to generate such token.
The token is based on a seed which is updated once per hour and on the text that will be translated. Both are combined - by some strange math - in order to generate a final token (e.g. 744915.856682) which is used by the API to validate the request.
This operation will cause an additional request to get an initial token from translate.google.com.
Example usage:
>>> from googletrans.gtoken import TokenAcquirer >>> acquirer = TokenAcquirer() >>> text = 'test' >>> tk = acquirer.do(text) >>> tk 950629.577246
Hint
iso639-1 language codes for?supported languages?for translation. Some language codes also include a country code, like zh-CN or zh-TW.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | LANGUAGES = { 'af': 'afrikaans', 'sq': 'albanian', 'am': 'amharic', 'ar': 'arabic', 'hy': 'armenian', 'az': 'azerbaijani', 'eu': 'basque', 'be': 'belarusian', 'bn': 'bengali', 'bs': 'bosnian', 'bg': 'bulgarian', 'ca': 'catalan', 'ceb': 'cebuano', 'ny': 'chichewa', 'zh-cn': 'chinese (simplified)', 'zh-tw': 'chinese (traditional)', 'co': 'corsican', 'hr': 'croatian', 'cs': 'czech', 'da': 'danish', 'nl': 'dutch', 'en': 'english', 'eo': 'esperanto', 'et': 'estonian', 'tl': 'filipino', 'fi': 'finnish', 'fr': 'french', 'fy': 'frisian', 'gl': 'galician', 'ka': 'georgian', 'de': 'german', 'el': 'greek', 'gu': 'gujarati', 'ht': 'haitian creole', 'ha': 'hausa', 'haw': 'hawaiian', 'iw': 'hebrew', 'hi': 'hindi', 'hmn': 'hmong', 'hu': 'hungarian', 'is': 'icelandic', 'ig': 'igbo', 'id': 'indonesian', 'ga': 'irish', 'it': 'italian', 'ja': 'japanese', 'jw': 'javanese', 'kn': 'kannada', 'kk': 'kazakh', 'km': 'khmer', 'ko': 'korean', 'ku': 'kurdish (kurmanji)', 'ky': 'kyrgyz', 'lo': 'lao', 'la': 'latin', 'lv': 'latvian', 'lt': 'lithuanian', 'lb': 'luxembourgish', 'mk': 'macedonian', 'mg': 'malagasy', 'ms': 'malay', 'ml': 'malayalam', 'mt': 'maltese', 'mi': 'maori', 'mr': 'marathi', 'mn': 'mongolian', 'my': 'myanmar (burmese)', 'ne': 'nepali', 'no': 'norwegian', 'ps': 'pashto', 'fa': 'persian', 'pl': 'polish', 'pt': 'portuguese', 'pa': 'punjabi', 'ro': 'romanian', 'ru': 'russian', 'sm': 'samoan', 'gd': 'scots gaelic', 'sr': 'serbian', 'st': 'sesotho', 'sn': 'shona', 'sd': 'sindhi', 'si': 'sinhala', 'sk': 'slovak', 'sl': 'slovenian', 'so': 'somali', 'es': 'spanish', 'su': 'sundanese', 'sw': 'swahili', 'sv': 'swedish', 'tg': 'tajik', 'ta': 'tamil', 'te': 'telugu', 'th': 'thai', 'tr': 'turkish', 'uk': 'ukrainian', 'ur': 'urdu', 'uz': 'uzbek', 'vi': 'vietnamese', 'cy': 'welsh', 'xh': 'xhosa', 'yi': 'yiddish', 'yo': 'yoruba', 'zu': 'zulu', 'fil': 'Filipino', 'he': 'Hebrew' } LANGCODES = dict(map(reversed, LANGUAGES.items())) |