-
Notifications
You must be signed in to change notification settings - Fork 1
/
sentimentQuery.py
executable file
·41 lines (30 loc) · 1.05 KB
/
sentimentQuery.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
35
36
37
38
39
40
41
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
from sentimentNews import news
from six import binary_type
from six.moves import input
# Unformatted text
def analyze_text(text):
client = language.LanguageServiceClient()
# print(text.encode('utf-8').strip())
if isinstance(text, binary_type):
text = text.decode('utf-8')
document = types.Document(
content=text,
type=enums.Document.Type.PLAIN_TEXT)
annotations = client.analyze_sentiment(document=document)
# Print the results
print_result(annotations)
def print_result(annotations):
score = annotations.document_sentiment.score
magnitude = annotations.document_sentiment.magnitude
print('Overall Sentiment: score of {} with magnitude of {}'.format(score, magnitude))
return 0
def query(searchTerm):
args = list([])
results = news(3, searchTerm, args)
while(not results.empty()):
analyze_text(results.get())
searchTerm = "trump"
query(searchTerm)