热线电话:13121318867

登录
2019-03-28 阅读量: 598
如何用python进行情感分析

我们在计划中遵循以下三个主要步骤:

  • 授权twitter API客户端。
  • 向Twitter API发出GET请求以获取特定查询的推文。
  • 解析推文。将每条推文分类为正面,负面或中立。

  • 首先,我们创建一个TwitterClient类。该类包含与Twitter API交互和解析推文的所有方法。我们使用__init__函数来处理API客户端的身份验证。
  • get_tweets函数中,我们使用:fetched_tweets = self.api.search(q = query,count = count)
    调用Twitter API来获取推文。
  • get_tweet_sentiment中,我们使用textblob模块。analysis = TextBlob(self.clean_tweet(tweet))

import re

import tweepy

from tweepy import OAuthHandler

from textblob import TextBlob

class TwitterClient(object):

'''

Generic Twitter Class for sentiment analysis.

'''

def __init__(self):

'''

Class constructor or initialization method.

'''

# keys and tokens from the Twitter Dev Console

consumer_key = 'XXXXXXXXXXXXXXXXXXXXXXXX'

consumer_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'

access_token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'

access_token_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXX'

# attempt authentication

try:

# create OAuthHandler object

self.auth = OAuthHandler(consumer_key, consumer_secret)

# set access token and secret

self.auth.set_access_token(access_token, access_token_secret)

# create tweepy API object to fetch tweets

self.api = tweepy.API(self.auth)

except:

print("Error: Authentication Failed")

30.7336
2
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子