Python 如何使用Google Cloud NL api进行情绪分析?

Python 如何使用Google Cloud NL api进行情绪分析?,python,twitter,textblob,google-cloud-nl,Python,Twitter,Textblob,Google Cloud Nl,如何使用Google cloud NL api对我选择的主题(关键字)的Twitter推文进行情感分析 我可以编写使用Twitter(twitterapi)的python脚本,以了解人们对我使用python的NL库“TextBlob”选择的主题的感受 import tweepy from textblob import TextBlob # Step 1 - Authenticate consumer_key= 'CONSUMER_KEY_HERE' consumer_secret= 'CO

如何使用Google cloud NL api对我选择的主题(关键字)的Twitter推文进行情感分析

我可以编写使用Twitter(twitterapi)的python脚本,以了解人们对我使用python的NL库“TextBlob”选择的主题的感受

 import tweepy from textblob import TextBlob

# Step 1 - Authenticate
consumer_key= 'CONSUMER_KEY_HERE'
consumer_secret= 'CONSUMER_SECRET_HERE'

access_token='ACCESS_TOKEN_HERE'
access_token_secret='ACCESS_TOKEN_SECRET_HERE'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

#Step 3 - Retrieve Tweets
public_tweets = api.search('Trump')



#CHALLENGE - Instead of printing out each tweet, save each Tweet to a CSV file
#and label each one as either 'positive' or 'negative', depending on the sentiment 
#You can decide the sentiment polarity threshold yourself


for tweet in public_tweets:
    print(tweet.text)

    #Step 4 Perform Sentiment Analysis on Tweets
    analysis = TextBlob(tweet.text)
    print(analysis.sentiment)
    print("")

您可以使用
谷歌云

此外,您还可以使用中的
track
参数实时过滤特定主题的推文

# Import the module and create a language client
from google.cloud import language
language_client = language.Client()

# Analyze the sentiment
document = language_client.document_from_html(tweet.text)
annotations = document.analyze_sentiment()
print(annotations.score, annotations.magnitude)