码迷,mamicode.com
首页 > 编程语言 > 详细

Python 对Twitter tweet的元素 (Word, Screen Name, Hash Tag)的词汇多样性分析

时间:2014-07-03 18:48:39      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:twitter   数据挖掘   

CODE:

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

'''
Created on 2014-7-3
@author: guaguastd
@name: tweet_lexical_diversity.py
'''

# Compute lexical diversity
def lexical_diversity(tokens):
    return 1.0*len(set(tokens))/len(tokens)

# Compute the average number of words per tweet
def average_words(statuses):
    total_words = sum([len(s.split()) for s in statuses])
    return 1.0*total_words/len(statuses)
    
if __name__ == '__main__':

    # 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 token in (words, screen_names, hashtags):
            print '\rLexical diversity of %s: ' % token
            print lexical_diversity(token)

        for status in (status_texts,):
            print '\rAverage words of %s: ' % status
            print average_words(status)

RESULT:

Input the query (eg. #MentionSomeoneImportantForYou, exit to quit): #MentionSomeoneImportantForYou
Length of statuses 1

Lexical diversity of [u'#MentionSomeoneImportantForYou', u'@RihannaDaily']: 
1.0

Lexical diversity of [u'RihannaDaily']: 
1.0

Lexical diversity of [u'MentionSomeoneImportantForYou']: 
1.0

Average words of [u'#MentionSomeoneImportantForYou @RihannaDaily']: 
2.0

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)的词汇多样性分析

标签:twitter   数据挖掘   

原文地址:http://blog.csdn.net/guaguastd/article/details/36614833

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!