贝叶斯分类器--文本分类的C语言实现

本文转载自:http://blog.csdn.net/caiye917015406/article/details/7887221,谢谢原作者!

==============================================================================

第一个是用c语言做的关于文本的分类,主要是对待分类文本所有单词在模板中概率的后验计算。算法比较简单,从网上下的(没记下地址,若不愿意公开,请留言,自当处理),稍作了一点修改。。,等有时间可以实现垃圾邮件的分类,利用斯坦福机器学习公开课中方法,统计高频词,利用朴素贝叶斯。等有时间和大家分享。

[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <direct.h> //_getcwd(), _chdir()  
  4. #include <stdlib.h> //_MAX_PATH, system()  
  5. #include <io.h> //_finddata_t, _findfirst(), _findnext(), _findclose()  
  6. #include<iostream>  
  7. using namespace std;  
  8. //#include<fstream>  
  9. char vocabulary[1000][20];/*声明公有二维数组,用来存储分割好的单词*/  
  10.   
  11.   
  12. /*=================将要分类的文本分割成单词存储在二维数组vocabulary中================*/  
  13. //@输入参数:要分类的文本  
  14. //@输出参数:该文本中总单词数  
  15.   
  16. int SplitToWord(char text[])  
  17. {  
  18. int i=0;  
  19. char seps[]=", .\n"/*定义单词的分隔符*/   
  20. char *substring;   
  21.   
  22. /******利用分隔符将文本内容分割成单词并存储******/  
  23. substring=strtok(text,seps);   
  24. while(substring!=NULL)   
  25. {     
  26.    strcpy(vocabulary[i],substring);//将单词存储到vocabulary数组中   
  27.    substring=strtok(NULL,seps);   
  28.    i++;  
  29. }  
  30. return i; //返回一共多少个单词  
  31. }  
  32.   
  33.   
  34. /*===============================计算该目录下的文件数================================*/  
  35. //@输入参数:无  
  36. //@输出参数:该目录下.txt文件数  
  37.   
  38. int CountDirectory()  
  39. {  
  40. int count=0; //txt文件计数器  
  41. long hFile;  
  42.     _finddata_t fileinfo;  
  43.   
  44. /********查找.txt文件,记录文件数**********/  
  45.     if ((hFile=_findfirst("*.txt",&fileinfo))!=-1L)  
  46.     {  
  47.         do  
  48.         {              
  49.     count++;  
  50.         } while (_findnext(hFile,&fileinfo) == 0);  
  51. }  
  52. return count;  
  53. }  
  54.   
  55.   
  56. /*===================================计算某类别中∏P(ai|vj)===================================*/  
  57. //@输入参数:分类文本中单词数  
  58. //@输出参数:该类别下∏P(ai|vj)  
  59.   
  60. float CalculateWordProbability(int wordCount)  
  61. {  
  62. int countSame; //分类文本中的某单词在所有训练样本中出现次数  
  63. int countAll=0; //训练样本中总单词数  
  64. char token;  
  65. FILE *fp;  
  66. float wordProbability=1; //为后面联乘做准备  
  67. int i,j;  
  68. long hFile;  
  69.     _finddata_t fileinfo;  
  70.   
  71.   
  72. for(j=0;j<wordCount;j++) //对于分类样本中的每一个单词  
  73. {  
  74.    countSame=0;  
  75.    countAll=0;  
  76.    if((hFile=_findfirst("*.txt",&fileinfo))!=-1L) //对于该类别下每一个.txt文本  
  77.    {  
  78.     do  
  79.     {  
  80.      if((fp=fopen(fileinfo.name,"r"))==NULL) //是否能打开该文本  
  81.      {  
  82.       printf("Sorry!Cannot open the file!\n");  
  83.       exit(0);  
  84.      }  
  85.   
  86.      /********存储此.txt文件中每个单词并与分类文本的单词作比较*******/  
  87.      while((token = fgetc(fp)) != EOF)   
  88.      {  
  89.       char keyword[1024];   
  90.       i = 0;   
  91.        
  92.       keyword[0] = token; // 将每个词第一个字符赋给数组第一个元素  
  93.       while ((keyword[++i] = fgetc(fp)) != ' ' && keyword[i] != '\t' && keyword[i] != EOF && keyword[i] != '\n'); // 开始读字符,直到遇到空白符,说明找到一个词   
  94.       keyword[i] = '\0';// 加结束符  
  95.       countAll++;  
  96.   
  97.       if (strcmp(keyword,vocabulary[j]) == 0) //比较两个单词是否相同  
  98.        countSame++;  
  99.      }  
  100.      fclose(fp);  
  101.   
  102.     }while (_findnext(hFile,&fileinfo) == 0);   
  103.    }  
  104.    wordProbability*=(float)(countSame+1)/(float)(wordCount+countAll)*300; //计算∏P(wj|vi),为了扩大效果而*380  
  105. }  
  106.   
  107. return wordProbability;  
  108. }  
  109.     
  110.   
  111. /*============================计算每个类别的最终概率输出结果===============================*/  
  112. //@输入参数:分类文本中单词数  
  113.     
  114. void CalculateProbability(int wordCount,int num)  
  115. {  
  116. /*********将类别表存储在二维数组中*********/  
  117. FILE *fp;  
  118. char classList[10][20]; //类别列表  
  119.     char ch;    //临时读取字符使用  
  120.     int index=0; //classList的行标  
  121.     int className_c=0; //classList的列标  
  122.   
  123. if((fp=fopen("ClassList.txt","r"))==NULL)  
  124.     {  
  125.         printf("Failed to open the file: ClassList.txt.\n");  
  126.     }  
  127.     ch = fgetc(fp);  
  128.     while(ch!=EOF)  
  129.     {  
  130.         if(ch!='\n')  
  131.         {  
  132.             classList[index][className_c]=ch;  
  133.             className_c++;  
  134.         }  
  135.         else  
  136.         {  
  137.             classList[index][className_c]='\0';  
  138.             index++;  
  139.             className_c=0;  
  140.         }  
  141.    ch = fgetc(fp);  
  142. }  
  143.   
  144. /********计算总文本数和每个类别下的文本数、∏P(ai|vj)********/  
  145. int txtCount[10]; //每个类别下的训练文本数  
  146. int countAll=0; //训练集中总文本数  
  147. float wordProbability[10]; //每个类别的单词概率,即∏P(ai|vj)  
  148.   
  149. if(_chdir("D:\\openCV\\openCVProject\\openCVtext\\贝叶斯(文本分类)—c语言\\example\\1")) //更改当前绝对路径  
  150.      printf("系统找不到指定路径!\n");  
  151. else  
  152. {  
  153.    txtCount[0]=CountDirectory(); //获取该类别下.txt文件数  
  154.    countAll+=txtCount[0];  
  155.    wordProbability[0]=CalculateWordProbability(wordCount); //获取该类别下∏P(wj|vi)  
  156. }  
  157. if(_chdir("D:\\openCV\\openCVProject\\openCVtext\\贝叶斯(文本分类)—c语言\\example\\2")) //更改当前绝对路径  
  158.    printf("系统找不到指定路径!\n");  
  159. else  
  160. {  
  161.    txtCount[1]=CountDirectory(); //获取该类别下.txt文件数  
  162.    countAll+=txtCount[1];  
  163.    wordProbability[1]=CalculateWordProbability(wordCount); //获取该类别下∏P(wj|vi)  
  164. }  
  165. if(_chdir("D:\\openCV\\openCVProject\\openCVtext\\贝叶斯(文本分类)—c语言\\example\\3")) //更改当前绝对路径  
  166.      printf("系统找不到指定路径!\n");  
  167. else  
  168. {  
  169.    txtCount[2]=CountDirectory(); //获取该类别下.txt文件数  
  170.    countAll+=txtCount[2];  
  171.    wordProbability[2]=CalculateWordProbability(wordCount); //获取该类别下∏P(wj|vi)  
  172. }  
  173. if(_chdir("D:\\openCV\\openCVProject\\openCVtext\\贝叶斯(文本分类)—c语言\\example\\4")) //更改当前绝对路径  
  174.      printf("系统找不到指定路径!\n");  
  175. else  
  176. {  
  177.    txtCount[3]=CountDirectory(); //获取该类别下.txt文件数  
  178.    countAll+=txtCount[3];  
  179.    wordProbability[3]=CalculateWordProbability(wordCount); //获取该类别下∏P(wj|vi)  
  180. }  
  181. if(_chdir("D:\\openCV\\openCVProject\\openCVtext\\贝叶斯(文本分类)—c语言\\example\\5")) //更改当前绝对路径  
  182.      printf("系统找不到指定路径!\n");  
  183. else  
  184. {  
  185.    txtCount[4]=CountDirectory(); //获取该类别下.txt文件数  
  186.    countAll+=txtCount[4];  
  187.    wordProbability[4]=CalculateWordProbability(wordCount); //获取该类别下∏P(wj|vi)  
  188. }  
  189. if(_chdir("D:\\openCV\\openCVProject\\openCVtext\\贝叶斯(文本分类)—c语言\\example\\6")) //更改当前绝对路径  
  190.      printf("系统找不到指定路径!\n");  
  191. else  
  192. {  
  193.    txtCount[5]=CountDirectory(); //获取该类别下.txt文件数  
  194.    countAll+=txtCount[5];  
  195.    wordProbability[5]=CalculateWordProbability(wordCount); //获取该类别下∏P(wj|vi)  
  196. }  
  197. if(_chdir("D:\\openCV\\openCVProject\\openCVtext\\贝叶斯(文本分类)—c语言\\example\\7")) //更改当前绝对路径  
  198.      printf("系统找不到指定路径!\n");  
  199. else  
  200. {  
  201.    txtCount[6]=CountDirectory(); //获取该类别下.txt文件数  
  202.    countAll+=txtCount[6];  
  203.    wordProbability[6]=CalculateWordProbability(wordCount); //获取该类别下∏P(wj|vi)  
  204. }  
  205. if(_chdir("D:\\openCV\\openCVProject\\openCVtext\\贝叶斯(文本分类)—c语言\\example\\8")) //更改当前绝对路径  
  206.      printf("系统找不到指定路径!\n");  
  207. else  
  208. {  
  209.    txtCount[7]=CountDirectory(); //获取该类别下.txt文件数  
  210.    countAll+=txtCount[7];  
  211.    wordProbability[7]=CalculateWordProbability(wordCount); //获取该类别下∏P(wj|vi)  
  212. }  
  213. if(_chdir("D:\\openCV\\openCVProject\\openCVtext\\贝叶斯(文本分类)—c语言\\example\\9")) //更改当前绝对路径  
  214.      printf("系统找不到指定路径!\n");  
  215. else  
  216. {  
  217.    txtCount[8]=CountDirectory(); //获取该类别下.txt文件数  
  218.    countAll+=txtCount[8];  
  219.    wordProbability[8]=CalculateWordProbability(wordCount); //获取该类别下∏P(wj|vi)  
  220. }  
  221. if(_chdir("D:\\openCV\\openCVProject\\openCVtext\\贝叶斯(文本分类)—c语言\\example\\10")) //更改当前绝对路径  
  222.      printf("系统找不到指定路径!\n");  
  223. else  
  224. {  
  225.    txtCount[9]=CountDirectory(); //获取该类别下.txt文件数  
  226.    countAll+=txtCount[9];  
  227.    wordProbability[9]=CalculateWordProbability(wordCount); //获取该类别下∏P(wj|vi)  
  228. }  
  229.   
  230. /*******计算先验概率和最终概率并输出分类结果*******/  
  231. float max=0;  
  232. int classNo=0;  
  233. float priorProbability[10];  
  234. float finalProbability[10];  
  235.   
  236. for(int i=0;i<num;i++)   
  237. {  
  238.    priorProbability[i]=(float)txtCount[i]/(float)countAll; //先验概率  
  239.    finalProbability[i]=priorProbability[i]*wordProbability[i]; //最终概率  
  240.    if(finalProbability[i]>max) //找到最大概率并记录  
  241.    {  
  242.     max=finalProbability[i];  
  243.     classNo=i;  
  244.    }  
  245.    printf("该文本为类别%s的概率为:%.5e\n",classList[i],finalProbability[i]); //输出每个类别的最终概率  
  246. }  
  247. printf("\n经分析,该文本最有可能为%s类文本!\n",classList[classNo]); //输出最后分类结果  
  248. }  
  249.   
  250.   
  251. /*===================调用文本分割函数和计算最终概率函数======================*/  
  252. //@输入参数:分类文本  
  253.   
  254. void NaiveBayesClassifier(char text[],int num)  
  255. {  
  256. int vocabularyCount;//分类样本中单词数  
  257.   
  258. vocabularyCount=SplitToWord(text); //对要分类的文本进行单词分割,结果存储在vocabulary数组中,返回分类样本中单词数  
  259. CalculateProbability(vocabularyCount,num); //计算最终概率  
  260. }  
  261.   
  262.   
  263. /*===================程序入口====================*/  
  264. int main()  
  265. {  
  266.    FILE *fp;  
  267.    if((fp=fopen("text.txt","r"))==NULL)  
  268.    {  
  269.         printf("Failed to open the file: ClassList.txt.\n");  
  270.    }  
  271.    char ch = fgetc(fp);  
  272.    int i=0;  
  273.    while(ch!=EOF)  
  274.    {  
  275.        ch = fgetc(fp);  
  276.        i++;  
  277.    }  
  278.    char *text=new char(i+1);  
  279.    fseek(fp,0,SEEK_SET);//  
  280.    ch = fgetc(fp);  
  281.    int j=0;  
  282.    while(ch!=EOF)  
  283.    {  
  284.        ch = fgetc(fp);  
  285.        cout<<ch;  
  286.        text[j]=ch;  
  287.        j++;  
  288.    }  
  289.   // char text[]=new char(i);;  
  290.    int num = 2;  
  291.   
  292.    NaiveBayesClassifier(text,num); /*调用朴素贝叶斯分类函数,返回最终分类结果*/  
  293. return 1;  
  294. }  
  295.    

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值