-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclassifier.cpp
More file actions
86 lines (77 loc) · 1.9 KB
/
classifier.cpp
File metadata and controls
86 lines (77 loc) · 1.9 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
//Spam Filter
//Team default string
#include "classifier.h"
#include "word.h"
using namespace std;
//Constructor, needs to open both files, needs to init dicitionary with each unique word, needs to count the number of words in the spam/ham files
Classifier::Classifier(string ham, string spam)
{
sin.open(spam.c_str(), ios::in);
hin.open(ham.c_str(), ios::in);
total_spam = total_ham = 0;
count_each_word();
}
//iterate trough dictionary, counting the number of times each word appears in spam/ham
void Classifier::count_each_word()
{
string input;
Word *pusher;
while(sin >> input){
if(!dictionary.empty()){
pusher=dictionary[0];
for(unsigned int i=0; i<dictionary.size(); i++){
if(pusher->return_name()==input){
pusher->increment_spam();
break;
}
}
if(pusher->return_name()==input) continue;
}
pusher = new Word(input);
dictionary.push_back(pusher);
pusher->increment_spam();
total_spam++;
}
while(hin >> input){
pusher=dictionary[0];
for(unsigned int i=0; i<dictionary.size(); i++){
if(pusher->return_name()==input){
pusher->increment_ham();
break;
}
}
if(pusher->return_name()==input) continue;
pusher = new Word(input);
dictionary.push_back(pusher);
pusher->increment_ham();
total_ham++;
}
}
//finds the number of times target appears in ham
int Classifier::lookup_ham(string target)
{
for(unsigned int i=0; i<dictionary.size(); i++)
if(dictionary[i]->return_name()==target)
return dictionary[i]->return_ham();
return 0;
}
//finds the number of times target appears in spam
int Classifier::lookup_spam(string target)
{
for(unsigned int i=0; i<dictionary.size(); i++)
if(dictionary[i]->return_name()==target)
return dictionary[i]->return_spam();
return 0;
}
int Classifier::return_total()
{
return dictionary.size();
}
int Classifier::return_total_ham()
{
return total_ham;
}
int Classifier::return_total_spam()
{
return total_spam;
}