Как я могу использовать API Google Cloud NL для анализа настроений?

Как я могу использовать API Google Cloud NL для анализа тональности твитов из Twitter с выбранной мной темой (ключевым словом)?

Я могу написать скрипт Python, который использует Twitter (Twitter api), чтобы понять, как люди относятся к теме, которую я выбираю, используя библиотеку 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("")

person Sanjay Shiradwade    schedule 13.01.2017    source источник


Ответы (1)


Вы можете использовать google-cloud модуль python:

# 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)

Кроме того, вы можете использовать параметр track в tweepy Streaming API для фильтрации твитов по определенной теме в реальном времени.

person Sara    schedule 13.01.2017