forked from 04RR/Salience-Analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
senti.py
37 lines (26 loc) · 1.07 KB
/
senti.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import sys
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = r"sentiment-analysis-284609-a234a6af795c.json"
def language_analysis(text):
client = language.LanguageServiceClient()
# The text to analyze
document = types.Document(
content=text,
type=enums.Document.Type.PLAIN_TEXT)
# Detects the sentiment of the text
sentiment = client.analyze_sentiment(document=document).document_sentiment
ent_analysis = client.analyze_entities(
document=document, encoding_type='UTF32')
dir(ent_analysis)
entities = ent_analysis.entities
return sentiment, entities
# text = 'Python is a great language i use everyday for my daily work and i make machine learning models and neural networks'
text = sys.argv[1]
sentiment, entities = language_analysis(text)
print('Text: {}'.format(text))
# print('Sentiment: {}, {}'.format(sentiment.score, sentiment.magnitude))
for e in entities:
print(e.name, e.metadata, e.salience)