-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
170 lines (146 loc) · 5.33 KB
/
main.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
import os
import knn
import mnb
import knn_classify
import mnb_classify
import extract as ex
from mail import Mail
from pprint import pprint
from getpass import getpass
if __name__ == '__main__':
current_path = os.path.dirname(os.path.abspath(__file__))
choice = 0
while choice != 5:
print('\nMenu:')
print('1 -> Create Dataset.\n2 -> Classify using kNN.\n3 -> Classify using MNB.\n4 -> Exit.')
choice = int(input('Enter your choice: '))
if choice == 1:
# Create Dataset
reply = input('Do you want to get emails from your email account? (y/n): ')[0].lower()
if reply == 'n':
dataset_name = input('Enter the name of existing dataset folder: ')
ex.main(dataset_name)
elif reply == 'y':
usr = input('Email: ')
pwd = getpass('Password: ')
e = Mail()
success = e.login(usr, pwd)
if success:
dataset_name = input('Enter a name for your dataset: ')
dataset_path = os.path.join(current_path, dataset_name)
if not os.path.exists(dataset_path):
os.mkdir(dataset_path)
print('Your Folders:')
pprint(e.list())
folder_names = []
print('Enter folder names you want in your dataset: (New line when done)')
while True:
name = input()
if name == '':
if len(folder_names) < 2:
print('Enter atleast two Folders!')
continue
else:
break
success = e.select(name)
if success:
folder_names.append(name)
for label in folder_names:
print('Fetching mails from ' + label + '...')
e.fetch_mailbox(dataset_path, label)
e.logout()
ex.main(dataset_name)
else:
print('Invalid Input.')
elif choice == 2:
# Classify using kNN
choice2 = 0
while choice2 != 4:
print('\nkNN Classification:')
print('1 -> Classify Dataset. (Splits Dataset into training and test set)')
print('2 -> Classify Testset. (Test kNN accuracy using Dataset as training set)')
print('3 -> Classify New Emails.\n4 -> Back.')
choice2 = int(input('Enter your choice: '))
if choice2 == 1:
# Classify Dataset
dataset_name = input('Enter the name of dataset folder to be classified: ')
knn.main(dataset_name)
elif choice2 == 2:
# Classify Testset
dataset_name = input('Enter the name of dataset folder to be used as training set: ')
testset_name = input('Enter the name of test set folder: ')
knn_classify.main(dataset_name, testset_name)
elif choice2 == 3:
# Classify New Emails
dataset_name = input('Enter the name of dataset folder to be used as training set: ')
reply = input('Do you want get new unread emails from your email account? (y/n): ')[0].lower()
if reply == 'y':
usr = input('Email: ')
pwd = getpass('Password: ')
e = Mail()
success = e.login(usr, pwd)
if success:
new_emails_name = input('Enter a folder name to store new emails: ')
new_emails_path = current_path + new_emails_name + "\\"
if not os.path.exists(new_emails_path):
os.mkdir(new_emails_path)
msg_uids = e.fetch_unread(new_emails_path)
knn_classify.main(dataset_name, new_emails_name, new_emails = True)
elif reply == 'n':
new_emails_name = input('Enter the name of new emails folder: ')
knn_classify.main(dataset_name, new_emails_name, new_emails = True)
else:
print('Invalid Input.')
elif choice2 == 4:
break
else:
print('Invalid choice.')
elif choice == 3:
# Classify using MNB
choice2 = 0
while choice2 != 4:
print('\nMNB Classification:')
print('1 -> Classify Dataset. (Splits Dataset into training and test set)')
print('2 -> Classify Testset. (Test MNB accuracy using Dataset as training set)')
print('3 -> Classify New Emails.\n4 -> Back.')
choice2 = int(input('Enter your choice: '))
if choice2 == 1:
# Classify Dataset
dataset_name = input('Enter the name of dataset folder to be classified: ')
mnb.main(dataset_name)
elif choice2 == 2:
# Classify Testset
dataset_name = input('Enter the name of dataset folder to be used as training set: ')
testset_name = input('Enter the name of test set folder: ')
mnb_classify.main(dataset_name, testset_name)
elif choice2 == 3:
# Classify New Emails
dataset_name = input('Enter the name of dataset folder to be used as training set: ')
reply = input('Do you want get new unread emails from your email account? (y/n): ')[0].lower()
if reply == 'y':
usr = input('Email: ')
pwd = getpass('Password: ')
e = Mail()
success = e.login(usr, pwd)
if success:
new_emails_name = input('Enter a folder name to store new emails: ')
new_emails_path = current_path + new_emails_name + "\\"
if not os.path.exists(new_emails_path):
os.mkdir(new_emails_path)
msg_uids = e.fetch_unread(new_emails_path)
predictions = mnb_classify.main(dataset_name, new_emails_name, new_emails = True)
e.assign_label(msg_uids, predictions)
elif reply == 'n':
new_emails_name = input('Enter the name of new emails folder: ')
mnb_classify.main(dataset_name, new_emails_name, new_emails = True)
else:
print('Invalid Input.')
elif choice2 == 4:
break
else:
print('Invalid choice.')
elif choice == 4:
# Exit
exit(0)
else:
print('Invalid Choice.')