forked from ayushagarwal001/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.py
163 lines (120 loc) · 3.13 KB
/
Test.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import pandas as pd
punctuation_chars = ["'", '"', ",", ".", "!", ":", ";", '#', '@']
# lists of words to use
positive_words = []
with open("positive_words.txt.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.txt") as pos_f:
for lin in pos_f:
if lin[0] != ';' and lin[0] != '\n':
negative_words.append(lin.strip())
string = ""
def strip_punctuation(string):
for y in punctuation_chars:
if y in string:
string=string.replace(y,"")
return(string)
def get_pos(str1):
l1=[]
l2=[]
cnt = 0
s = str1.lower()
l1 = s.split(" ")
for x in l1:
l2.append(strip_punctuation(x))
for y in positive_words:
if y in l2:
print(y)
cnt = cnt + 1
print(cnt)
return cnt
def get_neg(str1):
l1=[]
l2=[]
cnt = 0
s = str1.lower()
l1 = s.split(" ")
for x in l1:
l2.append(strip_punctuation(x))
for y in negative_words:
if y in l2:
print(y)
cnt = cnt + 1
print(cnt)
return cnt
#def get_count(strng):
# neg = get_neg(strng)
# pos = get_pos(strng)
# return (neg,pos);
def get_data(strng):
print(strng)
l3=[]
l4=[]
l5=[]
str1 = strng[0]
str2 = str1.split(",")
str3 = str2[0]
ps = get_pos(str3)
ng = get_neg(str3)
l4.append(ps)
l5.append(ng)
#print(l4)
#print(l5)
return list(zip(l4,l5))
l3=[]
l4=[]
l5=[]
lnlist=[]
rtcount=[]
repcount=[]
import csv
with open("project_twitter_data.csv", mode ='r')as file:
csvFile = csv.reader(file)
for lines in csvFile:
lnlist.append(lines)
#str2 = lines[0]
#l3 = str2.split(",")
#str3 = l3[0]
#ps = get_pos(str3)
#ng = get_neg(str3)
#l4.append(ps)
#l5.append(ng)
#data = list(zip(l4,l5))
for x in lnlist[1:]:
#print(x)
str2 = x[0]
l3 = str2.split(",")
strn1 = l3[1]
strn2 = l3[2]
rtcount.append(strn1)
repcount.append(strn2)
str3 = l3[0]
ps = get_pos(str3)
ng = get_neg(str3)
l4.append(ps)
l5.append(ng)
data = list(zip(l4,l5))
fields = ['Number of Retweets', 'Number of Replies', 'Positive Score', 'Negative Score', 'Net Score']
rows=[]
#def rowfn(x,y,z,u):
# return None
#for x,y,z,u in map(rowfn, rtcount,repcount,l4,l5):
# list1 = [x,y,z,u,z-u]
# rows.append(list1)
from itertools import zip_longest
for i, j, k, l in zip_longest(rtcount,repcount,l4,l5):
list1 = [i,j,k,l,k-l]
rows.append(list1)
filename = "result_data.csv"
with open(filename, 'w') as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow(fields)
csvwriter.writerows(rows)
with open('result_data.csv') as f:
for line in f:
print(line.strip())
print(data)
print(rows)