ssut / py-googletrans

(unofficial) Googletrans: Free and Unlimited Google translate API for Python. Translates totally free of charge.
http://py-googletrans.rtfd.io
MIT License
3.86k stars 717 forks source link

googletrans 4.0.0-rc1 fails with TypeError: 'NoneType' object is not iterable on client.py:222 #260

Closed MaxBrain closed 3 years ago

MaxBrain commented 3 years ago

Googletrans version:

I'm submitting a ...

Current behavior:

python3
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from googletrans import Translator
>>> translator = Translator()
>>> print(translator.translate("Author", src="en", dest="de").text)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\Users\Max\AppData\Local\Programs\Python\Python37-32\lib\site-packages\googletrans\client.py", line 222, in translate
    translated_parts = list(map(lambda part: TranslatedPart(part[0], part[1] if len(part) >= 2 else []), parsed[1][0][0][5]))
TypeError: 'NoneType' object is not iterable

Expected behavior:

I expect the "Author" word in deuch

Steps to reproduce:

ssut commented 3 years ago
Screen Shot 2020-12-13 at 5 25 21 AM

This probably is because of the gender-specific translate results. I will investigate and fix it soon.

noaho commented 3 years ago

I'm getting a similar issue, it seems whenever Google gives two results it fails.

[['wrb.fr', 'MkEWBc', '[[null,null,null,[[[0,[[[null,3]\n]\n,[true]\n]\n]\n,[2,[[[null,4]\n,[4,24]\n]\n,[false,true]\n]\n]\n]\n,24]\n]\n,[[[null,"\nPhraxāthity̒ kảlạng ca tkdin",null,null,null,[[null,[""]\n]\n,["\n"]\n,["พระอาทิตย์กำลังจะตกดิน",["พระอาทิตย์กำลังจะตกดิน","ดวงอาทิตย์เป็นเรื่องเกี่ยวกับชุด"]\n]\n]\n]\n]\n,"th",1,"en"]\n]\n', None, None, None, 'generic'], ['di', 63], ['af.httprm', 63, '-9129644094679872904', 37]] sequence item 0: expected str instance, NoneType found

ssut commented 3 years ago

@noaho Thanks for giving me the raw response. I'll fix it within a few days.

noaho commented 3 years ago

Thank you for the great project 😊

JosMan94 commented 3 years ago

Hello! I have the same problem too. @ssut thank you very much for continuing with the project :D Regards.

ahmedbr commented 3 years ago

Hi! any solution for this problem?

ssut commented 3 years ago

@ahmedbr I'm still working on it. Give me a little time please.

ahmedbr commented 3 years ago

@ahmedbr I'm still working on it. Give me a little time please.

Thank you so much for your efforts bro, I've just faced the problem and wanted to ask whether it's been solved. Thanks again. 😄

vbastianpc commented 3 years ago

I think I have the solution. Replace this line 222 of googletrans/client.py with this

try:
    translated_parts = list(map(lambda part: TranslatedPart(part[0], part[1] if len(part) >= 2 else []), parsed[1][0][0][5]))
except TypeError: # because of the gender-specific translate results
    translated_parts = [ TranslatedPart(parsed[1][0][1][0], [parsed[1][0][0][0], parsed[1][0][1][0]]) ]

Gender-specific translations now work.

>>> translator.translate("Author", src="en", dest="de").text
Autor

