【问题标题】:how can I suppress some output from the Python Library TextBlob sentiment.polarity如何抑制 Python 库 TextBlob 的一些输出
【发布时间】:2019-09-25 11:17:05
【问题描述】:

我正在使用 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】:

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

    【讨论】:

    猜你喜欢
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 2018-11-03
    • 2019-10-03
    • 2021-06-14
    相关资源
    最近更新 更多