我如何抑制Python库TextBlob sentiment.polarity中的某些输出

问题描述 投票:0回答:1

我正在使用Python为从TextBlob返回的结果分配标签。我非常基本的代码如下:

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))

这可以正确打印出情绪,但也可以打印出“无”,如下所示:

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

Positive

None

...

有什么方法可以禁止打印“无”?

感谢您的帮助或指点。

python output textblob suppress
1个回答
0
投票

您的函数sentLabel返回None。因此,当您使用print(sentLabel(b1))时,它会打印None

这应该为您工作。

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)
© www.soinside.com 2019 - 2024. All rights reserved.