![](/img/trans.png)
[英]How does TextBlob calculate sentiment polarity? How can I calculate a value for sentiment with machine learning classifier?
[英]how can I suppress some output from the Python Library TextBlob sentiment.polarity
我正在使用 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
...
有什么办法可以禁止打印“无”?
感谢您的任何帮助或指点。
您的 function 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.