I have a vietnamese dataset of 18k rows that I'm trying to translate to English using googletrans module.
from googletrans import Translator
translator = Translator()
def trans_text(df, text_field):
df[text_field] = df[text_field].apply(translator.translate, src='vi', dest='en').apply(getattr, args=('text',))
return df
trans_text(df_train.sample(1), "question")
I end up with the following JSONDecode error:
JSONDecodeError Traceback (most recent call last)
<ipython-input-21-d6791d78575e> in <module>()
24 df[text_field] = df[text_field].apply(translator.translate, src='vi', dest='en').apply(getattr, args=('text',))
25 return df
---> 26 trans_text(df_train.sample(1), "question")
27
28
<ipython-input-21-d6791d78575e> in trans_text(df, text_field)
22
23 def trans_text(df, text_field):
---> 24 df[text_field] = df[text_field].apply(translator.translate, src='vi', dest='en').apply(getattr, args=('text',))
25 return df
26 trans_text(df_train.sample(1), "question")
/opt/anaconda3/envs/sam-pycaret/lib/python3.6/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
4198 else:
4199 values = self.astype(object)._values
-> 4200 mapped = lib.map_infer(values, f, convert=convert_dtype)
4201
4202 if len(mapped) and isinstance(mapped[0], Series):
pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()
/opt/anaconda3/envs/sam-pycaret/lib/python3.6/site-packages/pandas/core/series.py in f(x)
4183
4184 def f(x):
-> 4185 return func(x, *args, **kwds)
4186
4187 else:
/opt/anaconda3/envs/sam-pycaret/lib/python3.6/site-packages/googletrans/client.py in translate(self, text, dest, src)
170
171 origin = text
--> 172 data = self._translate(text, dest, src)
173
174 # this code will be updated when the format is changed.
/opt/anaconda3/envs/sam-pycaret/lib/python3.6/site-packages/googletrans/client.py in _translate(self, text, dest, src)
79 r = self.session.get(url, params=params)
80
---> 81 data = utils.format_json(r.text)
82 return data
83
/opt/anaconda3/envs/sam-pycaret/lib/python3.6/site-packages/googletrans/utils.py in format_json(original)
60 converted = json.loads(original)
61 except ValueError:
---> 62 converted = legacy_format_json(original)
63
64 return converted
/opt/anaconda3/envs/sam-pycaret/lib/python3.6/site-packages/googletrans/utils.py in legacy_format_json(original)
52 text = text[:p] + states[j][1] + text[nxt:]
53
---> 54 converted = json.loads(text)
55 return converted
56
/opt/anaconda3/envs/sam-pycaret/lib/python3.6/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
352 parse_int is None and parse_float is None and
353 parse_constant is None and object_pairs_hook is None and not kw):
--> 354 return _default_decoder.decode(s)
355 if cls is None:
356 cls = JSONDecoder
/opt/anaconda3/envs/sam-pycaret/lib/python3.6/json/decoder.py in decode(self, s, _w)
337
338 """
--> 339 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
340 end = _w(s, end).end()
341 if end != len(s):
/opt/anaconda3/envs/sam-pycaret/lib/python3.6/json/decoder.py in raw_decode(self, s, idx)
355 obj, end = self.scan_once(s, idx)
356 except StopIteration as err:
--> 357 raise JSONDecodeError("Expecting value", s, err.value) from None
358 return obj, end
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I understand this arising due to a ban on my IP. I looked up ways to circumvent this and found that using VPN is worth the shot. I have HolaVPN installed already. However, being a newbie to all this, I'm not sure of how I could reproduce same steps as in the solution for hola instead. Any tips on a clear procedure to follow could greatly help. Thank you.
The library makes a request and without checking the status code assumes that the requests was successful:
79 r = self.session.get(url, params=params)
80
---> 81 data = utils.format_json(r.text)
Now, Google might not like your request for any reason it and return an error message with a matching status code (4xx, 5xx). The library still tries to parse the body as JSON which does not work since there is no body and raises a JSONDecodeError
that is unrelated to the original problem. You can't see the real cause.
Conclusion: the googletrans
library is missing crucial error handling. You may have to edit it and add error handling yourself.