-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.txt
62 lines (43 loc) · 1.68 KB
/
code.txt
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 = ["'", '"', ",", ".", "!", ":", ";", '#', '@']
# 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())
def strip_punctuation(string):
for char in string:
if char in punctuation_chars:
string=string.replace(char,"")
return string
def get_neg(string):
ch=0
for char in string.split(" "):
if strip_punctuation(char) in negative_words:
ch+=1
return ch
def get_pos(sentence):
count=0
for string in sentence.split(" "):
if strip_punctuation(string) in positive_words:
count+=1
return count
projectTwitterDataFile=open("project_twitter_data.csv","r")
resultingDataFile=open("resulting_data.csv","w")
def writeInDataFile(resultingDataFile):
resultingDataFile.write("Number of Retweets, Number of Replies, Positive Score, Negative Score, Net Score")
resultingDataFile.write("\n")
linesPTDF = projectTwitterDataFile.readlines()
headerDontUsed= linesPTDF.pop(0)
for linesTD in linesPTDF:
listTD = linesTD.strip().split(',')
resultingDataFile.write("{}, {}, {}, {}, {}".format(listTD[1], listTD[2], get_pos(listTD[0]), get_neg(listTD[0]), (get_pos(listTD[0])-get_neg(listTD[0]))))
resultingDataFile.write("\n")
writeInDataFile(resultingDataFile)
projectTwitterDataFile.close()
resultingDataFile.close()