0

i´m experimenting some troubles with texblob... i got a dataframe from Tweeter API and i'm trying to get a Sentimental Analysis through textblob but i was unable to do it due tooo:

from time import sleep
from textblob.exceptions import NotTranslated 
from textblob.exceptions import TranslatorError


def get_polarity(text):
  analysis = TextBlob(text)
  if text != ' ':
    try:
        if analysis.detect_language() == 'es':
          result = analysis.translate(from_lang = 'es', to = 'en').sentiment.polarity
          time.sleep(10)
          return result

        elif analysis.detect_language() != 'es':
          return TextBlob(text)
    except TranslatorError or NotTranslated:
      return TextBlob(text)
    else:
      return TextBlob(text)



df['polarity'] = df.apply(get_polarity)

All the above returns All the above returns the next Traceback....


HTTPError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_5456/3601509909.py in <module>
----> 1 df['polarity'] = df['clean_text'].apply(get_polarity)

~\anaconda3\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwargs)
   4355         dtype: float64
   4356         """
-> 4357         return SeriesApply(self, func, convert_dtype, args, kwargs).apply()
   4358 
   4359     def _reduce(

~\anaconda3\lib\site-packages\pandas\core\apply.py in apply(self)
   1041             return self.apply_str()
   1042 
-> 1043         return self.apply_standard()
   1044 
   1045     def agg(self):

~\anaconda3\lib\site-packages\pandas\core\apply.py in apply_standard(self)
   1096                 # List[Union[Callable[..., Any], str]]]]]"; expected
   1097                 # "Callable[[Any], Any]"
-> 1098                 mapped = lib.map_infer(
   1099                     values,
   1100                     f,  # type: ignore[arg-type]

~\anaconda3\lib\site-packages\pandas\_libs\lib.pyx in pandas._libs.lib.map_infer()

~\AppData\Local\Temp/ipykernel_5456/2552980312.py in get_polarity(text)
      6   if text != ' ':
      7     try:
----> 8         if analysis.detect_language() == 'es':
      9           result = analysis.translate(from_lang = 'es', to = 'en').sentiment.polarity
     10           time.sleep(10)

~\anaconda3\lib\site-packages\textblob\blob.py in detect_language(self)
    595             DeprecationWarning
    596         )
--> 597         return self.translator.detect(self.raw)
    598 
    599     def correct(self):

~\anaconda3\lib\site-packages\textblob\translate.py in detect(self, source, host, type_)
     74             client="te",
     75         )
---> 76         response = self._request(url, host=host, type_=type_, data=data)
     77         result, language = json.loads(response)
     78         return language

~\anaconda3\lib\site-packages\textblob\translate.py in _request(self, url, host, type_, data)
     94         if host or type_:
     95             req.set_proxy(host=host, type=type_)
---> 96         resp = request.urlopen(req)
     97         content = resp.read()
     98         return content.decode('utf-8')

~\anaconda3\lib\urllib\request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    212     else:
    213         opener = _opener
--> 214     return opener.open(url, data, timeout)
    215 
    216 def install_opener(opener):

~\anaconda3\lib\urllib\request.py in open(self, fullurl, data, timeout)
    521         for processor in self.process_response.get(protocol, []):
    522             meth = getattr(processor, meth_name)
--> 523             response = meth(req, response)
    524 
    525         return response

~\anaconda3\lib\urllib\request.py in http_response(self, request, response)
    630         # request was successfully received, understood, and accepted.
    631         if not (200 <= code < 300):
--> 632             response = self.parent.error(
    633                 'http', request, response, code, msg, hdrs)
    634 

~\anaconda3\lib\urllib\request.py in error(self, proto, *args)
    559         if http_err:
    560             args = (dict, 'default', 'http_error_default') + orig_args
--> 561             return self._call_chain(*args)
    562 
    563 # XXX probably also want an abstract factory that knows when it makes

~\anaconda3\lib\urllib\request.py in _call_chain(self, chain, kind, meth_name, *args)
    492         for handler in handlers:
    493             func = getattr(handler, meth_name)
--> 494             result = func(*args)
    495             if result is not None:
    496                 return result

~\anaconda3\lib\urllib\request.py in http_error_default(self, req, fp, code, msg, hdrs)
    639 class HTTPDefaultErrorHandler(BaseHandler):
    640     def http_error_default(self, req, fp, code, msg, hdrs):
--> 641         raise HTTPError(req.full_url, code, msg, hdrs, fp)
    642 
    643 class HTTPRedirectHandler(BaseHandler):

HTTPError: HTTP Error 400: Bad Request

Can anyone helpe pleaseeee, i'm really stuck, i think that i could be a little bit silly but i'm a beginner here.

Thanks boys

1 Answer 1

2

I know it's late, but still, textblob's lang detection and translation are deprecated. Use Google Translator's official API instead.

Link: https://textblob.readthedocs.io/en/dev/changelog.html#id2

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.