-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_processing.py
332 lines (199 loc) · 7.16 KB
/
data_processing.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import pandas as pd
import matplotlib.pyplot as plt
import subprocess
import os
from nltk import pos_tag
from collections import Counter
import operator
import math
import random
from nltk.corpus import stopwords
import time
def refine_raw_data(filename):
filename_out = filename.strip('.rawtext')+str('.txt')
data = open(filename, 'r')
data = data.read()
data = ' '.join(data.split())
stopWords = set(stopwords.words('english'))
filtered = ''
sentence = ''
for x in data:
if x != '.':
sentence = sentence + x
if x == '.':
filtered = filtered + check(sentence,stopWords) + '\n'
sentence = ''
data_out = open(filename_out, 'w+')
data_out.write(filtered)
data_out.close()
print(filtered)
def check(sentence,stopWords):
temp = ''
for x in sentence.split():
if x in stopWords:
pass
if x not in stopWords:
temp = temp + str(x) + ' '
return temp
def data_mining(filename):
filename_out = filename.strip('.txt') + str('.out')
command = ["./apriori", '-n1', filename, filename_out]
subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
time.sleep(1)
nano_filter_function(filename_out)
def nano_filter_function(filename_out):
f_nano = open(filename_out, 'r')
temp = f_nano.read()
temp = temp.split('\n')
for x in temp:
write_data(x)
# nano_filter = ''
# for x in range(len(temp)):
# if x % 2 == 0:
# if temp[x] in ['The', 'the', '"', '"The'] or temp[x].endswith("'s") or temp[x].isalpha() ==False:
# pass
# else:
# nano_filter += temp[x]
# nano_filter += ' '
# nano_filter += temp[x + 1] + '\n'
f_nano.close()
#write_data(nano_filter)
def write_data(data):
grand = open(grand_out, 'a+')
grand.write(str(data) + '\n')
grand.close()
def mined_words(file):
f = open(file, 'r')
temp = f.read()
temp = temp.split()
words = []
magnitudes = []
for x in range(len(temp)):
if x % 2 == 0:
words.append(temp[x])
if x % 2 != 0:
magnitudes.append(float(temp[x].strip('()')))
return words, magnitudes
def filter_words(words):
temp = pos_tag(words, tagset='universal')
pos_filter = ['ADJ', 'NOUN']
words_filtered = []
for i in range(len(temp)):
if temp[i][1] in pos_filter:
words_filtered.append(temp[i][0].strip('"'))
else:
pass
return words_filtered
def words_in_files(filename, words_filtered):
o = open(filename, 'r')
total = o.read()
total = total.split()
freq = []
for x in total:
if x in words_filtered:
freq.append(x)
return tuple(freq)
def words_count(freq):
frequency = dict(Counter(freq))
return frequency
def strip_data(frequency):
words2process = round(len(frequency) * 70 / 100)
i = 0
plot_words_in_occurrence = {}
plot_words_in_support = {}
for key, value in frequency.items():
if i >= words2process:
plot_words_in_occurrence[key] = value
plot_words_in_support[key] = float()
i += 1
plot_words_in_support = process_strip_data(plot_words_in_support)
final_dict = get_percent(plot_words_in_support,plot_words_in_occurrence)
return final_dict
def process_strip_data(plot_words_in_support):
f = open(grand_out, 'r')
temp = f.read()
temp = temp.split()
for x in range(len(temp)):
if x % 2 == 0:
if temp[x] in plot_words_in_support.keys():
catch = plot_words_in_support[temp[x]] + float(temp[x + 1].strip('()'))
plot_words_in_support[temp[x]] = catch
return plot_words_in_support
def get_percent(plot_words_in_support,plot_words_in_occurrence):
final_dict = {}
for key, value in plot_words_in_support.items():
final_dict.update({key: value * 70 / 100})
for key, value in final_dict.items():
if key in plot_words_in_occurrence:
temp = (plot_words_in_occurrence[key] * 30 / 100) + value
final_dict[key] = round(temp)
final_dict = dict(sorted(final_dict.items(), key=operator.itemgetter(1)))
return final_dict
def get_coordinates(final_dict):
text = []
area = []
for key, value in final_dict.items():
text.append(key)
area.append((2 * math.pi * value) * 25)
y = []
for key, value in final_dict.items():
y.append(value)
x = random.sample(range(len(y)), len(text))
return text, area, x , y
def basic_bubble(x,y,text,area):
grand_png = grand_out.strip('.out')+str('.png')
for i in range(len(text)):
#print(text[i])
plt.text(x[i], y[i], str(text[i]), horizontalalignment='center')
# making the scatter plot
plt.scatter(x, y, s=area, linewidths=2, edgecolor='w')
# axis([0,11,200,1280])
plt.title("WORDS USED IN TODAY'S NEWS")
plt.xlabel('Number Of Bubbles')
plt.ylabel('Magnitude')
plt.savefig(grand_png, format='png', dpi=1000)
#plt.yticks([])
#plt.xticks([])
plt.show()
plt.clf()
if __name__ == "__main__":
directory = "/home/agha/Sir Naeem/"
############################### Grand OutPut File ###################################
global grand_out
grand_out = 'grand_data.out'
############################## IF GRANDE OUTPUT EXSIST ###############################
if grand_out in os.listdir(directory):
os.remove(grand_out)
################################## Data Refine #######################################
for filename in os.listdir(directory):
if filename.endswith(".rawtext"):
refine_raw_data(filename)
continue
else:
continue
################################## Data Mining #######################################
for filename in os.listdir(directory):
if filename.endswith(".txt"):
data_mining(filename)
continue
else:
continue
########################## Getting Words and their Magnitudes ##########################
words, magnitudes = mined_words(grand_out)
words_filtered = filter_words(words)
################### Checking number of Filtered Words in News Files ####################
freq = ()
for filename in os.listdir(directory):
if filename.endswith(".txt"):
temp = ()
temp = words_in_files(filename,words_filtered)
freq = freq + temp
#################### Getting Count per Filtered Word as a Dictionary ####################
frequency = words_count(freq)
frequency = dict(sorted(frequency.items(), key=operator.itemgetter(1))) #Sorting
#################### Getting Words in occurrence & Support Separate #######################
final_dict = strip_data(frequency)
####################### Get PLotting Coordinates ###################################
text , area, x ,y= get_coordinates(final_dict)
######################### Plotting the Coordinates ###########################
basic_bubble(x,y,text,area)