简体   繁体   中英

AttributeError: 'NoneType' object has no attribute 'group' while using Google translator

I have pandas dataframe column in Polish language and want to translate into English Language but I got error. Code below:

from googletrans import Translator
translator = Translator()
df['text_en'] = df2['text_pl'].apply(translator.translate, src='pl', dest='en')

Error Below:

 63 
     64         # this will be the same as python code after stripping out a reserved word 'var'
---> 65         code = unicode(self.RE_TKK.search(r.text).group(1)).replace('var ', '')
     66         # unescape special ascii characters such like a \x3d(=)
     67         if PY3:  # pragma: no cover

AttributeError: 'NoneType' object has no attribute 'group'

I don't know if you've solved your issue but I experienced the same thing. I discovered the module google_trans_new . You should try it:

pip install google_trans_new

For the translation part:

from google_trans_new import google_translator  
  
translator = google_translator()  
translate_text = translator.translate('首先感谢我的父母他们对我的关爱',lang_tgt='en')  

which return:

'First of all thank my parents for their love'

For detection:

from google_trans_new import google_translator  
  
detector = google_translator()  
detect_result = detector.detect('首先感谢我的父母他们对我的关爱')

which gives

['zh-CN', 'chinese (simplified)']

In your case, it would maybe work this way:

detector = google_translator()  

def detect(x):
    try:
        detected_language = detector.detect(x)
    except:
        detected_language = None
    return detected_language

df['language detected'] = df2['text_pl'].apply(detect)

to detect the language (if your not sure all is in english or polish).

And in a similar way to translate. (See example above). I did this way:

Translationlist = df2['text_pl'].unique()

LANGT = []
for lang in Translationlist :
    try:
        translate_text = translator.translate(lang, lang_tgt='en') 
    except:
        translate_text = None
    LANGT.append(translate_text)

and then merged it with the original dataframe. (Reason of choice: 40 languages and some "weird" text I wasn't sure would be translatable), but you may not need to do it that way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM