Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 0
  1. from google.cloud import language
  2. from google.cloud.language import enums
  3. from google.cloud.language import types
  4.  
  5. def get_sentiment_entity(document):
  6.     output = {}
  7.     client = language.LanguageServiceClient()
  8.     document = types.Document(
  9.         content=document,
  10.         type=enums.Document.Type.PLAIN_TEXT)
  11.  
  12.     sentiment = client.analyze_sentiment(document=document).document_sentiment
  13.     output["doc_sent"] = sentiment.score
  14.     output["doc_mag"] = sentiment.magnitude
  15.    
  16.     entities = client.analyze_entity_sentiment(document=document)
  17.     output['entities'] = list(set([e.name for e in entities.entities if (abs(e.sentiment.score >0))]))
  18.     return output
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement