扩大Twitter情绪分析

时间:2017-10-29 22:38:07

标签: python twitter nlp

下面的代码分析了Twitter的情绪:是积极的,消极的还是中性的。然而,对于许多推文来说这是相当不准确的,例如,如果它包括“有人给了他一个中指的saulte”,我想训练该程序以识别中指意味着不尊重,即使它在句子中包括敬礼这个词。

任何建议都将不胜感激。

导入重新 导入tweepy 来自tweepy导入OAuthHandler 来自textblob导入TextBlob

class TwitterClient(object):
    '''
    Generic Twitter Class for sentiment analysis.
    '''
    def __init__(self):
        '''
        Class constructor or initialization method.
        '''
        # keys and tokens from the Twitter Dev Console
        consumer_key = 'WHexAxkRn6uEJkzS2CKpeQejI'
        consumer_secret = 'fSxjGVM247YS6Y6BpkWXaIfr6ThXdoSUg2y0aR259vNXVPPfob'
        access_token = '915324744140025862-jnGvcTPkJHOObkeydiVburK8SdAngEk'
        access_token_secret = 'JGgkWI9Lq0rJU1K0C8JLplRnSrEuw8pj3anOlIsn3YdiO'


        # attempt authentication
        try:
            # create OAuthHandler object
            self.auth = OAuthHandler(consumer_key, consumer_secret)
            # set access token and secret
            self.auth.set_access_token(access_token, access_token_secret)
            # create tweepy API object to fetch tweets
            self.api = tweepy.API(self.auth)
        except:
            print("Error: Authentication Failed")

    def clean_tweet(self, tweet):
        '''
        Utility function to clean tweet text by removing links, special characters
        using simple regex statements.
        '''
        return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)", " ", tweet).split())

    def get_tweet_sentiment(self, tweet):
        '''
        Utility function to classify sentiment of passed tweet
        using textblob's sentiment method
        '''
        # create TextBlob object of passed tweet text
        analysis = TextBlob(self.clean_tweet(tweet))
        # set sentiment
        if analysis.sentiment.polarity > 0:
            return 'positive'
        elif analysis.sentiment.polarity == 0:
            return 'neutral'
        else:
            return 'negative'

    def get_tweets(self, query, count = 30):
        '''
        Main function to fetch tweets and parse them.
        '''
        # empty list to store parsed tweets
        tweets = []

        try:
            # call twitter api to fetch tweets
            fetched_tweets = self.api.search(q = query, count = count)

            # parsing tweets one by one
            for tweet in fetched_tweets:
                # empty dictionary to store required params of a tweet
                parsed_tweet = {}

                # saving text of tweet
                parsed_tweet['text'] = tweet.text
                # saving sentiment of tweet
                parsed_tweet['sentiment'] = self.get_tweet_sentiment(tweet.text)

                # appending parsed tweet to tweets list
                if tweet.retweet_count > 0:
                    # if tweet has retweets, ensure that it is appended only once
                    if parsed_tweet not in tweets:
                        tweets.append(parsed_tweet)
                else:
                    tweets.append(parsed_tweet)

            # return parsed tweets
            return tweets

        except tweepy.TweepError as e:
            # print error (if any)
            print("Error : " + str(e))

def main():
    # creating object of TwitterClient Class
    api = TwitterClient()
    # calling function to get tweets
    tweets = api.get_tweets(query = 'Donald Trump', count = 200)

    # picking positive tweets from tweets
    ptweets = [tweet for tweet in tweets if tweet['sentiment'] == 'positive']
    # percentage of positive tweets
    print("Positive tweets percentage: {} %".format(100*len(ptweets)/len(tweets)))
    # picking negative tweets from tweets
    ntweets = [tweet for tweet in tweets if tweet['sentiment'] == 'negative']
    # percentage of negative tweets
    print("Negative tweets percentage: {} %".format(100*len(ntweets)/len(tweets)))
    # percentage of neutral tweets
    print("Neutral tweets percentage:{}%".format(100*(len(tweets) - len(ntweets) - len(ptweets))/len(tweets)))

    # printing first 5 positive tweets
    print("\n\nPositive tweets:")
    for tweet in ptweets[:20]:
        print(tweet['text'])

    # printing first 5 negative tweets
    print("\n\nNegative tweets:")
    for tweet in ntweets[:20]:
        print(tweet['text'])

if __name__ == "__main__":
    # calling main function
    main()

1 个答案:

答案 0 :(得分:0)

此算法不遵循机器学习中使用的任何分类程序,因此它没有任何训练。这是一种基于非常基本的统计程序的算法,要执行该算法,要求事先按感觉对一袋单词进行分类(一包正单词和另一个带有负单词的情感)。

按照这种基本的统计程序,将单词分类为中立是极其复杂的。这就是为什么您的算法效果不佳的原因。

此外,输入转发率大于0

if tweet.retweet_count> 0 :

但是,如果在一段时间内无法衡量转发的比例,那将毫无意义。

因此,您的算法很难正常工作。我建议对员工单词袋单词排名和令牌化进行更多研究。

您可以查看此链接以获取一些详细信息:https://www.pluralsight.com/guides/building-a-twitter-sentiment-analysis-in-python

成功和问候。