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

332 views Asked by At

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'

1

There are 1 answers

0
Serge de Gosson de Varennes On

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.