-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentiment_analysis.py
More file actions
62 lines (51 loc) · 1.68 KB
/
sentiment_analysis.py
File metadata and controls
62 lines (51 loc) · 1.68 KB
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
punctuation_chars = ["'", '"', ",", ".", "!", ":", ";", '#', '@']
def strip_punctuation(wrd):
new_wrd = ""
idx = 0
while idx < len(wrd):
if wrd[idx] not in punctuation_chars:
new_wrd = new_wrd + wrd[idx]
idx = idx + 1
return new_wrd
def get_pos(sentences):
count = 0
for wrd in sentences.lower().split():
word = strip_punctuation(wrd)
if word in positive_words:
count = count + 1
return count
def get_neg(sentences):
count = 0
for wrd in sentences.lower().split():
word = strip_punctuation(wrd)
if word in negative_words:
count = count + 1
return count
# lists of words to use
positive_words = []
with open("positive_words.txt") as pos_f:
for lin in pos_f:
if lin[0] != ';' and lin[0] != '\n':
positive_words.append(lin.strip())
negative_words = []
with open("negative_words.txt") as pos_f:
for lin in pos_f:
if lin[0] != ';' and lin[0] != '\n':
negative_words.append(lin.strip())
tweets_file = open('project_twitter_data.csv', 'r')
tweets = tweets_file.readlines()
#print(tweets)
outfile = open('resulting_data.csv', 'w')
#output the header row
outfile.write('Number of Retweets, Number of Replies, Positive Score, Negative Score, Net Score\n')
#print(tweets)
for tweet in tweets[1:]:
data = tweet.strip().split(',')
retweets = data[1]
replies = data[2]
positive_score = get_pos(data[0])
negative_score = get_neg(data[0])
net_score = positive_score - negative_score
outfile.write('{},{},{},{},{}\n'.format(retweets, replies, positive_score, negative_score, net_score))
tweets_file.close()
outfile.close()