-
Notifications
You must be signed in to change notification settings - Fork 7
/
extract.py
667 lines (584 loc) · 18.5 KB
/
extract.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
import sys
import re
import os
import csv
import nltk
import string
from os import listdir
from itertools import product
from os.path import isfile, join
from stemming.porter2 import stem
from nltk.corpus import wordnet
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
from sklearn.feature_extraction.text import TfidfVectorizer
def tokenize(text):
'''Tokenize and Stem words for tf-idf'''
tokens = nltk.word_tokenize(text)
stems = []
for item in tokens:
stems.append(PorterStemmer().stem(item))
return stems
def get_token_dict(folder_path, folder_names):
'''Create token dictionary for tf-idf'''
token_dict = {}
for var in range(len(folder_names)):
path = folder_path + folder_names[var] + "\\"
for dirpath, dirs, files in os.walk(path):
for f in files:
fname = os.path.join(dirpath, f)
with open(fname) as pearl:
text = pearl.read()
token_dict[f] = text.lower().translate(string.punctuation)
return token_dict
def collectFeatures():
'''Read the email and collect features like words, bigrams, trigrams'''
w = []
subd, wordsd = {}, {}
digramsd={}
trigramsd={}
numbers=[]
dollars=[]
urls=[]
emails=[]
count_number=[]
count_dollar=[]
count_mail=[]
count_url=[]
stop=stopwords.words("english")
counter=0
# for each folder/category
for loop_var in range(len(folder_names)):
mypath = join(folder_path, folder_names[loop_var])
os.chdir(mypath)
# fp = open(current_path + 'features_' + str(loop_var) + '.txt', 'w') # write features into features.txt file
# for each email
for fo in listdir(mypath):
if isfile(join(mypath,fo)):
count_number.append(0)
f=open(fo,"r")
fr=f.read()
subl=[]
# read and collect features in subject line
match=re.search('subject:(.+)\n',fr.lower())
if match:
subl=subl+[w for w in re.split('\W',match.group(1)) if w]
for i in subl:
if i.isdigit():
count_number[counter]+=1
for i in subl:
if i in stop:
subl.remove(i)
for i,j in zip(subl,subl[1:]):
i=stem(i)
j=stem(j)
if i+" "+j in digramsd:
digramsd[i+" "+j]+=1
else:
digramsd[i+" "+j]=1
for i,j,k in zip(subl,subl[1:],subl[2:]):
i=stem(i)
j=stem(j)
k=stem(k)
if i+" "+j+" "+k in trigramsd:
trigramsd[i+" "+j+" "+k]+=1
else:
trigramsd[i+" "+j+" "+k]=1
for elements in subl:
element=stem(elements)
if element in subd:
subd[element]+=1
else:
subd[element]=1
f.close()
# read and collect features in email content
f=open(fo,"rU")
flag=0
wordsl=[]
for line in f:
if flag==0 and not re.search(r'x-filename',line.lower()):
continue
elif flag==0 and re.search(r'x-filename',line.lower()):
flag=1
continue
elif not ( re.search(r'forwarded by',line.lower()) or re.search(r'original message',line.lower()) ):
wordsl=wordsl+[w for w in re.split('\W',line.lower()) if w]
elif re.search(r'forwarded by',line.lower()) or re.search(r'original message',line.lower()):
break
for i in wordsl:
if i.isdigit():
count_number[counter]+=1
for i in wordsl:
if i in stop:
wordsl.remove(i)
for i,j in zip(wordsl,wordsl[1:]):
i=stem(i)
j=stem(j)
if i+" "+j in digramsd:
digramsd[i+" "+j]+=1
else:
digramsd[i+" "+j]=1
for i,j,k in zip(wordsl,wordsl[1:],wordsl[2:]):
i=stem(i)
j=stem(j)
k=stem(k)
if i+" "+j+" "+k in trigramsd:
trigramsd[i+" "+j+" "+k]+=1
else:
trigramsd[i+" "+j+" "+k]=1
for elements in wordsl:
element=stem(elements)
if element in wordsd:
wordsd[element]+=1
else:
wordsd[element]=1
f.close()
# collect features like email-ids and urls
f=open(fo,"rU")
count_mail.append(0)
count_url.append(0)
emails=[]
urls=[]
for line in f:
if not ( re.search(r'forwarded by',line.lower()) or re.search(r'original message',line.lower()) ):
emails=[]
urls=[]
emails = emails + re.findall(r'\w+[.|\w]\w+@\w+[.]\w+[.|\w+]\w+',line)
urls = urls + re.findall(r'www.',line)
for email in emails:
count_mail[counter]+=1
for url in urls:
count_url[counter]+=1
else:
break
counter+=1
f.close()
# write into features.txt
# for p in subd.items():
# fp.write(f.name+":%s:%s\n" % p)
# for p in wordsd.items():
# fp.write(f.name+":%s:%s\n" % p)
return subd, wordsd, digramsd, trigramsd, count_number, count_mail, count_url
def makefiles(workfilename, wordfilename):
'''Write collected features into workfiles with names workfilename and wordfilename'''
w = []
fw = open(folder_path + workfilename, 'w') # open workfile
words_file = open(folder_path + wordfilename, 'w') # open wordfile
counter=0
stop=stopwords.words("english")
# Perform tf-idf
token_dict = get_token_dict(folder_path, folder_names)
tfidf = TfidfVectorizer(tokenizer=tokenize, stop_words='english')
tfs = tfidf.fit_transform(token_dict.values())
# for each folder/category
for loop_var in range(len(folder_names)):
mypath = folder_path + folder_names[loop_var]
os.chdir(mypath)
# write all the feature words into wordfile
if loop_var == 0:
for elements in wordsd:
if wordsd[elements] > cut_off:
words_file.write(elements+',')
words_file.write('`~1,')
for elements in subd:
if subd[elements] > cut_off:
words_file.write(elements+',')
words_file.write('`~2,')
for elements in digramsd:
if digramsd[elements] > 1:
words_file.write(elements+',')
words_file.write('`~3,')
# to avoid last extra comma ','
first = True
for elements in trigramsd:
if trigramsd[elements] > 1:
if first:
words_file.write(elements)
first = False
else:
words_file.write(','+elements)
words_file.write('\n')
words_file.close()
# for each email
for fo in listdir(mypath):
if isfile(join(mypath,fo)):
subd_temp={}
wordsd_temp={}
digramsd_temp={}
trigramsd_temp={}
f=open(fo,"rU")
fr=f.read()
fr=fr.lower()
subl=[]
response = tfidf.transform([fr])
feature_names = tfidf.get_feature_names()
# read and collect features in subject line
match=re.search('subject:(.+)\n',fr)
if match:
subl=subl+[w for w in re.split('\W',match.group(1)) if w]
for i in subl:
if i in stop:
subl.remove(i)
for i,j in zip(subl,subl[1:]):
i=stem(i)
j=stem(j)
if i+" "+j in digramsd_temp:
digramsd_temp[i+" "+j]+=1
else:
digramsd_temp[i+" "+j]=1
for i,j,k in zip(subl,subl[1:],subl[2:]):
i=stem(i)
j=stem(j)
k=stem(k)
if i+" "+j+" "+k in trigramsd_temp:
trigramsd_temp[i+" "+j+" "+k]+=1
else:
trigramsd_temp[i+" "+j+" "+k]=1
for elements in subl:
element=stem(elements)
if element in subd:
if element in subd_temp:
subd_temp[element]+=1
else:
subd_temp[element]=1
# read and collect features in email content
f.close()
f=open(fo,"rU")
flag=0
wordsl=[]
for line in f:
if flag==0 and not re.search(r'x-filename',line.lower()):
continue
elif flag==0 and re.search(r'x-filename',line.lower()):
flag=1
continue
elif not ( re.search(r'forwarded by',line.lower()) or re.search(r'original message',line.lower()) ):
wordsl=wordsl+[w for w in re.split('\W',line.lower()) if w]
elif re.search(r'forwarded by',line.lower()) or re.search(r'original message',line.lower()):
break
for i in wordsl:
if i in stop:
wordsl.remove(i)
for i,j in zip(wordsl,wordsl[1:]):
i=stem(i)
j=stem(j)
if i+" "+j in digramsd_temp:
digramsd_temp[i+" "+j]+=1
else:
digramsd_temp[i+" "+j]=1
for i,j,k in zip(wordsl,wordsl[1:],wordsl[2:]):
i=stem(i)
j=stem(j)
k=stem(k)
if i+" "+j+" "+k in trigramsd_temp:
trigramsd_temp[i+" "+j+" "+k]+=1
else:
trigramsd_temp[i+" "+j+" "+k]=1
for elements in wordsl:
element=stem(elements)
if element in wordsd:
if element in wordsd_temp:
wordsd_temp[element]+=1
else:
wordsd_temp[element]=1
for word in wordsd:
if wordsd[word] > cut_off:
if word in wordsd_temp:
if word in feature_names:
fw.write(str(wordsd_temp[word]*response[0,feature_names.index(word)]))
fw.write(',')
else:
fw.write(str(wordsd_temp[word]))
fw.write(',')
else:
fw.write('0,')
for subject_word in subd:
if subd[subject_word] > cut_off:
if subject_word in subd_temp:
if subject_word in feature_names:
fw.write(str(subd_temp[subject_word]*response[0,feature_names.index(subject_word)]*subject_weight))
fw.write(',')
else:
fw.write(str(subd_temp[subject_word]*subject_weight))
fw.write(',')
else:
fw.write('0,')
for digram_word in digramsd:
if digramsd[digram_word] > 1:
if digram_word in digramsd_temp:
fw.write(str(digramsd_temp[digram_word]))
fw.write(',')
else:
fw.write('0,')
for trigram_word in trigramsd:
if trigramsd[trigram_word] > 1:
if trigram_word in trigramsd_temp:
fw.write(str(trigramsd_temp[trigram_word]))
fw.write(',')
else:
fw.write('0,')
fw.write(str(count_url[counter]))
fw.write(',')
fw.write(str(count_mail[counter]))
fw.write(',')
fw.write(str(count_number[counter]))
fw.write(',')
counter+=1
fw.write(str(loop_var) + '\n')
def loadTrainingset(current_path, workfilename, wordfilename, trainingSet):
'''Load training set and training words from specified files in the given current_path'''
# load training set
with open(current_path + workfilename, 'r') as csvfile:
lines = csv.reader(csvfile)
dataset = list(lines)
for x in range(len(dataset)):
for y in range(len(dataset[x])):
dataset[x][y] = float(dataset[x][y])
trainingSet.append(dataset[x])
csvfile.close()
# load training words
with open(current_path + wordfilename, 'r') as csvfile:
lines = csv.reader(csvfile)
dataset = list(lines)
index1 = dataset[0].index('`~1')
index2 = dataset[0].index('`~2')
index3 = dataset[0].index('`~3')
wordsd = dataset[0][:index1]
subd = dataset[0][index1+1:index2]
digramsd = dataset[0][index2+1:index3]
trigramsd = dataset[0][index3+1:]
return wordsd, subd, digramsd, trigramsd
def loadTestset(folder_path, folder_names, wordsd, subd, digramsd, trigramsd):
'''Load test set for new emails'''
w = []
testSet = []
numbers=[]
dollars=[]
urls=[]
emails=[]
count_number=[]
count_dollar=[]
count_mail=[]
count_url=[]
all_files = []
counter=0
subject_weight = 4
similarity_cutoff = 0.8
stop=stopwords.words("english")
# tf-idf
token_dict = get_token_dict(folder_path, folder_names)
tfidf = TfidfVectorizer(tokenizer=tokenize, stop_words='english')
tfs = tfidf.fit_transform(token_dict.values())
# for each folder/category (incase where new emails are a test set)
for loop_var in range(len(folder_names)):
mypath = folder_path + folder_names[loop_var]
os.chdir(mypath)
# for each email
for fo in listdir(mypath):
if isfile(join(mypath,fo)):
all_files.append(mypath + '/' + fo)
temp_list = []
subd_temp={}
wordsd_temp={}
digramsd_temp={}
trigramsd_temp={}
f=open(fo,"rU")
fr=f.read()
fr=fr.lower()
subl=[]
count_number.append(0)
response = tfidf.transform([fr])
feature_names = tfidf.get_feature_names()
# read and collect features in subject line
match=re.search('subject:(.+)\n',fr)
if match:
subl=subl+[w for w in re.split('\W',match.group(1)) if w]
for i in subl:
if i.isdigit():
count_number[counter]+=1
for i in subl:
if i in stop:
subl.remove(i)
for i,j in zip(subl,subl[1:]):
i=stem(i)
j=stem(j)
if i+" "+j in digramsd_temp:
digramsd_temp[i+" "+j]+=1
else:
digramsd_temp[i+" "+j]=1
for i,j,k in zip(subl,subl[1:],subl[2:]):
i=stem(i)
j=stem(j)
k=stem(k)
if i+" "+j+" "+k in trigramsd_temp:
trigramsd_temp[i+" "+j+" "+k]+=1
else:
trigramsd_temp[i+" "+j+" "+k]=1
for elements in subl:
element=stem(elements)
if element in subd_temp:
subd_temp[element]+=1
else:
subd_temp[element]=1
f.close()
# read and collect features in email content
f=open(fo,"rU")
flag=0
wordsl=[]
for line in f:
if flag==0 and not re.search(r'x-filename',line.lower()):
continue
elif flag==0 and re.search(r'x-filename',line.lower()):
flag=1
continue
elif not ( re.search(r'forwarded by',line.lower()) or re.search(r'original message',line.lower()) ):
wordsl=wordsl+[w for w in re.split('\W',line.lower()) if w]
elif re.search(r'forwarded by',line.lower()) or re.search(r'original message',line.lower()):
break
for i in wordsl:
if i.isdigit():
count_number[counter]+=1
for i in wordsl:
if i in stop:
wordsl.remove(i)
for i,j in zip(wordsl,wordsl[1:]):
i=stem(i)
j=stem(j)
if i+" "+j in digramsd_temp:
digramsd_temp[i+" "+j]+=1
else:
digramsd_temp[i+" "+j]=1
for i,j,k in zip(wordsl,wordsl[1:],wordsl[2:]):
i=stem(i)
j=stem(j)
k=stem(k)
if i+" "+j+" "+k in trigramsd_temp:
trigramsd_temp[i+" "+j+" "+k]+=1
else:
trigramsd_temp[i+" "+j+" "+k]=1
for elements in wordsl:
element=stem(elements)
if element in wordsd_temp:
wordsd_temp[element]+=1
else:
wordsd_temp[element]=1
f.close()
# collect additional features like email_ids and urls
f=open(fo,"rU")
count_mail.append(0)
count_url.append(0)
emails=[]
urls=[]
for line in f:
if not ( re.search(r'forwarded by',line.lower()) or re.search(r'original message',line.lower()) ):
emails=[]
urls=[]
emails = emails + re.findall(r'\w+[.|\w]\w+@\w+[.]\w+[.|\w+]\w+',line)
urls = urls + re.findall(r'www.',line)
for email in emails:
count_mail[counter]+=1
for url in urls:
count_url[counter]+=1
else:
break
f.close()
# collect features using synonyms and similarity in email content
for word in wordsd:
if word in wordsd_temp:
if word in feature_names:
temp_list.append(wordsd_temp[word]*response[0,feature_names.index(word)])
else:
temp_list.append(wordsd_temp[word])
else:
synonyms, flag = [], False
for syn in wordnet.synsets(word):
for l in syn.lemmas():
synonyms.append(l.name())
for synonym in synonyms:
if synonym in wordsd_temp:
temp_list.append(wordsd_temp[synonym])
flag = True
break
if flag == False:
syn1 = wordnet.synsets(word)
for word2 in wordsd_temp:
syn2 = wordnet.synsets(word2)
for sense1, sense2 in product(syn1, syn2):
sim = wordnet.wup_similarity(sense1, sense2)
if sim and sim >= similarity_cutoff:
temp_list.append(wordsd_temp[word2])
flag = True
break
break # Use this break to check only first Synsets
if flag == True:
break
if flag == False:
temp_list.append(0)
# collect features using synonyms and similarity in subject line
for word in subd:
if word in subd_temp:
if word in feature_names:
temp_list.append(subd_temp[word]*response[0,feature_names.index(word)]*subject_weight)
else:
temp_list.append(subd_temp[word]*subject_weight)
else:
synonyms, flag = [], False
for syn in wordnet.synsets(word):
for l in syn.lemmas():
synonyms.append(l.name())
for synonym in synonyms:
if synonym in subd_temp:
temp_list.append(subd_temp[synonym]*subject_weight)
flag = True
break
if flag == False:
syn1 = wordnet.synsets(word)
for word2 in subd_temp:
syn2 = wordnet.synsets(word2)
for sense1, sense2 in product(syn1, syn2):
sim = wordnet.wup_similarity(sense1, sense2)
if sim and sim >= similarity_cutoff:
temp_list.append(subd_temp[word2])
flag = True
break
break # Use this break to check only first Synsets
if flag == True:
break
if flag == False:
temp_list.append(0)
for digram in digramsd:
if digram in digramsd_temp:
temp_list.append(digramsd_temp[digram])
else:
temp_list.append(0)
for trigram in trigramsd:
if trigram in trigramsd_temp:
temp_list.append(trigramsd_temp[trigram])
else:
temp_list.append(0)
temp_list.append(count_url[counter])
temp_list.append(count_mail[counter])
temp_list.append(count_number[counter])
temp_list.append(loop_var)
counter += 1
testSet.append(temp_list)
return testSet, all_files
def main(dataset_name):
'''Main function to start execution of extract.py'''
global folder_names, subject_weight, cut_off, current_path, folder_path
global subd, wordsd, digramsd, trigramsd, count_number, count_mail, count_url
subject_weight = 3
cut_off = 5
current_path = os.path.dirname(os.path.abspath(__file__))
folder_path = os.path.join(current_path, dataset_name)
folder_names = next(os.walk(folder_path))[1]
if 'results' in folder_names:
folder_names.remove('results')
print('Collecting Features...')
subd, wordsd, digramsd, trigramsd, count_number, count_mail, count_url = collectFeatures()
print('Features collected successfully.')
print('Making Workfile...')
makefiles('mergedworkfile.csv', 'wordfile.csv')
print('Workfile complete.\nProgram run successful.')
if __name__ == '__main__':
main("dataset")