




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
基于朴素贝叶斯分类器的文本分类算法(C语言).txt两个人吵架,先说对不起的人,并不是认输了,并不是原谅了。他只是比对方更珍惜这份感情。#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; /*定义单词的分隔符*/ 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; _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; /训练样本中总单词数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(,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); / 开始读字符,直到遇到空白符,说明找到一个词 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; /*=计算每个类别的最终概率输出结果=*/输入参数:分类文本中单词数 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(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(系统找不到指定路径!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=CalculateWordProbability(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 txtCount3=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) /更改当前绝对路径 printf(系统找不到指定路径!n);else txtCount6=CountDirectory(); /获取该类别下.txt文件数 countAll+=txtCount6; wordProbability6=CalculateWordProbability(wordCount); /获取该类别下P(wj|vi)if(_chdir(F:SogouCSample8) /更改当前绝对路径 printf(系统找不到指定路径!n);else txtCount7=CountDirectory(); /获取该类别下.txt文件数 countAll+=txtCount7; wordProbability7=CalculateWordProbability(wordCount); /获取该类别下P(wj|vi)if(_chdir(F:SogouCSample9) /更改当前绝对路径 printf(系统找不到指定路径!n);else txtCount8=CountDirectory(); /获取该类别下.txt文件数 countAll+=txtCount8; wordProbability8=CalculateWordProbability(wordCount); /获取该类别下P(wj|vi)if(_chdir(F:SogouCSample10) /更改当前绝对路径 printf(系统找不到指定路径!n);else txtCount9=CountDirectory(); /获取该类别下.txt文件数 countAll+=txtCount9; wordProbability9=CalculateWordProbability(wordCount); /获取该类别下P(wj|vi)/*计算先验概率和最终概率并输出分类结果*/float max=0;int classNo=0;float priorProbability10;float finalProbability10;for(int i=0;imax) /找到最大概率并记录 max=finalProbabilityi; classNo=i; printf(该文本为类别%s的概率为:%.5en,classListi,finalProbabilityi); /输出每个类别的最终概率printf(n经分析,该文本最有可能为%s类文本!n,classListclassNo); /输出最后分类结果/*=调用文本分割函数和计算最终概率函数=*/输入参数:分类文本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.Microsofts acquisition offer on Jan. 31 premium of 62% than Yahoos 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 op
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 吉林省白城市实验高级中学2025-2026学年高二上学期开学考试物理试卷
- 超材料声学滤波器-洞察及研究
- 安徽省滁州市凤阳县2024-2025学年八年级下学期第一次月考生物试题(含答案)
- 模板公共安全防控-洞察及研究
- 部门安全培训考核表课件
- 智能图标字体应用-洞察及研究
- 垃圾分类政策优化-洞察及研究
- 辩证唯物主义课件
- 基于区块链的跨境反光织带供应链溯源体系设计
- 基于人工智能的氟化反应条件动态优化与数字孪生系统构建
- 【MOOC】当代社会中的科学与技术-南京大学 中国大学慕课MOOC答案
- 学习六条禁令心得体会
- 京东方校园2024招聘胜任力测评题库
- (完整版)水利部考试历年真题-水利基础知识试题集
- 医院客服主管年终总结
- 软件系统集成方案
- 幼儿园饮用水突发污染事故应急处理预案
- 联通技能竞赛考试题及答案(5G核心网知识部分)
- 政治-中国特色社会主义教材探究与分享参考答案高中政治统编版必修一
- 恶性贫血的动物模型构建和研究
- “余香萦绕”-2024年中考语文作文押题分析+学生习作+素材积累+金句积累
评论
0/150
提交评论