Python 对Twitter中指定话题的Tweet基本元素的频谱分析

CODE:

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

'''
Created on 2014-7-9
@author: guaguastd
@name: entities_frequency_map.py
'''

if __name__ == '__main__':

    # import Counter
    from collections import Counter
    
    # import visualize
    from visualize import visualize_frequency_map
    
    # pip install prettytable
    # from prettytable import PrettyTable

    # 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)):
            # Build a frequency map for each set of data and plot the values
            c = Counter(data)
            sTitle = label
            xLabel = "Bins (number of times an item appeared)"
            yLabel = "Number of items in bin"
            visualize_frequency_map(c.values(), sTitle, xLabel, yLabel)

RESULT:






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、付费专栏及课程。

余额充值