朴素贝叶斯算法C--代码有截图

上传人:我*** 文档编号:132612444 上传时间:2020-05-18 格式:DOCX 页数:7 大小:13.13KB
返回 下载 相关 举报
朴素贝叶斯算法C--代码有截图_第1页
第1页 / 共7页
朴素贝叶斯算法C--代码有截图_第2页
第2页 / 共7页
朴素贝叶斯算法C--代码有截图_第3页
第3页 / 共7页
朴素贝叶斯算法C--代码有截图_第4页
第4页 / 共7页
朴素贝叶斯算法C--代码有截图_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《朴素贝叶斯算法C--代码有截图》由会员分享,可在线阅读,更多相关《朴素贝叶斯算法C--代码有截图(7页珍藏版)》请在金锄头文库上搜索。

1、#include #include #include #include /_getcwd(), _chdir()#include /_MAX_PATH, system()#include /_finddata_t, _findfirst(), _findnext(), _findclose()char vocabulary100020;/*声明公有二维数组,用来存储分割好的单词*/*=将要分类的文本分割成单词存储在二维数组vocabulary中=*/输入参数:要分类的文本/输出参数:该文本中总单词数int SplitToWord(char text)int i=0;char seps=, .n

2、; /*定义单词的分隔符*/ char *substring; /*利用分隔符将文本内容分割成单词并存储*/substring=strtok(text,seps); while(substring!=NULL) strcpy(vocabularyi,substring);/将单词存储到vocabulary数组中 substring=strtok(NULL,seps); i+;return i; /返回一共多少个单词/*=计算该目录下的文件数=*/输入参数:无/输出参数:该目录下.txt文件数int CountDirectory()int count=0; /txt文件计数器long hFile

3、; _finddata_t fileinfo;/*查找.txt文件,记录文件数*/ if (hFile=_findfirst(*.txt,&fileinfo)!=-1L) do count+; while (_findnext(hFile,&fileinfo) = 0);return count;/*=计算某类别中P(ai|vj)=*/输入参数:分类文本中单词数/输出参数:该类别下P(ai|vj)float CalculateWordProbability(int wordCount)int countSame; /分类文本中的某单词在所有训练样本中出现次数int countAll=0; /训

4、练样本中总单词数char token;FILE *fp;float wordProbability=1; /为后面联乘做准备int i,j;long hFile; _finddata_t fileinfo;for(j=0;jwordCount;j+) /对于分类样本中的每一个单词 countSame=0; countAll=0; if(hFile=_findfirst(*.txt,&fileinfo)!=-1L) /对于该类别下每一个.txt文本 do if(fp=fopen(fileinfo.name,r)=NULL) /是否能打开该文本 printf(Sorry!Cannot open t

5、he file!n); exit(0); /*存储此.txt文件中每个单词并与分类文本的单词作比较*/ while(token = fgetc(fp) != EOF) char keyword1024; i = 0; keyword0 = token; / 将每个词第一个字符赋给数组第一个元素 while (keyword+i = fgetc(fp) != & keywordi != t & keywordi != EOF & keywordi != n); / 开始读字符,直到遇到空白符,说明找到一个词 keywordi = 0;/ 加结束符 countAll+; if (strcmp(ke

6、yword,vocabularyj) = 0) /比较两个单词是否相同 countSame+; fclose(fp); while (_findnext(hFile,&fileinfo) = 0); wordProbability*=(float)(countSame+1)/(float)(wordCount+countAll)*300; /计算P(wj|vi),为了扩大效果而*380return wordProbability; /*=计算每个类别的最终概率输出结果=*/输入参数:分类文本中单词数 void CalculateProbability(int wordCount)/*将类别表存

7、储在二维数组中*/FILE *fp;char classList1020; /类别列表 char ch; /临时读取字符使用 int index=0; /classList的行标 int className_c=0; /classList的列标if(fp=fopen(ClassList.txt,r)=NULL) printf(Failed to open the file: ClassList.txt.n); ch = fgetc(fp); while(ch!=EOF) if(ch!=n) classListindexclassName_c=ch; className_c+; else cla

8、ssListindexclassName_c=0; index+; className_c=0; ch = fgetc(fp);/*计算总文本数和每个类别下的文本数、P(ai|vj)*/int txtCount10; /每个类别下的训练文本数int countAll=0; /训练集中总文本数float wordProbability10; /每个类别的单词概率,即P(ai|vj)if(_chdir(c:SogouCSample1) /更改当前绝对路径 printf(系统找不到指定路径!n);else txtCount0=CountDirectory(); /获取该类别下.txt文件数 coun

9、tAll+=txtCount0; wordProbability0=CalculateWordProbability(wordCount); /获取该类别下P(wj|vi)if(_chdir(c:SogouCSample2) /更改当前绝对路径 printf(系统找不到指定路径!n);else txtCount1=CountDirectory(); /获取该类别下.txt文件数 countAll+=txtCount1; wordProbability1=CalculateWordProbability(wordCount); /获取该类别下P(wj|vi)if(_chdir(c:SogouCS

10、ample3) /更改当前绝对路径 printf(系统找不到指定路径!n);else txtCount2=CountDirectory(); /获取该类别下.txt文件数 countAll+=txtCount2; wordProbability2=CalculateWordProbability(wordCount); /获取该类别下P(wj|vi)if(_chdir(c:SogouCSample4) /更改当前绝对路径 printf(系统找不到指定路径!n);else txtCount3=CountDirectory(); /获取该类别下.txt文件数 countAll+=txtCount3

11、; wordProbability3=CalculateWordProbability(wordCount); /获取该类别下P(wj|vi)if(_chdir(c:SogouCSample5) /更改当前绝对路径 printf(系统找不到指定路径!n);else txtCount4=CountDirectory(); /获取该类别下.txt文件数 countAll+=txtCount4; wordProbability4=CalculateWordProbability(wordCount); /获取该类别下P(wj|vi)if(_chdir(c:SogouCSample6) /更改当前绝对路径 printf(系统找不到指定路径!n);else txtCount5=CountDirectory(); /获取该类别下.txt文件数 countAll+=txtCount5; wordProbability5=CalculateWordProbability(wordCount); /获取该类别下P(wj|vi)if(_chdir(c:SogouCSample7) /更改当前绝对路径 printf(系统找不到指定路径!n);else

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 事务文书

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号