-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleaner.py
More file actions
165 lines (150 loc) · 6.36 KB
/
cleaner.py
File metadata and controls
165 lines (150 loc) · 6.36 KB
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
import nltk
import os
import numpy as np
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
nltk.download('stopwords') #gets the stopwords
nltk.download('punkt') #gets the punctuation
nltk.download('punkt_tab') #im not sure why this is needed but my IDE won't work without it
s_words = set(stopwords.words('english'))
#GET THE INPUT
print('***Email Extraction Program***')
dataset_no = 0
while not (dataset_no == 1 or dataset_no == 2 or dataset_no == 4):
dataset_no = int(input('Select dataset (enter 1, 2, 4): '))
print('Input recieved!')
#-----------------------------------------------------------------------------------------------------
folders = ['dataset\\' + 'enron' + str(dataset_no) + '_train\\ham',
'dataset\\' + 'enron' + str(dataset_no) + '_train\\spam',
'dataset\\' + 'enron' + str(dataset_no) + '_test\\ham',
'dataset\\' + 'enron' + str(dataset_no) + '_test\\spam'
]
#-----------------------------------------------------------------------------------------------------
print('Extracting Data...')
master_matrix = []
vocabulary_set = set() #global set of vocabulary
words = [] #this is the global header row
vocab_words_hash = {} #this maps a word to its index in the global header row
vocab_size = 0 #vocab_size = number of columns
num_rows = 0 #number of rows = number of files
cutoff = 0 #catches the number of ham files
for i in range(2):
entries = os.scandir(folders[i])
for entry in entries:
num_rows += 1 #each row represents a different file
row_vocabulary = {} #local vocabulary
if entry.is_file():
open_file = open(folders[i] + '\\' + entry.name, mode='r', encoding='latin1')
fileText = open_file.read()
tokens = word_tokenize(fileText)
for item in tokens:
#set the string to all lower-case
item = item.lower()
#we check to verify if it is a valid feature
#ignore numbers, punctuation, words of length 1, and stopwords
if item.isalpha() and len(item) > 2 and item not in s_words:
if item not in row_vocabulary: #if we haven't seen the word in this email
row_vocabulary[item] = 1
else:
row_vocabulary[item] += 1
if item not in vocabulary_set: #if we have never seen the word ever
vocabulary_set.add(item)
words.append(item)
vocab_words_hash[item] = vocab_size
vocab_size += 1
master_matrix.append(row_vocabulary)
open_file.close()
if i == 0:
cutoff = num_rows
words.append('label')
print('Complete!')
#print(len(vocabulary_set))
print('Generating Training Matrices...')
BOW_train = np.zeros((num_rows, (vocab_size) + 1), dtype=int)
Bernoulli_train = np.zeros((num_rows, (vocab_size) + 1),dtype=int)
#print(BOW.shape)
#print(Bernoulli.shape)
#print(vocab_words_hash['subject'])
#now we populate the Bag of Words and Bernoulli matrices
r = 0 #iterator
for s in master_matrix:
for key, value in s.items():
column_index = vocab_words_hash[key]
BOW_train[r, column_index] += value
Bernoulli_train[r,column_index] = 1
if r < cutoff:
BOW_train[r, -1] = 0
Bernoulli_train[r, -1] = 0
else:
BOW_train[r, -1] = 1
Bernoulli_train[r, -1] = 1
r += 1
print('Complete!')
'''
verification
#print(Bernoulli[318,-1])
#print(Bernoulli[319, -1])
print(words[5])
print(vocab_words_hash['please'])
print(BOW[0,5])
print(Bernoulli[0,5])
'''
#SAVE THE TRAINING FILES
print('Saving training data...')
#----------------------------------------------------------------------------------------------------------------
np.savetxt('enron' + str(dataset_no) + '_bow_train.csv', BOW_train, delimiter=',', header=','.join(words), fmt= "%d")
np.savetxt('enron' + str(dataset_no) + '_bernoulli_train.csv', Bernoulli_train, delimiter=',', header=','.join(words), fmt= "%d")
#----------------------------------------------------------------------------------------------------------------
print('Complete!')
#i need to calculate the number of rows in the test data
#there is a more efficient way to do this, but I don't care
#it works
test_rows = 0
for i in range(2,4):
f = os.listdir(folders[i])
test_rows += len(f)
#print(test_rows)
print('Generating the testing Matrices...')
BOW_test = np.zeros((test_rows, vocab_size + 1), dtype=int)
Bernoulli_test = np.zeros((test_rows, vocab_size + 1), dtype=int)
#now for the test Set
r = 0
for i in range(2,4):
entries = os.scandir(folders[i])
for entry in entries:
#print(entry.name)
if entry.is_file():
open_file = open(folders[i] + '\\' + entry.name, mode='r', encoding='latin1')
#print("decoding r#", r)
fileText = open_file.read()
tokens = word_tokenize(fileText)
open_file.close()
for item in tokens:
item = item.lower()
if item in vocabulary_set:
#print(item)
BOW_test[r,vocab_words_hash[item]] += 1
Bernoulli_test[r, vocab_words_hash[item]] = 1
if i == 2:
BOW_test[r, -1] = 0 #setting the class label
Bernoulli_test[r, -1] = 0
else:
BOW_test[r, -1] = 1 #setting the class label
Bernoulli_test[r, -1] = 1
open_file.close()
r += 1
'''
debugging
print(vocab_words_hash['hou'])
print(BOW_test[0,vocab_words_hash['hou']])
print(Bernoulli_test[0,vocab_words_hash['hou']])
'''
print('Complete!')
#SAVE THE TEST FILES
print('Saving Training Data...')
#--------------------------------------------------------------------------------------------------------------
np.savetxt('enron' + str(dataset_no) + '_bernoulli_test.csv', Bernoulli_test, delimiter=',', header=','.join(words), fmt= "%d")
np.savetxt('enron' + str(dataset_no) + '_bow_test.csv', BOW_test, delimiter=',', header=','.join(words), fmt= "%d")
#--------------------------------------------------------------------------------------------------------------
print('Complete!')
print('Have a nice Day! :)')