原文地址:http://blog.sina.com.cn/s/blog_6b36e6750100zf6l.html
#include <stdio.h>
#include <string.h>
#include <direct.h> //_getcwd(), _chdir()
#include <stdlib.h> //_MAX_PATH, system()
#include <io.h> //_finddata_t, _findfirst(), _findnext(), _findclose()
char vocabulary[1000][20];
//@输入参数:要分类的文本
//@输出参数:该文本中总单词数
int SplitToWord(char text[])
{
int i=0;
char seps[]=", .\n";
char *substring;
substring=strtok(text,seps);
while(substring!=NULL)
{
}
return i; //返回一共多少个单词
}
//@输入参数:无
//@输出参数:该目录下.txt文件数
int CountDirectory()
{
int count=0; //txt文件计数器
long hFile;
}
return count;
}
//@输入参数:分类文本中单词数
//@输出参数:该类别下∏P(ai|vj)
float CalculateWordProbability
(int wordCount)
{
int countSame; //分类文本中的某单词在所有训练样本中出现次数
int countAll=0; //训练样本中总单词数
char token;
FILE *fp;
float wordProbability=1; //为后面联乘做准备
int i,j;
long hFile;
for(j=0;j<wordCount;j++) //对于分类样本中的每一个单词
{
}
return wordProbability;
}
//@输入参数:分类文本中单词数
void CalculateProbability(int wordCount)
{
FILE *fp;
char classList[10][20]; //类别列表
if((fp=fopen("ClassList.txt","r"))==NULL)
}
int txtCount[10]; //每个类别下的训练文本数
int countAll=0; //训练集中总文本数
float wordProbability[10]; //每个类别的单词概率,即∏P(ai|vj)
if(_chdir("F:\\SogouC\\Sample\\1")) //更改当前绝对路径
else
{
}
if(_chdir("F:\\SogouC\\Sample\\2")) //更改当前绝对路径
else
{
}
if(_chdir("F:\\SogouC\\Sample\\3")) //更改当前绝对路径
else
{
}
if(_chdir("F:\\SogouC\\Sample\\4")) //更改当前绝对路径
else
{
}
if(_chdir("F:\\SogouC\\Sample\\5")) //更改当前绝对路径
else
{
}
if(_chdir("F:\\SogouC\\Sample\\6")) //更改当前绝对路径
else
{
}
if(_chdir("F:\\SogouC\\Sample\\7")) //更改当前绝对路径
else
{
}
if(_chdir("F:\\SogouC\\Sample\\8")) //更改当前绝对路径
else
{
}
if(_chdir("F:\\SogouC\\Sample\\9")) //更改当前绝对路径
else
{
}
if(_chdir("F:\\SogouC\\Sample\\10")) //更改当前绝对路径
else
{
}
float max=0;
int classNo=0;
float priorProbability[10];
float finalProbability[10];
for(int i=0;i<10;i++)
{
}
printf("\n经分析,该文本最有可能为%s类文本!\n",classList[classNo]); //输出最后分类结果
}
//@输入参数:分类文本
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.Microsoft's acquisition offer on Jan. 31 premium of 62% than Yahoo's 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,stock prices tumbled sharply.Trying to make a difference for Microsoft in the Internet market, the acquisition of Yahoo is a shortcut, because the two sides have very strong complementarity.";
return 1;
}