-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentiment.py
81 lines (66 loc) · 2.49 KB
/
sentiment.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""Demonstrates how to make a simple call to the Natural Language API."""
import argparse
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
from google.oauth2 import service_account
from pygame import mixer
import random
import time
#Attempting to fix credentials problem
credentials = service_account.Credentials.from_service_account_file(
'/home/parul/rare_use/hackpsu/mood-songs/creds.json')
def print_result(annotations):
score = annotations.document_sentiment.score
magnitude = annotations.document_sentiment.magnitude
for index, sentence in enumerate(annotations.sentences):
sentence_sentiment = sentence.sentiment.score
print('Sentence {} has a sentiment score of {}'.format(
index, sentence_sentiment))
print('Overall Sentiment: score of {} with magnitude of {}'.format(
score, magnitude))
return 0
def analyze(content):
"""Run a sentiment analysis request on text within a passed filename."""
client = language.LanguageServiceClient(credentials=credentials)
#with open(movie_review_filename, 'r') as review_file:
# Instantiates a plain text document.
# content = review_file.read()
print(content)
document = types.Document(
content=content,
type=enums.Document.Type.PLAIN_TEXT)
annotations = client.analyze_sentiment(document=document)
sentiment = annotations.document_sentiment
# Print the results
print(sentiment.score, sentiment.magnitude)
print_result(annotations)
return sentiment
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'movie_review_filename',
help='The filename of the movie review you\'d like to analyze.')
args = parser.parse_args()
#movie_review_filename = 'reviews/bladerunner-neg.txt'
with open(args.movie_review_filename, 'r') as review_file:
# Instantiates a plain text document.
content = review_file.read()
sentiment = analyze(content)
#print(sentiment.score, sentiment.magnitude)
r = random.randint(1,4)
print(r)
if sentiment.score<0:
print('Its sad!')
song = "songs/sad/"+str(r)+".mp3"
else:
print('Its happy!')
song = "songs/happy/"+str(r)+".mp3"
mixer.init()
mixer.music.load(song)
print('Playing music')
mixer.music.play()
time.sleep(120)
print('Exiting...')