-
Notifications
You must be signed in to change notification settings - Fork 1
/
sentiment-analysis.py
39 lines (32 loc) · 1.22 KB
/
sentiment-analysis.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
import argparse
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
def main(movie_review_filename):
'''Run a sentiment analysis request on text within a passed filename.'''
credentials = GoogleCredentials.get_application_default()
service = discovery.build('language', 'v1', credentials=credentials)
with open(movie_review_filename, 'r') as review_file:
service_request = service.documents().analyzeSentiment(
body={
'document': {
'type': 'PLAIN_TEXT',
'content': review_file.read(),
}
}
)
response = service_request.execute()
score = response['documentSentiment']['score']
magnitude = response['documentSentiment']['magnitude']
for i, sentence in enumerate(response['sentences']):
sentence_sentiment = sentence['sentiment']['score']
print('Sentence {} has a sentiment score of {}'.format(i, sentence_sentiment))
print('Overall Sentiment: score of {} with magnitude of {}'.format(
score, magnitude))
return 0
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("movie_review_filename")
args = parser.parse_args()
if args is False:
SystemExit
main(args.movie_review_filename)