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)的频率分析,布布扣,bubuko.com

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

上一篇:Javascript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等)


下一篇:java读取properties文件的方法