Skip to content

Instantly share code, notes, and snippets.

@TudorGruian
Created January 8, 2019 11:20
Show Gist options
  • Save TudorGruian/53fd9991dbdcb17f3146b77b3a877e74 to your computer and use it in GitHub Desktop.
Save TudorGruian/53fd9991dbdcb17f3146b77b3a877e74 to your computer and use it in GitHub Desktop.
from google.cloud import language_v1
from google.cloud.language_v1 import enums
import six
def sample_analyze_sentiment(content):
client = language_v1.LanguageServiceClient()
# content = 'Your text to analyze, e.g. Hello, world!'
if isinstance(content, six.binary_type):
content = content.decode('utf-8')
type_ = enums.Document.Type.PLAIN_TEXT
document = {'type': type_, 'content': content}
response = client.analyze_sentiment(document)
sentiment = response.document_sentiment
print('Score: {}'.format(sentiment.score))
print('Magnitude: {}'.format(sentiment.magnitude))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment