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

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

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

if __name__ == '__main__':

    # import Counter
    from collections import Counter
    
    # pip install prettytable
    from prettytable import PrettyTable
    
    # import login, see http://blog.csdn.net/guaguastd/article/details/31706155 
    from login import oauth_login

    # get the twitter access api
    twitter_api = oauth_login()
    
    # import tweet, see http://blog.csdn.net/guaguastd/article/details/36163301
    from tweets import tweet

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

        for label, data in (('Word', words),
                            ('Screen Name', screen_names),
                            ('Hashtag', hashtags)):
            pt = PrettyTable(field_names=[label, 'Count'])
            c = Counter(data)
            [ pt.add_row(kv) for kv in c.most_common()[:10]]
            pt.align[label], pt.align['Count'] = 'l', 'r'
            print pt


Result:

Input the query (eg. #MentionSomeoneImportantForYou, exit to quit): Hello world
Length of statuses 99
'next_results'
+-------+-------+
| Word  | Count |
+-------+-------+
| the   |    99 |
| hello |    52 |
| is    |    50 |
| in    |    50 |
| me    |    46 |
| best  |    46 |
| you   |    46 |
| world |    44 |
| it    |    42 |
| tweet |    40 |
+-------+-------+
+--------------+-------+
| Screen Name  | Count |
+--------------+-------+
| Harry_Styles |    39 |
| justinbieber |     6 |
| shots        |     6 |
| john         |     6 |
| WHATCHAKNO   |     4 |
| hatahata88   |     2 |
| Michael5SOS  |     2 |
| Oprah_World  |     1 |
| kuga_aimu    |     1 |
| chriscobbins |     1 |
+--------------+-------+
+--------------+-------+
| Hashtag      | Count |
+--------------+-------+
| MoneyAnthem  |     4 |
| MILLIONBUCKS |     4 |
| New          |     4 |
| MUSTHEAR     |     4 |
| WorldCup2014 |     2 |
| gousa        |     1 |
| Lukaku       |     1 |
| USA          |     1 |
| BEL          |     1 |
| MGWV         |     1 |
+--------------+-------+

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



Python 对Twitter tweet的元素 (Word, Screen Name, Hash Tag)的频率分析,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。