2017-01-13 4 views
1

내가 선택한 주제 (키워드)로 트위터의 트윗에 대한 정서 분석을 위해 Google 클라우드 NL API를 어떻게 사용할 수 있습니까?감정 분석을 위해 Google Cloud NL API를 사용하려면 어떻게해야하나요?

내가 트위터 (트위터 API) 것을 사용 파이썬 스크립트를 작성할 수 있습니다

명 내가 당신은 google-cloudpython module 사용할 수 있습니다 파이썬의 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("") 

답변

1

사용을 선택 주제에 대해 느끼는 방법 :

# 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 APItrack 매개 변수를 사용하여 특정 주제의 트윗을 실시간으로 필터링 할 수 있습니다.

관련 문제