情感分析是估算通过书面文本或口头交流为特定事件提供反馈的人们的情绪的过程。当然,口头交流也必须转换为书面文本,以便可以通过python程序进行分析。人们表达的情绪可能是正面的也可能是负面的。通过为情感文本中的不同单词分配权重,我们可以计算出一个数值,从而对情感进行数学评估。
客户反馈-了解客户对产品或服务的意见对企业至关重要。当客户的反馈以书面形式提供时,我们可以在Twitter中运行情感分析,以编程方式找出总体反馈是正面还是负面,并采取纠正措施。
政治运动-对于政治反对派来说,了解演讲者的反应至关重要。如果可以通过在线平台(如社交媒体平台)收集公众的反馈,那么我们可以判断公众对特定演讲的回应。
政府倡议-政府不时实施新计划时,他们可以通过征询公众意见来判断对新计划的反应。公众经常通过Twitter表达赞扬或愤怒。
下面我们列出了用python构建情感分析程序所需的步骤。
首先,我们安装Tweepy和TextBlob。该模块将帮助我们从Twitter收集数据以及提取文本并进行处理。
向Twitter进行身份验证。我们需要使用API键,以便可以从高音扬声器中提取数据。
然后,根据推文中的文本将推文分为正面和负面推文。
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…