情绪分析评论数量少

2024-04-19 18:35:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在Twitter上进行情绪分析。我的代码运行没有任何问题,但有一件事引起了我的注意。当我为“堆栈溢出”运行脚本时,它总共只显示15条注释。我不认为这是可能的(只有15条评论与Twitter中的堆栈溢出有关)。 我不确定我在逻辑上是否做错了什么。你知道吗

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


Tags: keyfromposimporttokenauthsecretaccess