ó <¿CVc@@sdZddlmZddlZddlZddlZddlmZmZm Z ddl m Z m Z de fd„ƒYZd„ZdS( s” Translator module that uses the Google Translate API. Adapted from Terry Yin's google-translate-python. Language detection added by Steven Loria. i(tabsolute_importN(tPY2trequestt urlencode(tTranslatorErrort NotTranslatedt TranslatorcB@sreZdZdZidd6Zd dd d d„Zd d d„Zd„Zd„Z d „Z d d d d „Z RS( s A language translator and detector. Usage: :: >>> from textblob.translate import Translator >>> t = Translator() >>> t.translate('hello', from_lang='en', to_lang='fr') u'bonjour' >>> t.detect("hola") u'es' s)http://translate.google.com/translate_a/tsxMozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19s User-AgenttencC@s˜tr|jdƒ}nidd6dd6dd6|d6|d6|d 6}|j|jd |d |d |ƒ}|j|ƒrˆ|j|ƒStd ƒ‚dS(s7Translate the source text from one language to another.sutf-8tptclientsUTF-8tietoetslttlttextthostttype_tdatas4Translation API returned the input string unchanged.N(Rtencodet _get_json5turlt_translation_successfult_get_translation_from_json5R(tselftsourcet from_langtto_langRRRtjson5((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/textblob/textblob/translate.pyt translate#s$ cC@s’tr|jdƒ}nt|ƒdkr9tdƒ‚nidd6dd6dd6|d 6}|j|jd |d |d |ƒ}|j|ƒ}|S( s"Detect the source text's language.sutf-8is1Must provide a string with at least 3 characters.RR sUTF-8R R RRRR(RRtlenRRRt_get_language_from_json5(RRRRRRtlang((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/textblob/textblob/translate.pytdetect/s"$cC@s'tj|ƒ}d|kr#|dSdS(Ntsrc(tjsontloadstNone(Rtcontentt json_data((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/textblob/textblob/translate.pyR:s cC@sXd}tj|ƒ}d|krNdjg|dD]}|d^q2ƒ}nt|ƒS(Nut sentencestttrans(R"R#tjoint _unescape(RR%tresultR&ts((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/textblob/textblob/translate.pyR@s  -cC@setj|ƒ}t}d|kra|dd}d|krad|kra|d|dk}qan|S(s{Validate API returned expected schema, and that the translated text is different than the original string. R'itorigR)(R"R#tFalse(RR%R&R,tresponse((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/textblob/textblob/translate.pyRGs c C@sƒt|ƒjdƒ}tjd|d|jd|ƒ}|sB|r[|jd|d|ƒntj|ƒ}|jƒ}|jdƒS(Nsutf-8RtheadersRRttype( RRRtRequestR1t set_proxyturlopentreadtdecode( RRRRRt encoded_datatreqtrespR%((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/textblob/textblob/translate.pyRSs!  N( t__name__t __module__t__doc__RR1R$RR RRRR(((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/textblob/textblob/translate.pyRs      cC@s"d}d„}tj|||ƒS(s6Unescape unicode character codes within a string. s\\{1,2}u[0-9a-fA-F]{4}cS@stjdƒ|jƒƒdS(Ntunicode_escapei(tcodecst getdecodertgroup(tx((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/textblob/textblob/translate.pytas(tretsub(RtpatternR7((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/textblob/textblob/translate.pyR+]s (R=t __future__RR"RDR?ttextblob.compatRRRttextblob.exceptionsRRtobjectRR+(((sh/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/textblob/textblob/translate.pyts   M