我正在从Twitter运行情绪分析。我的代码正在运行,没有任何问题,但是有一件事引起了我的注意。当我为“ Stack OverFlow”运行脚本时,它总共仅显示15条注释。我认为这是不可能的(只有15条评论与Twitter中的Stack OverFlow相关)。 我不确定我在逻辑上做错了什么。
import tweepy
from textblob import TextBlob
import pandas as pd
from plotly import __version__
import cufflinks as cf
from plotly.offline import download_plotlyjs,init_notebook_mode,plot,iplot
init_notebook_mode(connected=True)
cf.go_offline()
consumer_key = ''
consumer_key_secret = ''
access_token = ''
access_token_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_key_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
public_tweets = api.search('Stack OverFlow')
pos, neg, neu = 0, 0, 0
for tweet in public_tweets:
print(tweet.text)
analysis = TextBlob(tweet.text)
print(analysis.sentiment)
if analysis.sentiment[0]>0:
pos+=1
elif analysis.sentiment[0]<0:
neg+=1
else:
neu+=1
print("Positive: {}\nNegative: {}\nNeutral: {}".format(pos,neg,neu))
values=[pos,neg,neu]
Labels=["Positive","Negative","Neutral"]
df=pd.DataFrame({'Label':['Positive','Negative','Neutral'],'Values':[pos,neg,neu]})
df.iplot(kind='bar',x='Label',y='Values',title='Sentiment Analysis: Stack OverFlow')
****我的结果:情绪(极性= 0.125,主观性= 0.8) 正面:7 负数:1 中立:7 ****