Search code examples
pythonpandaslambdanlp

How to fix classifer and lambda to TextBlob


any tips are welcome. I have some nlp model and i would like create some classifer. #test

from textblob.classifiers import NaiveBayesClassifier
cl = NaiveBayesClassifier(train)
cl.classify("This is an amazing library!")
'pos'

But when i add classificator to my lambda , isn't work .

df['sentiment_rat'] = df['Text'].apply(lambda tweet: TextBlob(tweet,classifier=cl).sentiment)

I get same result like after .sentiment not 'pos' or 'neg'

Text    Tweet_tokenized Tweet_nonstop   Tweet_stemmed   sentiment   sentiment_rat
0   0   RT @bennyjohnson: 🚨 BOMBSHELL🚨 \n\nVeteran &am... [rt, bennyjohnson, bombshell, veteran, amp, bu...   [rt, bennyjohnson, bombshell, veteran, amp, bu...   [rt, bennyjohnson, bombshel, veteran, amp, bus...   (0.0, 0.07142857142857142)  (0.0, 0.07142857142857142)

Solution

  • With a quick search in the doc and by executing it:

    blob.sentiment is not your classifier, it is the default TextBlob sentiment classifier.

    In order to use your classifier you should use TextBlob(tweet,classifier=cl).classify() not TextBlob(tweet,classifier=cl).sentiment