(This is my first comment on github, I don't know how to put this code snippet in your pull request)

ahmedbr commented 3 years ago

I think I have the solution. Replace this line 222 of googletrans/client.py with this

try:
    translated_parts = list(map(lambda part: TranslatedPart(part[0], part[1] if len(part) >= 2 else []), parsed[1][0][0][5]))
except TypeError: # because of the gender-specific translate results
    translated_parts = [ TranslatedPart(parsed[1][0][1][0], [parsed[1][0][0][0], parsed[1][0][1][0]]) ]

Gender-specific translations now work.

>>> translator.translate("Author", src="en", dest="de").text
Autor

(This is my first comment on github, I don't know how to put this code snippet in your pull request)

Thanks for your reply, but this doesn't work for me

Saravananslb commented 3 years ago

Use this for google translation and it is working fine https://pypi.org/project/pygoogletranslation

Thank me later

mr2rm commented 3 years ago

Hi, Still any solution to this issue? Can I work on this?

terryyz commented 3 years ago

@mr2rm You are more than welcome to work on this :)

kidburglar commented 3 years ago

@ssut @mr2rm Is there a reason it's not fixed yet? I ask because I tested the change of vbastianpc and it fixed the issue in my case.

@ahmedbr do you still have a way to reproduce your issue?

mr2rm commented 3 years ago

@kidburglar That's because I couldn't reproduce it. It seems gender-specific translation won't return in response anymore.

ssikyou commented 3 years ago

Same error here, it seems the issue still exsits.

kidburglar commented 3 years ago

@mr2rm

I will try to find few case where I can reproduct the issue. I noted that it was because the gender like you said but also if you got an URL the answer from google is not the same.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Luke8023 commented 2 years ago

incorrect from en to fr gives this error

MaxBrain commented 2 years ago

Actual code to reproduce

python3
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from googletrans import Translator
>>> translator = Translator()
>>> print(translator.translate("GREAT", src="en", dest="es").text)

The code from the first post still crashes

print(translator.translate("Author", src="en", dest="de").text)
dandiep commented 2 years ago

Still an issue. Any gendered word causes this. Another example:

translator.translate("white", src="en", dest="es")

lesleypotters commented 2 years ago

I still have this problem when running the code like this: df['text_en'] = df['text'].apply(lambda x: translator.translate(x, src=es, dest='en').text )

However, when I take out the src=es part, it does run - but it gets a timeout after 1,5h.

gonzalo-cordova-pou commented 2 years ago

translator.translate("modern" , src="en", dest ='es') returns an Error: TypeError: 'NoneType' object is not iterable It looks like it has to do with the gender-specific translate. As "modern" in spanish is "moderna" and "moderno" depending on the gender

MFRS commented 2 years ago

No one ever updated this thread, so I discovered this from stackoverflow, but this issue has already been solved. If you do "pip install googletrans==3.1.0a0" it will give you the first result of a gender-specific translate, no longer giving you this error.

Source: https://stackoverflow.com/questions/52455774/googletrans-stopped-working-with-error-nonetype-object-has-no-attribute-group

DanialPahlavan commented 2 years ago

No one ever updated this thread, so I discovered this from stackoverflow, but this issue has already been solved. If you do "pip install googletrans==3.1.0a0" it will give you the first result of a gender-specific translate, no longer giving you this error.

Source: https://stackoverflow.com/questions/52455774/googletrans-stopped-working-with-error-nonetype-object-has-no-attribute-group

The version googletrans==3.1.0a0 has this problem too .

rodfranck commented 1 year ago

Still an issue. Other examples:

translator.translate('ALPINE', src='en', dest='es') translator.translate('Armenian', src='en', dest='es')

edisonnery commented 1 year ago

I don`t know if someone explains here what is the source of this problem.

I mapped that it happens when the translation returns two or more options.

An example is the word "Automatic". When this word is translated for Portuguese without context can return two options, "Automático" or "Automática".

I think that you can create a parameter to define which genre the code needs to return male or female.

sachin-suresh-rapyuta commented 1 year ago

Was there any solution to this error, other than the try block suggested here?

ventz commented 1 year ago

@MFRS Thank you - worked perfectly!

vTuanpham commented 1 year ago

No one ever updated this thread, so I discovered this from stackoverflow, but this issue has already been solved. If you do "pip install googletrans==3.1.0a0" it will give you the first result of a gender-specific translate, no longer giving you this error.

Source: https://stackoverflow.com/questions/52455774/googletrans-stopped-working-with-error-nonetype-object-has-no-attribute-group

@MFRS This work for me, be sure to restart session if you are running on colab