2

I am getting the following error while translating a column from spanish to English:

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

My data frame looks like the following:

case_id      es                                             fr
1234         -                                              -
2345         Hola como estas? Encantada de conocerte        comment vas-tu aujourd'hui     

3456         Hola como estas? Encantada de conocerte        -
123321       -                                              comment vas-tu aujourd'hui

'-' is something that shows that there are no comments. My data frame has a blank strings as well apart from comments so I have replaced the blanks with a '-'

I am using the following code:

import googletrans
from googletrans import Translator
translator = Translator()
df['es_en'] = df['es'].apply(lambda x: translator.translate(x, src='es',dest='en').text)
df['fr_en'] = df['fr'].apply(lambda x: translator.translate(x, src='fr',dest='en').text)

What is wrong here? Why I am getting this error?

Django0602
  • 797
  • 7
  • 26

1 Answers1

1

It seems some data related problem, one idea is return NaN or what need if parsing failed:

def trans(x, s):
    try:
        return translator.translate(x, src=s, dest='en').text
    except:
        return np.nan

df['es_en'] = df['es'].apply(lambda x: trans(x, 'es'))
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • and if I have multiple columns with different languages? like I have another column for french comments, and then german comments so do I need to write a new function for all such columns? Just updated my data set with a french column as well – Django0602 Jun 16 '20 at 11:34
  • I will test and update you in a while! but thank you so much :) – Django0602 Jun 16 '20 at 12:00
  • I am getting the following error: TypeError: trans() missing 1 required positional argument: 's' – Django0602 Jun 16 '20 at 12:21
  • Still getting that error. I am using the right code as you mentioned. Not sure why this is coming up. are we missing something here? – Django0602 Jun 16 '20 at 12:24
  • Testing it now! it will take some time to give me the results as there are some comments that need to be translated! will keep you posted! – Django0602 Jun 16 '20 at 12:29
  • It's working fine! But it is taking too much time considering I only have 100 comments in total with an average character count of 200. Can we make it faster in some way? – Django0602 Jun 16 '20 at 12:48
  • @Django0602 - Unfortunately it is slow, because `google method` is slow, so not :( – jezrael Jun 16 '20 at 12:49
  • I have a bad news, the translation did not work. I got no translated results. What could have gone wrong ? – Django0602 Jun 16 '20 at 13:16
  • @Django0602 - I guess some data releated problem, because one string worjking nice :( – jezrael Jun 16 '20 at 13:17
  • I am still not able to see the translation. Could it be due to a fact that I am on a secured network and might need a VPN to pass that? – Django0602 Jun 16 '20 at 16:57
  • @Django0602 - Hard to know. Are data confidental? If not, is possible share it? – jezrael Jun 17 '20 at 05:23
  • The data is not confidential and how do I share it? I can share some sample rows. But I think the issue is around the web request made to the google. which my secured network is not allowing. Do you know any way if I can use a VPN code with the code that you have shared? – Django0602 Jun 17 '20 at 08:38
  • @Django0602 - Unfortunately not, I have no permission in my work. – jezrael Jun 17 '20 at 08:40
  • 1
    Got it! Thanks! I will see what else can be done! But you solution was easy and worked well.. – Django0602 Jun 17 '20 at 08:48