【问题标题】:How can I use Google Cloud NL api for sentiment analysis?如何使用 Google Cloud NL api 进行情绪分析?
【发布时间】:2017-01-13 08:54:10
【问题描述】:

如何使用谷歌云 NL api 对来自 Twitter 的带有我选择的主题(关键字)的推文进行情绪分析?

我可以编写使用 Twitter(Twitter api)的 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("")

【问题讨论】:

    标签: python twitter textblob google-cloud-nl


    【解决方案1】:

    您可以使用google-cloud python module:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-16
      • 1970-01-01
      • 2015-09-23
      • 2012-05-01
      相关资源
      最近更新 更多