#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#include<math.h>
#include<dirent.h>
#include<sys/types.h>
#define TYPE struct words
#define LEN sizeof(struct words)
TYPE *create_list(FILE *, char[]);
void print_list(TYPE *);
void classification(TYPE *, TYPE *);
struct words{
char word[5000]; //the word name
int count; //the amount of this word appears in this file
char classify[10]; //the class of this word
double pHam; //possibility of class ham for this word
double pSpam; //possibility of class spam
struct words *next; //point to the next word
};
//int TotCount = 0;
//int DifCount = 1;
int numSpamWords = 0; //all distinct words of class spam
int numHamWords = 0; //all distinct words of class ham
char finalClass[5]; //final class of test file
int numHamFile = 0; //the number of ham files;
int numSpamFile = 0; //the number of spam files
int main(int argc, char *argv[ ])
{
TYPE *head=NULL,*temp, *last, *head1;
FILE *fp, *fp1;
DIR *dirptr = NULL;
int count = 0; //count that the amount of files
struct dirent *entry;
char trainDir[300], testFile[300], trainFile[300];
strcpy(trainDir,argv[1]);
strcpy(testFile,argv[2]);
if((dirptr=opendir(trainDir))==NULL)
{
printf("There are no this dir");
return 1;
}
else
{
while((entry=readdir(dirptr))!=NULL)
{
if((entry->d_type)==8)
{
strcpy(trainFile, trainDir);
strcat(trainFile, "/");
strcat(trainFile,entry->d_name);
if((fp = fopen(trainFile,"r"))==NULL)
{
printf("Can't open this file\n");
exit (1);
}
if(strstr(entry->d_name, "ham"))
{
temp=create_list(fp,"ham");
numHamFile++;
}
if(strstr(entry->d_name, "spam"))
{
temp=create_list(fp,"spam");
numSpamFile++;
}
fclose(fp);
count++;
if(count==1)
head=temp;
else
last->next=temp;
while((temp->next)!=NULL)
temp=temp->next;
last=temp;
}
}
closedir(dirptr);
}
if((fp1=fopen(testFile,"r"))==NULL)
{
printf("Can not open this test file\n");
exit (1);
}
head1=create_list(fp1,"unknow");
fclose(fp1);
//print_list(head4);
classification(head,head1); //classify the test file
printf("%s\n",finalClass);
}
TYPE *create_list(FILE *fp, char classify[10])
{
TYPE *in,*last,*head=NULL,*temp;
char ch;
char chr[5000];
int n = 0;
/*int TotCount = 0;
int DifCount = 1;*/
ch = fgetc(fp); //get the first char in the file
while(ch==' ')
ch = fgetc(fp);
while(ch!=EOF)
{
//if the 'ch' is ' ',jump it, if the 'ch' is EOF, exit this loop
int i = 0;
if(ch == EOF)
break;
//create a node
if((in = malloc(LEN))==NULL)
{
printf("out of memory!\n");
exit(0);
}
//get words from file
while(((ch>='a')&&(ch<='z'))||((ch>='A')&&(ch<='Z'))||(ch=='-') )
//while((ch!=' ')&&(ch!=EOF)&&(ch!='\0'))
{
chr[i]=ch;
i++;
ch=fgetc(fp);
}
ch=fgetc(fp);
while(ch==' ')
ch = fgetc(fp);
chr[i]='\0';
/*TotCount++;*/
n++;
//put information to the node
strcpy(in->word,chr);
in->count=1;
in->next=NULL;
strcpy(in->classify,classify);
if(strcmp(in->word,"\0"))
{
//create the head node in this list
if(n == 1)
{
head = in;
last = in;
}
else
{
temp=head;
while((temp!=NULL)&&strcmp(in->word,temp->word))
temp=temp->next;
if(temp==NULL)
{
last->next=in;
last=in;
//DifCount++;
}
else
temp->count=temp->count+1;
}
//add the number of spam words or ham
if(classify=="ham")
numHamWords++;
else if(classify=="spam")
numSpamWords++;
}
}
return head;
}
void print_list(TYPE *head)
{
while(head!=NULL)
{
printf("%s %d %s\n",head->word,head->count,head->classify);
head = head->next;
}
}
//the function is for classify each word in the test file, the first parameter is the head point of training
//list and the second parameter is the head point of test list
void classification(TYPE *head1, TYPE *head2)
{
TYPE *p1,*p2;
int nHam,nSpam; //count the number of one word which is classified by ham or spam
double pHamFile=0,pSpamFile=0;//count the possibility of this file to be ham or spam
double pHamWords=0,pSpamWords=0; //the sum numbers of all ham or spam words in testing file
p1 = head1;
p2 = head2;
while(p2!=NULL)
{
nHam=0;
nSpam=0;
while(p1!=NULL)
{
if(!strcmp(p1->word,p2->word)&&!strcmp(p1->classify,"ham"))
nHam+=p1->count;
else if(!strcmp(p1->word,p2->word)&&!strcmp(p1->classify,"spam"))
nSpam+=p1->count;
p1=p1->next;
}
p1 = head1;
p2->pHam=(double)(nHam+1)/(numHamWords+numHamWords+numSpamWords);
p2->pSpam=(double)(nSpam+1)/(numSpamWords+numHamWords+numSpamWords);
p2=p2->next;
}
//count the class of this file
while(head2!=NULL)
{
pHamWords+=log10(head2->pHam)*head2->count;
pSpamWords+=log10(head2->pSpam)*head2->count;
head2=head2->next;
}
pHamFile = log10((double)numHamFile/(numHamFile+numSpamFile))+pHamWords;
pSpamFile = log10((double)numSpamFile/(numHamFile+numSpamFile))+pSpamWords;
if(pHamFile>pSpamFile)
strcpy(finalClass,"ham");
if(pHamFile<pSpamFile)
strcpy(finalClass,"spam");
if(pHamFile==pSpamFile)
strcpy(finalClass,"unkown");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/123282.html
標籤:VCL組件使用和開發
上一篇:逆濾波進行影像復原后出現了白色斑點,用什么方法可以去掉呢?
下一篇:大神求解
