我对Python还是很陌生,想编写一个程序,该程序使用特定的关键字流推文,然后对其应用TextBlob情感。我能够连续运行推文流,但是每条推文的情感分析返回“ None”。我该如何应用它?对此代码的任何建议也将不胜感激。
def clean_tweet(tweet):
return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)", " ", tweet).split())
def analize_tweets(tweets):
positive = 0
negative = 0
neutral = 0
polarity = 0
analysis = TextBlob(clean_tweet(tweets))
polarity += analysis.sentiment.polarity
if (analysis.sentiment.polarity == 0):
neutral += 1
elif (analysis.sentiment.polarity < 0.00):
negative += 1
elif (analysis.sentiment.polarity > 0.00):
positive += 1
class TwitterListener(tw.StreamListener):
def on_status(self, status):
print(status.text)
print(analize_tweets(status.text))
def on_error(self, status_code):
if status_code == 420:
return False
def _data(self, data):
if self.listener.on_data(data) is False:
self.running = False
streamlistener = TwitterListener()
tweets = tw.Stream(auth = api.auth, listener=streamlistener)
tweets.filter(track=['Trump'], languages = ["en"])
最后,我希望实时绘制一个图表,其中包含结果流。不过,到目前为止,我只希望将情感分析应用于每条推文。