-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassify.cc
223 lines (194 loc) · 5.48 KB
/
classify.cc
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
#include <iostream>
#include <vector>
#include "helper.h"
using namespace std;
map<string, pair<int, int>> get_model(ifstream& spamFile, ifstream& hamFile);
vector<string> getTests(ifstream& testFile);
void classifyTests(vector<string>& messages, map<string, pair<int, int>>& model, ofstream& outputFile);
int main(int argc, char** argv)
{
string i, is, ih, o;
if (argc == 5) { // No optional arguments provided.
i = string(argv[1]); // Assume first argument is testing input.
is = string(argv[2]); // Assuming second argument is spam input.
ih = string(argv[3]); // Assume third argument is ham input.
o = string(argv[4]); // Assume fourth argument is classification output.
}
else {
string x[8];
for (int j = 1; j < 9; j++) {
x[j-1] = string(argv[j]);
}
if (x[0][0] == '-' && x[2][0] == '-' && x[4][0] == '-' && x[6][0]) {
for (int k = 0; k <= 6; k+= 2) {
if (x[k].size() == 2) {
if (x[k][1] == 'i') {
i = x[k+1];
}
else if (x[k][1] == 'o') {
o = x[k+1];
}
else {
cerr << "Unknown argument " << x[k] << endl;
return -1;
}
}
else if (x[k][1] == 'i' && x[k][2] == 's') {
is = x[k+1];
}
else if (x[k][1] == 'i' && x[k][2] == 'h') {
ih = x[k+1];
}
else {
cerr << "Unknown argument " << x[k] << endl;
return -1;
}
}
}
else {
cerr << "Incorrect argument structure" << endl;
return -1;
}
}
if (i.empty()) { cerr << "Input test argument not satisfied" << endl; return -1; }
if (is.empty()) { cerr << "Input spam argument not satisfied" << endl; return -1; }
if (ih.empty()) { cerr << "Input ham argument not satisfied" << endl; return -1; }
if (o.empty()) { cerr << "Output argument not satisfied" << endl; return -1; }
ifstream inputFile(i, ifstream::in);
ifstream spamFile(is, ifstream::in);
ifstream hamFile(ih, ifstream::in);
ofstream classifyFile(o, ofstream::out);
// Get the trained model from file.
map<string, pair<int, int>> trained_model = get_model(spamFile, hamFile); //word: <spam_count, ham_count>
// Get messages to classify.
vector<string> tests = getTests(inputFile);
// The bread and butter. Naive bayes classifier.
classifyTests(tests, trained_model, classifyFile);
return 0;
}
map<string, pair<int, int>> get_model(ifstream& spamFile, ifstream& hamFile)
{
map<string, pair<int, int>> model;
if(spamFile.good() && hamFile.good()) {
string line, word, num;
int index, count;
while(getline(spamFile, line)) {
// Parse word
index=0;
while(line[index] != ' ') {
word += line[index];
index++;
}
// Parse spam count
++index;
while(index < line.size()) {
num += line[index];
index++;
}
// Convert spam count to int
count = stoi(num, nullptr, 10);
// Update our model.
if (model.count(word) == 0) {
model.insert(pair<string, pair<int, int>>(word, make_pair(count, 0)));
}
else {
get<0>(model.find(word)->second) += count;
}
word.clear();
num.clear();
count = 0;
}
while(getline(hamFile, line))
{
// Parse word
index=0;
while(line[index] != ' ') {
word += line[index];
index++;
}
// Parse spam count
++index;
while(index < line.size()) {
num += line[index];
index++;
}
// Convert spam count to int
count = stoi(num, nullptr, 10);
// Update our model.
if (model.count(word) == 0) {
model.insert(pair<string, pair<int, int>>(word, make_pair(0, count)));
}
else {
get<1>(model.find(word)->second) += count;
}
word.clear();
num.clear();
count = 0;
}
}
spamFile.close();
hamFile.close();
return model;
}
vector<string> getTests(ifstream& testFile)
{
string message;
vector<string> messages;
if (testFile.good()) {
while(getline(testFile, message)) {
messages.push_back(message);
}
}
testFile.close();
return messages;
}
void classifyTests(vector<string>& messages, map<string, pair<int, int>>& model, ofstream& outputFile)
{
// Get the total ammount of spam/ham classifcations from our trained model.
double spamTotal=0, hamTotal=0;
for(auto it = model.begin(); it != model.end(); it++) {
spamTotal += get<0>(it->second);
hamTotal += get<1>(it->second);
}
vector<double> spamProb;
vector<double> hamProb;
// Probability
double spam, ham, spam_count, ham_count;
string word, eval;
for (auto& message : messages)
{
// Clean Message
eval.assign(message);
cleanLine(eval);
toLower(eval);
// Evaluate Message
for (auto& c : eval) {
if (c != ' ') word += c;
else if(model.count(word)) {
// Get number of times a word was marked for spam/ham
spam_count = get<0>(model.find(word)->second);
ham_count = get<1>(model.find(word)->second);
// Calculate probability that word is spam and ham
spamProb.push_back( spam_count / (spam_count + ham_count) );
hamProb.push_back( ham_count / (spam_count + ham_count) );
word.clear();
}
else word.clear();
}
// Prior Probability
spam = spamTotal / (spamTotal + hamTotal);
ham = hamTotal / (hamTotal + spamTotal);
// Calculate probability scores the message being spam or ham.
for(int index = 0; index < spamProb.size(); ++index) {
if (spamProb[index] != 0) spam *= spamProb[index];
if (hamProb[index] != 0) ham *= hamProb[index];
}
// Output to file
if (ham >= spam) outputFile << "ham" "\n"; // We predict ham!
else outputFile << "spam" << "\n"; // We predict spam!
eval.clear();
spamProb.clear();
hamProb.clear();
}
outputFile.close();
}