基于朴素贝叶斯分类器的文本分类算法(C语言)

#include 
#include 
#include  //_getcwd(), _chdir()
#include  //_MAX_PATH, system()
#include  //_finddata_t, _findfirst(), _findnext(), _findclose()


char vocabulary[1000][20];




//@输入参数:要分类的文本
//@输出参数:该文本中总单词数


int SplitToWord(char text[])
{
int i=0;
char seps[]=", .\n";  
char *substring; 


substring=strtok(text,seps); 
while(substring!=NULL) 
{   
   strcpy(vocabulary[i],substring);//将单词存储到vocabulary数组中 
   substring=strtok(NULL,seps); 
   i++;
}
return i; //返回一共多少个单词
}




//@输入参数:无
//@输出参数:该目录下.txt文件数


int CountDirectory()
{
int count=0; //txt文件计数器
long hFile;
    _finddata_t fileinfo;


    if ((hFile=_findfirst("*.txt",&fileinfo))!=-1L)
    {
        do
        {            
    count++;
        } while (_findnext(hFile,&fileinfo) == 0);
}
return count;
}




//@输入参数:分类文本中单词数
//@输出参数:该类别下∏P(ai|vj)


float CalculateWordProbability(int wordCount)
{
int countSame; //分类文本中的某单词在所有训练样本中出现次数
int countAll=0; //训练样本中总单词数
char token;
FILE *fp;
float wordProbability=1; //为后面联乘做准备
int i,j;
long hFile;
    _finddata_t fileinfo;




for(j=0;jmax) //找到最大概率并记录
   {
    max=finalProbability[i];
    classNo=i;
   }
   printf("该文本为类别%s的概率为:%.5e\n",classList[i],finalProbability[i]); //输出每个类别的最终概率
}
printf("\n经分析,该文本最有可能为%s类文本!\n",classList[classNo]); //输出最后分类结果
}




//@输入参数:分类文本


void NaiveBayesClassifier(char text[])
{
int vocabularyCount;//分类样本中单词数


vocabularyCount=SplitToWord(text); //对要分类的文本进行单词分割,结果存储在vocabulary数组中,返回分类样本中单词数
CalculateProbability(vocabularyCount); //计算最终概率
}




int main()
{
char text[]="Microsoft offered 44.6 billion dollars to buy Yahoo.February 1st network reported the Associated Press news, Microsoft offered 44.6 billion dollars in cash and stock to buy Yahoo search site.Microsoft offered to pay 31 dollars per share for Yahoo.Microsoft's acquisition offer on Jan. 31 premium of 62% than Yahoo's closing price of 19.18 dollars.Microsoft said that Yahoo shareholders can choose cash or stock transactions. Microsoft and Yahoo have sought cooperation in late 2006 and early 2007.The last two years, Yahoo has been in a dilemma: the market share decline,poor operating performance,stock prices tumbled sharply.Trying to make a difference for Microsoft in the Internet market, the acquisition of Yahoo is a shortcut, because the two sides have very strong complementarity.";


   NaiveBayesClassifier(text);
return 1;
}


 

你可能感兴趣的:(android布局,java,GUI,数据库)