Search code examples
pythonsentiment-analysisgeneric-lambda

Sentiment analysis with Lambda Expressions in Python


I'm trying to use TextBlob to perform sentiment analysis in Power BI. I'd like to use a lamdba expression because it seems to be substantially faster than running an iterative loop in Power BI.

For example, using Text Blob:

dataset['Polarity Score'] =dataset['Comment'].apply(lambda x: TextBlob(str(x).lower()).sentiment.polarity) 

creates a Power BI data column named "Polarity Score" that has the numerical values from TextBlob.

I would like to do similar withe the TextBlob.classify() function. However, I don't know how to pass it the second argument of the classifier.

Tutorials show to create and use a classifier:

from textblob.classifiers import NaiveBayesClassifier
from textblob import TextBlob

cl = NaiveBayesClassifier(train)
blob = TextBlob("The beer is good. But the hangover is horrible.", classifier=cl)
blob.classify()

I've tried

dataset['Polarity Class'] =dataset['Comment'].apply(lambda x: TextBlob(str(x).lower()).classify(), classifier=cl)

and

dataset['Polarity Class'] =dataset['Comment'].apply(lambda x,y: TextBlob(str(x).lower()).classify(), y=cl)

Neither work and point to the way I'm passing the classifier. How would I pass the classifier parameter in a lambda expression?


Solution

  • Simply

    cl = NaiveBayesClassifier(train)
    dataset['Polarity Class'] = dataset['Comment'].apply(
        lambda x: TextBlob(str(x).lower(), classifier=cl).classify()
    )
    

    Or if you want to refactor the possibly confusing lambda expression out,

    cl = NaiveBayesClassifier(train)
    
    def classify(x):
        return TextBlob(str(x).lower(), classifier=cl).classify()
    
    dataset['Polarity Class'] = dataset['Comment'].apply(classify)
    

    is equivalent.