使用Python编程进行Twitter情绪分析。
情绪分析是评估人们通过书面文本或口头交流对特定事件反馈的情绪的过程。当然,口头交流也必须转换为书面文本,以便可以通过Python程序进行分析。人们表达的情绪可能是积极的或消极的。通过对情绪文本中不同单词赋予权重,我们计算出一个数值,这给了我们对情绪的数学评估。
实用性
客户反馈 − 了解客户对产品或服务的意见对于企业至关重要。当客户的反馈以书面文本的形式提供时,我们可以在Twitter上运行情绪分析,以编程方式找出整体反馈是正面还是负面,并采取纠正措施。
政治活动 − 对于政治对手来说,了解他们发表演讲的对象的反应至关重要。如果可以通过社交媒体等在线平台收集公众的反馈,那么我们可以判断公众对特定演讲的反应。
政府举措 − 当政府不时实施新计划时,他们可以通过了解民意来判断对新计划的反应。公众常常通过Twitter表达他们的赞扬或不满。
方法
下面列出了使用Python构建情绪分析程序所需的步骤。
首先,我们安装Tweepy和TextBlob。此模块将帮助我们从Twitter收集数据,以及提取文本并进行处理。
验证Twitter。我们需要使用API密钥才能从Twitter提取数据。
然后,我们根据推文中的文本将推文分类为正面推文和负面推文。
示例
import re import tweepy from tweepy import OAuthHandler from textblob import TextBlob class Twitter_User(object): def __init__(self): consumer_key = '1ZG44GWXXXXXXXXXjUIdse' consumer_secret = 'M59RI68XXXXXXXXXXXXXXXXV0P1L6l7WWetC' access_token = '865439532XXXXXXXXXX9wQbgklJ8LTyo3PhVDtF' access_token_secret = 'hbnBOz5XXXXXXXXXXXXXefIUIMrFVoc' try: self.auth = OAuthHandler(consumer_key, consumer_secret) self.auth.set_access_token(access_token, access_token_secret) self.api = tweepy.API(self.auth) except: print("Error: Authentication Failed") def pristine_tweet(self, twitter): return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)", " ", twitter).split()) def Sentiment_Analysis(self, twitter): audit = TextBlob(self.pristine_tweet(twitter)) # set sentiment if audit.sentiment.polarity > 0: return 'positive' elif audit.sentiment.polarity == 0: return 'negative' def tweet_analysis(self, query, count = 10): twitter_tweets = [] try: get_twitter = self.api.search(q = query, count = count) for tweets in get_twitter: inspect_tweet = {} inspect_tweet['text'] = tweets.text inspect_tweet['sentiment'] = self.Sentiment_Analysis(tweets.text) if tweets.retweet_count > 0: if inspect_tweet not in twitter_tweets: twitter_tweets.append(inspect_tweet) else: twitter_tweets.append(inspect_tweet) return twitter_tweets except tweepy.TweepError as e: print("Error : " + str(e)) def main(): api = Twitter_User() twitter_tweets = api.tweet_analysis(query = 'Ram Nath Kovind', count = 200) Positive_tweets = [tweet for tweet in twitter_tweets if tweet['sentiment'] == 'positive'] print("Positive tweets percentage: {} %".format(100*len(Positive_tweets)/len(twitter_tweets))) Negative_tweets = [tweet for tweet in twitter_tweets if tweet['sentiment'] == 'negative'] print("Negative tweets percentage: {} %".format(100*len(Negative_tweets)/len(twitter_tweets))) print("\n\nPositive_tweets:") for tweet in Positive_tweets[:10]: print(tweet['text']) print("\n\nNegative_tweets:") for tweet in Negative_tweets[:10]: print(tweet['text']) if __name__ == "__main__": main()
输出
运行上述代码会得到以下结果:
Positive tweets percentage: 48.78048780487805 % Negative tweets percentage: 46.34146341463415 % Positive_tweets: RT @heartful_ness: "@kanhashantivan presents a model of holistic living. My deep & intimate association with this organisation goes back to… RT @heartful_ness: Heartfulness Guide @kamleshdaaji welcomes honorable President of India Ram Nath Kovind @rashtrapatibhvn, honorable first… RT @DrTamilisaiGuv: Very much pleased by the affection shown by our Honourable President Sri Ram Nath Kovind and First Lady madam Savita Ko… RT @BORN4WIN: Who became the first President of India from dalit community? A) K.R. Narayanan B) V. Venkata Giri C) R. Venkataraman D) Ram… Negative_tweets: RT @Keyadas63: What wuld those #empoweredwomen b termed who reach Hon HC at the drop of a hat But Demand #Alimony Maint? @MyNation_net @vaa… RT @heartful_ness: Thousands of @heartful_ness practitioners meditated with Heartfulness Guide @kamleshdaaji at @kanhashantivan & await the… RT @TurkeyinDelhi: Ambassador Sakir Ozkan Torunlar attended the Joint Session of Parliament of #India and listened the address of H.E. Shri…
广告