-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourse2project.py
More file actions
50 lines (43 loc) · 1.47 KB
/
course2project.py
File metadata and controls
50 lines (43 loc) · 1.47 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
def strip_punctuation(word):
for i in punctuation_chars:
word = word.replace(i, '')
return word
def get_pos(line):
count = 0
for word in line.split():
if strip_punctuation(word).lower() in positive_words:
count += 1
return count
def get_neg(line):
count = 0
for word in line.split():
if strip_punctuation(word).lower() in negative_words:
count += 1
return count
punctuation_chars = ["'", '"', ",", ".", "!", ":", ";", '#', '@']
# 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())
f = open('project_twitter_data.csv', 'r')
ansfile = open('resulting_data.csv', 'w')
ansfile.write('Number of Retweets, Number of Replies, Positive Score, Negative Score, Net Score\n')
i = 0
for line in f.readlines():
if(i == 0):
i = 1
continue
temp = line.split(',')
retweet_count = temp[1]
reply_count = temp[2].strip('\n')
pos_score = get_pos(line)
neg_score = get_neg(line)
net_score = pos_score - neg_score
ansfile.write('{},{},{},{},{}\n'.format(retweet_count, reply_count, pos_score, neg_score, net_score))