-
Notifications
You must be signed in to change notification settings - Fork 0
/
FinalProject.py
76 lines (63 loc) · 2.11 KB
/
FinalProject.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
# Removes punctuation from word
def strip_punctuation(word):
for char in word:
if char in punctuation_chars:
print
word = word.replace(char, "")
return word
# It returns the total negative word count
def get_neg(str):
str = strip_punctuation(str).lower()
count = 0
for word in str.split():
if word in negative_words:
count = count + 1
return count
# It returns the total positive word count
def get_pos(str):
str = strip_punctuation(str).lower()
count = 0
for word in str.split():
if word in positive_words:
count = count + 1
return count
punctuation_chars = ["'", '"', ",", ".", "!", ":", ";", '#', '@']
# lists of words to use
# Its give positive words only and save in positive_word list
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())
# Its give negative words only and save in negative_word list
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())
count = 0
noOfRetweet = []
noOfReplies = []
pcount = []
ncount = []
fileobj = open("project_twitter_data.csv", "r")
content = fileobj.readlines()
for line in content[1:]:
value = line.split(",")
noOfRetweet.append(value[1])
noOfReplies.append(value[2].strip())
pcount.append(get_pos(value[0]))
ncount.append(get_neg(value[0]))
count += 1
netScore = [p-n for p, n in zip(pcount,ncount)] # zip get 1st element from pcount and ncount, perform action(p-n) save in a list.Similarly perform with the rest of the element.
fileobj2 = open("resulting_data.csv", "w")
fileobj2.write('Number of Retweets, Number of Replies, Positive Score, Negative Score, Net Score')
fileobj2.write('\n')
i = 0
while i < count:
row_string = '{},{},{},{},{}'.format(noOfRetweet[i], noOfReplies[i], pcount[i], ncount[i], netScore[i])
print(row_string)
fileobj2.write(row_string)
fileobj2.write('\n')
i += 1
fileobj2.close()