为了账号安全,请及时绑定邮箱和手机立即绑定

如何抑制 Python 库 TextBlob 的一些输出

如何抑制 Python 库 TextBlob 的一些输出

侃侃尔雅 2022-06-07 16:55:42
我正在使用 Python 为从 TextBlob 返回的结果分配标签。我非常基本的代码如下所示:from textblob import TextBlobdef 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)])PositiveNone...有什么办法可以禁止打印“无”?感谢您的任何帮助或指点。
查看完整描述

1 回答

?
三国纷争

TA贡献1804条经验 获得超7个赞

你的函数sentLabelreturn 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)


查看完整回答
反对 回复 2022-06-07
  • 1 回答
  • 0 关注
  • 243 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号