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

上传人:F****n 文档编号:98878952 上传时间:2019-09-15 格式:DOC 页数:7 大小:30KB
返回 下载 相关 举报
基于朴素贝叶斯分类器的文本分类算法(c语言).doc_第1页
第1页 / 共7页
基于朴素贝叶斯分类器的文本分类算法(c语言).doc_第2页
第2页 / 共7页
基于朴素贝叶斯分类器的文本分类算法(c语言).doc_第3页
第3页 / 共7页
基于朴素贝叶斯分类器的文本分类算法(c语言).doc_第4页
第4页 / 共7页
基于朴素贝叶斯分类器的文本分类算法(c语言).doc_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《基于朴素贝叶斯分类器的文本分类算法(c语言).doc》由会员分享,可在线阅读,更多相关《基于朴素贝叶斯分类器的文本分类算法(c语言).doc(7页珍藏版)》请在金锄头文库上搜索。

1、基于朴素贝叶斯分类器的文本分类算法(C语言).txt两个人吵架,先说对不起的人,并不是认输了,并不是原谅了。他只是比对方更珍惜这份感情。#include #include #include /_getcwd(), _chdir()#include /_MAX_PATH, system()#include /_finddata_t, _findfirst(), _findnext(), _findclose()char vocabulary100020;/*声明公有二维数组,用来存储分割好的单词*/*=将要分类的文本分割成单词存储在二维数组vocabulary中=*/输入参数:要分类的文本/输出

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

3、.txt文件数int CountDirectory()int count=0; /txt文件计数器long hFile; _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 wordCo

4、unt)int countSame; /分类文本中的某单词在所有训练样本中出现次数int countAll=0; /训练样本中总单词数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(

5、fileinfo.name,r)=NULL) /是否能打开该文本 printf(Sorry!Cannot open the 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); / 开始读字符,直

6、到遇到空白符,说明找到一个词 keywordi = 0;/ 加结束符 countAll+; if (strcmp(keyword,vocabularyj) = 0) /比较两个单词是否相同 countSame+; fclose(fp); while (_findnext(hFile,&fileinfo) = 0); wordProbability*=(float)(countSame+1)/(float)(wordCount+countAll)*300; /计算P(wj|vi),为了扩大效果而*380return wordProbability; /*=计算每个类别的最终概率输出结果=*/输入

7、参数:分类文本中单词数 void CalculateProbability(int wordCount)/*将类别表存储在二维数组中*/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(

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

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

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

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

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 教学/培训

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