-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentiment_analysis.py
40 lines (31 loc) · 1.14 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
from alchemyapi import AlchemyAPI
import json
import sys
import os
#transcript = sys.argv[1]
transcript = open("transcripts/Republicans/rep-12-15-2015.txt")
names = []
texts = [""] * 25
for line in transcript:
for word in line.split():
if word.isupper():
if ':' in word:
if word not in names:
names.append(word)
for i in range(len(names)):
if line.find(names[i]) > -1:
texts[i] += line
alchemyapi = AlchemyAPI()
path = 'transcripts/Republicans/rep-12-15-2015.txt'
file_name = str(os.path.splitext(path)[0]) + '_sentiment.txt'
f = open(file_name, 'w')
for i in range(len(names)):
response = alchemyapi.sentiment('text', texts[i])
if response['status'] == 'OK':
f.write(str(texts[i].split()[0]) + ' Sentiment \n')
f.write('type: ' + str(response['docSentiment']['type']) + '\n')
if 'score' in response['docSentiment']:
f.write('score: ' + str(response['docSentiment']['score']) + '\n \n')
else:
print('Error in sentiment analysis call: ',
response['statusInfo'])