Python 对Twitter tweet的元素 (Word, Screen Name, Hash Tag)的频率分析

CODE:

#!/usr/bin/python 
# -*- coding: utf-8 -*-

'''
Created on 2014-7-2
@author: guaguastd
@name: tweet_frequency_analysis.py
'''

if __name__ == '__main__':
    
    # import frequency
    from frequency import frequency_analysis
    
    # import search
    from search import search_for_tweet
        
    # import login, see http://blog.csdn.net/guaguastd/article/details/31706155 
    from login import twitter_login

    # get the twitter access api
    twitter_api = twitter_login()
    
    # import tweet
    from tweet import extract_tweet_entities

    while 1:
        query = raw_input('\nInput the query (eg. #MentionSomeoneImportantForYou, exit to quit): ')
        
        if query == 'exit':
            print 'Successfully exit!'
            break
        
        statuses = search_for_tweet(twitter_api, query)
        status_texts,screen_names,hashtags,words = extract_tweet_entities(statuses)  

        for label, data in (('Word', words),
                            ('Screen Name', screen_names),
                            ('Hashtag', hashtags)):
            frequency_analysis(label, data, 10)

RESULT:

Input the query (eg. #MentionSomeoneImportantForYou, exit to quit): #MentionSomeoneImportantForYou
Length of statuses 96
+--------------------------------+-------+
| Word                           | Count |
+--------------------------------+-------+
| #MentionSomeoneImportantForYou |    84 |
| RT                             |    49 |
| @paynashton                    |    13 |
| #mentionsomeoneimportantforyou |    12 |
| @gellystyles                   |    11 |
| @cuddlingxbrooks               |     9 |
| @sickhorandiva                 |     9 |
| @cuddlingxbrooks:              |     8 |
| so                             |     8 |
| @fratboyliamx                  |     7 |
+--------------------------------+-------+
+-----------------+-------+
| Screen Name     | Count |
+-----------------+-------+
| paynashton      |    18 |
| cuddlingxbrooks |    17 |
| gellystyles     |    15 |
| sickhorandiva   |    13 |
| SwaggyOnFire1   |     9 |
| TichaaAlves     |     7 |
| wtvpottorff     |     7 |
| idkdallasbae    |     7 |
| ElenaBomerC     |     7 |
| cuddings        |     7 |
+-----------------+-------+
+-----------------------------------+-------+
| Hashtag                           | Count |
+-----------------------------------+-------+
| MentionSomeoneImportantForYou     |    84 |
| mentionsomeoneimportantforyou     |    12 |
| MentionSomeoneBeautiful           |     1 |
| mentionyourinternetbestfriend     |     1 |
| MentionSomeoneYouLoveAndCareAbout |     1 |
| BAMsingleOutTmrw                  |     1 |
+-----------------------------------+-------+

Input the query (eg. #MentionSomeoneImportantForYou, exit to quit): exit
Successfully exit!

Python中进行Twitter数据情感分析,可以使用一些第三方库和工具来实现。以下是一个基本的步骤: 1. 获取Twitter数据:可以使用Twitter API来获取Twitter数据,或者使用一些第三方工具,如Tweepy等。 2. 数据预处理:对获取的Twitter数据进行预处理,包括去除停用词、标点符号和URL链接等,以及进行分词和词性标注等。 3. 情感分析:使用情感分析算法对预处理后的Twitter文本进行情感分类和打分,通常情感分析算法包括基于规则的方法、情感词典方法以及机器学习方法等。 4. 结果可视化:将情感分析结果可视化展示,如柱状图、饼状图等。 以下是一个使用Python进行Twitter数据情感分析的示例代码: ```python import tweepy from textblob import TextBlob import matplotlib.pyplot as plt # Twitter API credentials consumer_key = "your_consumer_key" consumer_secret = "your_consumer_secret" access_key = "your_access_key" access_secret = "your_access_secret" # Authenticate to Twitter auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_key, access_secret) # Create API object api = tweepy.API(auth) # Search for tweets containing a certain keyword public_tweets = api.search('Python') # Initialize variables to store sentiment scores positive = 0 negative = 0 neutral = 0 # Loop through each tweet and perform sentiment analysis for tweet in public_tweets: analysis = TextBlob(tweet.text) if analysis.sentiment.polarity > 0: positive += 1 elif analysis.sentiment.polarity < 0: negative += 1 else: neutral += 1 # Visualize the sentiment analysis results labels = ['Positive', 'Negative', 'Neutral'] sizes = [positive, negative, neutral] colors = ['yellowgreen', 'lightcoral', 'gold'] explode = (0.1, 0, 0) plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=140) plt.axis('equal') plt.show() ``` 上述代码使用Tweepy库获取包含关键词“Python”的Twitter数据,使用TextBlob库进行情感分析,然后使用Matplotlib库将情感分析结果可视化展示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值