-
Notifications
You must be signed in to change notification settings - Fork 36
/
preprocessor.py
168 lines (128 loc) · 4.21 KB
/
preprocessor.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
164
165
166
167
168
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 26 16:34:13 2018
@author: Nikie Jo Deocampo
"""
import json
import csv
from nltk.tokenize import word_tokenize
import string
import re
import time
import pandas as pd
tweets_data = []
x = []
y = []
k = []
some_milby = []
print("===========================")
print("Starting Preprocess Function")
print("=========================== \n\n")
def getdata(dataurl):
print("===========================")
print("Retrieving TXT File")
tweets_data_path = dataurl
tweets_file = open(tweets_data_path, "r")
for line in tweets_file:
try:
tweet = json.loads(line)
tweets_data.append(tweet)
except:
continue
print("===========================")
print("Retrieving Successfull")
print("=========================== \n \n")
time.sleep(3)
processdata()
def processdata():
print("===========================")
print("Recovering Data Teets")
print("===========================")
time.sleep(1)
RE_EMOJI = re.compile('[\U00010000-\U0010ffff]', flags=re.UNICODE)
for i in range(len(tweets_data)):
q = tweets_data[i]['text']
o = tweets_data[i]['id_str']
q = RE_EMOJI.sub(r'', q)
i = q.translate(str.maketrans('','',string.punctuation))
x.append(i)
k.append(o)
print("===========================")
print("Data Tweets Recovered")
print("===========================\n\n")
def readdict(dataurl):
print("===========================")
print("Reading Dictionary")
print("===========================")
with open(dataurl) as tsvfile:
reader = csv.reader(tsvfile, delimiter='\t')
for row in reader:
i = []
i.append(row[2])
i.append(row[5])
y.append(i)
print("===========================")
print("Dictionary Preparation Done")
print("===========================\n\n")
addpolarity()
def addpolarity():
start_time = time.time()
counter = 0
print("===========================")
print("Processing please wait...")
print("===========================\n\n")
for j in x:
tweet_token = j
token = word_tokenize(tweet_token)
sumnum = 0
sum_word = 0
for t in token:
for d in y:
if t == d[0]:
sentiment = d[1]
if sentiment == "positive":
sumnum += 1
sum_word += 1
elif sentiment == "negative":
sumnum += -1
sum_word += 1
else:
sumnum += 0
sum_word += 1
break
if sum_word != 0.0:
sum_more = sumnum / sum_word
if sum_more >= 0.2:
sum_more = 1
elif (sum_more < 0.2) and (sum_more > -0.5):
sum_more = 0
elif sum_more <= -0.5:
sum_more = -1
else:
print("****")
sum_var = []
varid = k[counter]
sum_var.append(varid)
sum_var.append(sum_more)
some_milby.append(sum_var)
counter += 1
print("Processing time: ", round((time.time() - start_time),8), "Seconds \n\n")
time.sleep(3)
print("===========================")
print("Processing Finish")
print("===========================")
savetoxlsx()
def savetoxlsx():
df = pd.DataFrame(some_milby)
df.to_excel('processed_data/output.xlsx', header=("id","sentiment"), index=False)
#file = open("testfile_data.txt","w")
#file.write(some_milby)
#file.close()
print("===========================")
print("Data Saved!")
print("===========================")
def runall():
getdata('data/tweetdata.txt')
readdict('data/dictionary.tsv')
runall()