![樸素貝葉斯算法C--代碼有截圖(共7頁)_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/18/75d951cb-c211-4751-a0b9-303fc60f01e8/75d951cb-c211-4751-a0b9-303fc60f01e81.gif)
![樸素貝葉斯算法C--代碼有截圖(共7頁)_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/18/75d951cb-c211-4751-a0b9-303fc60f01e8/75d951cb-c211-4751-a0b9-303fc60f01e82.gif)
![樸素貝葉斯算法C--代碼有截圖(共7頁)_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/18/75d951cb-c211-4751-a0b9-303fc60f01e8/75d951cb-c211-4751-a0b9-303fc60f01e83.gif)
![樸素貝葉斯算法C--代碼有截圖(共7頁)_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/18/75d951cb-c211-4751-a0b9-303fc60f01e8/75d951cb-c211-4751-a0b9-303fc60f01e84.gif)
![樸素貝葉斯算法C--代碼有截圖(共7頁)_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/18/75d951cb-c211-4751-a0b9-303fc60f01e8/75d951cb-c211-4751-a0b9-303fc60f01e85.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上#include #include #include #include /_getcwd(), _chdir()#include /_MAX_PATH, system()#include /_finddata_t, _findfirst(), _findnext(), _findclose()char vocabulary100020;/*聲明公有二維數(shù)組,用來存儲分割好的單詞*/*=將要分類的文本分割成單詞存儲在二維數(shù)組vocabulary中=*/輸入?yún)?shù):要分類的文本/輸出參數(shù):該文本中總單詞數(shù)int SplitToWord(char text)int i=0;c
2、har seps=, .n; /*定義單詞的分隔符*/ char *substring; /*利用分隔符將文本內(nèi)容分割成單詞并存儲*/substring=strtok(text,seps); while(substring!=NULL) strcpy(vocabularyi,substring);/將單詞存儲到vocabulary數(shù)組中 substring=strtok(NULL,seps); i+;return i; /返回一共多少個單詞/*=計算該目錄下的文件數(shù)=*/輸入?yún)?shù):無/輸出參數(shù):該目錄下.txt文件數(shù)int CountDirectory()int count=0; /txt文件
3、計數(shù)器long hFile; _finddata_t fileinfo;/*查找.txt文件,記錄文件數(shù)*/ if (hFile=_findfirst(*.txt,&fileinfo)!=-1L) do count+; while (_findnext(hFile,&fileinfo) = 0);return count;/*=計算某類別中P(ai|vj)=*/輸入?yún)?shù):分類文本中單詞數(shù)/輸出參數(shù):該類別下P(ai|vj)float CalculateWordProbability(int wordCount)int countSame; /分類文本中的某單詞在所有訓(xùn)練樣本中出現(xiàn)次數(shù)int c
4、ountAll=0; /訓(xùn)練樣本中總單詞數(shù)char token;FILE *fp;float wordProbability=1; /為后面聯(lián)乘做準(zhǔn)備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!
5、Cannot open the file!n); exit(0); /*存儲此.txt文件中每個單詞并與分類文本的單詞作比較*/ while(token = fgetc(fp) != EOF) char keyword1024; i = 0; keyword0 = token; / 將每個詞第一個字符賦給數(shù)組第一個元素 while (keyword+i = fgetc(fp) != & keywordi != t & keywordi != EOF & keywordi != n); / 開始讀字符,直到遇到空白符,說明找到一個詞 keywordi = 0;/ 加結(jié)束符 countAll+;
6、if (strcmp(keyword,vocabularyj) = 0) /比較兩個單詞是否相同 countSame+; fclose(fp); while (_findnext(hFile,&fileinfo) = 0); wordProbability*=(float)(countSame+1)/(float)(wordCount+countAll)*300; /計算P(wj|vi),為了擴(kuò)大效果而*380return wordProbability; /*=計算每個類別的最終概率輸出結(jié)果=*/輸入?yún)?shù):分類文本中單詞數(shù) void CalculateProbability(int word
7、Count)/*將類別表存儲在二維數(shù)組中*/FILE *fp;char classList1020; /類別列表 char ch; /臨時讀取字符使用 int index=0; /classList的行標(biāo) int className_c=0; /classList的列標(biāo)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
8、_c+; else classListindexclassName_c=0; index+; className_c=0; ch = fgetc(fp);/*計算總文本數(shù)和每個類別下的文本數(shù)、P(ai|vj)*/int txtCount10; /每個類別下的訓(xùn)練文本數(shù)int countAll=0; /訓(xùn)練集中總文本數(shù)float wordProbability10; /每個類別的單詞概率,即P(ai|vj)if(_chdir(c:SogouCSample1) /更改當(dāng)前絕對路徑 printf(系統(tǒng)找不到指定路徑!n);else txtCount0=CountDirectory(); /獲取該類別
9、下.txt文件數(shù) countAll+=txtCount0; wordProbability0=CalculateWordProbability(wordCount); /獲取該類別下P(wj|vi)if(_chdir(c:SogouCSample2) /更改當(dāng)前絕對路徑 printf(系統(tǒng)找不到指定路徑!n);else txtCount1=CountDirectory(); /獲取該類別下.txt文件數(shù) countAll+=txtCount1; wordProbability1=CalculateWordProbability(wordCount); /獲取該類別下P(wj|vi)if(_ch
10、dir(c:SogouCSample3) /更改當(dāng)前絕對路徑 printf(系統(tǒng)找不到指定路徑!n);else txtCount2=CountDirectory(); /獲取該類別下.txt文件數(shù) countAll+=txtCount2; wordProbability2=CalculateWordProbability(wordCount); /獲取該類別下P(wj|vi)if(_chdir(c:SogouCSample4) /更改當(dāng)前絕對路徑 printf(系統(tǒng)找不到指定路徑!n);else txtCount3=CountDirectory(); /獲取該類別下.txt文件數(shù) countA
11、ll+=txtCount3; wordProbability3=CalculateWordProbability(wordCount); /獲取該類別下P(wj|vi)if(_chdir(c:SogouCSample5) /更改當(dāng)前絕對路徑 printf(系統(tǒng)找不到指定路徑!n);else txtCount4=CountDirectory(); /獲取該類別下.txt文件數(shù) countAll+=txtCount4; wordProbability4=CalculateWordProbability(wordCount); /獲取該類別下P(wj|vi)if(_chdir(c:SogouCSam
12、ple6) /更改當(dāng)前絕對路徑 printf(系統(tǒng)找不到指定路徑!n);else txtCount5=CountDirectory(); /獲取該類別下.txt文件數(shù) countAll+=txtCount5; wordProbability5=CalculateWordProbability(wordCount); /獲取該類別下P(wj|vi)if(_chdir(c:SogouCSample7) /更改當(dāng)前絕對路徑 printf(系統(tǒng)找不到指定路徑!n);else txtCount6=CountDirectory(); /獲取該類別下.txt文件數(shù) countAll+=txtCount6;
13、wordProbability6=CalculateWordProbability(wordCount); /獲取該類別下P(wj|vi)if(_chdir(C:SogouCSample8) /更改當(dāng)前絕對路徑 printf(系統(tǒng)找不到指定路徑!n);else txtCount7=CountDirectory(); /獲取該類別下.txt文件數(shù) countAll+=txtCount7; wordProbability7=CalculateWordProbability(wordCount); /獲取該類別下P(wj|vi)if(_chdir(C:SogouCSample9) /更改當(dāng)前絕對路徑
14、 printf(系統(tǒng)找不到指定路徑!n);else txtCount8=CountDirectory(); /獲取該類別下.txt文件數(shù) countAll+=txtCount8; wordProbability8=CalculateWordProbability(wordCount); /獲取該類別下P(wj|vi)if(_chdir(C:SogouCSample10) /更改當(dāng)前絕對路徑 printf(系統(tǒng)找不到指定路徑!n);else txtCount9=CountDirectory(); /獲取該類別下.txt文件數(shù) countAll+=txtCount9; wordProbabilit
15、y9=CalculateWordProbability(wordCount); /獲取該類別下P(wj|vi)/*計算先驗(yàn)概率和最終概率并輸出分類結(jié)果*/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經(jīng)分析,該文本
16、最有可能為%s類文本!n,classListclassNo); /輸出最后分類結(jié)果/*=調(diào)用文本分割函數(shù)和計算最終概率函數(shù)=*/輸入?yún)?shù):分類文本void NaiveBayesClassifier(char text)int vocabularyCount;/分類樣本中單詞數(shù)vocabularyCount=SplitToWord(text); /對要分類的文本進(jìn)行單詞分割,結(jié)果存儲在vocabulary數(shù)組中,返回分類樣本中單詞數(shù)CalculateProbability(vocabularyCount); /計算最終概率/*=程序入口=*/int main()char text=Microso
17、ft 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 operating performance,st
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 8 沏茶問題(說課稿)-2024-2025學(xué)年四年級上冊數(shù)學(xué)人教版001
- Unit 8 I can do this for you?(說課稿)-2024-2025學(xué)年譯林版(三起)(2024)英語三年級上冊
- Review Module Unit 1(說課稿)-2023-2024學(xué)年外研版(三起)英語五年級下冊
- 2024-2025學(xué)年新教材高中生物 第5章 基因突變及其他變異 微專題六 遺傳變異相關(guān)的解題方法說課稿 新人教版必修第二冊
- 2025合同樣例舞臺燈光音響租賃合同范本
- 2024春八年級語文下冊 第1單元 2回延安說課稿 新人教版
- 5草船借箭說課稿-2023-2024學(xué)年五年級下冊語文統(tǒng)編版
- Unit1 Making friends(說課稿)-2024-2025學(xué)年人教PEP版(2024)英語三年級上冊
- 2024-2025學(xué)年高中化學(xué) 第一章 物質(zhì)結(jié)構(gòu)元素周期律 第一節(jié) 元素周期表第3課時說課稿3 新人教版必修2
- 陽光板雨棚施工方案
- 理發(fā)店美容美發(fā)場所衛(wèi)生管理制度
- 成人失禁相關(guān)性皮炎的預(yù)防與護(hù)理
- 人教版(2024新版)七年級上冊數(shù)學(xué)第六章《幾何圖形初步》測試卷(含答案)
- 2025屆高三數(shù)學(xué)一輪總復(fù)習(xí) 第六章 專題六 幾何體的外接球與內(nèi)切球問題配套課件
- 引水隧洞施工支洞專項施工方案
- JT-T-496-2018公路地下通信管道高密度聚乙烯硅芯塑料管
- DB43-T 2939-2024 醬腌菜咸胚中亞硝酸鹽的測定頂空-氣相色譜法
- 食材配送投標(biāo)方案技術(shù)標(biāo)
- 再見深海合唱簡譜【珠海童年樹合唱團(tuán)】
- 高中物理 選修1 第四章 光(折射反射干涉衍射偏振)(2024人教版)
- 計算機(jī)安全弱口令風(fēng)險
評論
0/150
提交評論