pythonoutputtextblobsuppress

how can I suppress some output from the Python Library TextBlob sentiment.polarity


I am using Python to assign labels to results returned from TextBlob. My very basic code looks like this:

from textblob import TextBlob

def sentLabel(blob):
    label = blob.sentiment.polarity 

    if(label == 0.0):
        print('Neutral')
    elif(label > 0.0):
        print('Positive')
    else:
        print('Negative')

    Feedback1 = "The food in the canteen was awesome"
    Feedback2 = "The food in the canteen was awful"
    Feedback3 = "The canteen has food"


    b1 = TextBlob(Feedback1)
    b2 = TextBlob(Feedback2)
    b3 = TextBlob(Feedback3)

    print(b1.sentiment_assessments)
    print(sentLabel(b1))
    print(b2.sentiment_assessments)
    print(sentLabel(b2))
    print(b3.sentiment_assessments)
    print(sentLabel(b3))

This prints out the sentiment correctly but it also prints out "None" as shown below:

Sentiment(polarity=1.0, subjectivity=1.0, assessments=[(['awesome'], 1.0, 1.0, None)])

Positive

None

...

Is there any way to suppress "None" from being printed?

Thanks for any help or pointers.


Solution

  • Your function sentLabel return None. Hence when you use print(sentLabel(b1)), it prints None.

    This should work for you.

    from textblob import TextBlob
    
    def sentLabel(blob):
        label = blob.sentiment.polarity 
    
        if(label == 0.0):
            print('Neutral')
        elif(label > 0.0):
            print('Positive')
        else:
            print('Negative')
    
        Feedback1 = "The food in the canteen was awesome"
        Feedback2 = "The food in the canteen was awful"
        Feedback3 = "The canteen has food"
    
    
        b1 = TextBlob(Feedback1)
        b2 = TextBlob(Feedback2)
        b3 = TextBlob(Feedback3)
    
        print(b1.sentiment_assessments)
        sentLabel(b1)
        print(b2.sentiment_assessments)
        sentLabel(b2)
        print(b3.sentiment_assessments)
        sentLabel(b3)