#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
string posting_list[6][10]={
{"my","dog","has","flea","problems","help","please","null"},
{"maybe","not","take","him","to","dog","park","stupid","null"},
{"my","dalmation","is","so","cute","I","love","him","null"},
{"stop","posting","stupid","worthless","garbage","null"},
{"mr","licks","ate","my","steak","how","to","stop","him","null"},
{"quit","buying","worthless","dog","food","stupid","null"}
};
int class_vec[6] = {0,1,0,1,0,1}; //1 , 0
class NaiveBayes
{
private:
vector > list_of_posts;
vector list_classes;
map my_vocab_list;
int *return_vec;
vector > train_mat;
vector p0vect;
vector p1vect;
float p_abusive;
public:
NaiveBayes()
{
vector vec;
for(int i=0;i > :: iterator it = list_of_posts.begin();
int index = 1;
while( it!=list_of_posts.end() )
{
//vector vec( *it.begin(),*it.end() );
vector vec = *it;
vector :: iterator tmp_it = vec.begin();
while( tmp_it!=vec.end() )
{
//cout vec( return_vec , return_vec + my_vocab_list.size()+1 );
train_mat.push_back(vec);
delete []return_vec;
}
}//get train matrix
void print()
{
cout > :: iterator it = train_mat.begin();
while(it!=train_mat.end())
{
vector vec = *it;
vector :: iterator itt = vec.begin();
while( itt!=vec.end())
{
cout p0vect(train_mat[0].size(),1); //the frequency of each word in
non-absusive docs
p0vect.resize(train_mat[0].size(),1);
//vector p1vect(train_mat[0].size(),1); //the frequency of each word in
abusive docs
p1vect.resize(train_mat[0].size(),1);
printf("p0num.size() = %d , p1num.size() = %d\n",p0vect.size(),p1vect.size());
float p0Denom = 2.0; //the total number of words in non-abusive docs
float p1Denom = 2.0; //the total number of words in abusive docs
/* calculate the p0num,p1num,p0Denom,p1Denom */
for(int i=0;ip0 )
{
return 1;
}
else
{
return 0;
}
}
};
int main()
{
NaiveBayes nb;
nb.create_vocab_list();
//nb.set_of_words_to_vec(5);
nb.get_train_matrix();
nb.print();
nb.train_NB0();
string doc1_to_classify[] = {"love","my","dalmation","null"};
string doc2_to_classify[] = {"stupid","garbage","null"};
cout<<"doc1 classified as : "< cout<<"doc2 classified as : "< return 0;
}