app.js
//贝叶斯分类器
//特征字典:一些词语数组(改成了特征函数),定义生产特征
//类别的训练集:找出特征在类别中的概率
//->产出概率矩阵
//特征组合全展开,找出特征相同,概率最大的列表
//生产特征组合字典,做判断
const fs=require('fs');
function getText(filepath){
return fs.readFileSync(filepath).toString();
}
const makeFeatures=require('./makeFeatures')
const {tagNames,getTagIndex,getAllP}=require('./config')
const html=getText('./src/tag2/2.txt')
const nfeature=makeFeatures({html});
// console.log(getAllP(nfeature));
const index=getTagIndex(nfeature);
console.log(tagNames[index],index,tagNames);
特征函数 makeFeatures.js
//贝叶斯分类器 //特征字典:一些词语数组 //类别的训练集:找出特征在类别中的概率 //->产出概率矩阵 //特征组合全展开,找出特征相同,概率最大的列表 //生产特征组合字典,做判断 const locals={}; const funcArr=[ function ({html,locals}) { if(html.indexOf('垃圾')>-1){ return '1' } return '0' }, function ({html,locals}) { if(html.indexOf('食品')>-1){ return '1' } return '0' }, function ({html,locals}) { if(html.indexOf('商品')>-1){ return '1' } return '0' } ]; const crypto = require('crypto'); const cryptoPassFunc = function(password) { const md5 = crypto.createHash('md5'); return md5.update(password).digest('hex'); }; locals.md5Cache={} //生产特征 function makeFeatures({html}) { const md5=cryptoPassFunc(html) if(!locals.md5Cache[md5]){ const req={ locals:locals, html:html, }; let s=''; for(let i=0;i<funcArr.length;i++){ s=s+funcArr[i](req); } locals.md5Cache[md5]=s; } return locals.md5Cache[md5]; } module.exports=makeFeatures;
配置文件,定义了分类名称、训练集位置 config.js
//贝叶斯分类器 //特征字典:一些词语数组 //类别的训练集:找出特征在类别中的概率 //->产出概率矩阵 //特征组合全展开,找出特征相同,概率最大的列表 //生产特征组合字典,做判断 const glob=require('glob'); const Bayes=require('./utils/Bayes'); function getMaxIndex(arr) { let index=0; for(let i=1;i<arr.length;i++){ if(arr[index].lessThan(arr[i])){ index=i; } } return index; } const tagNames=glob.sync('./src/*/').map(function (path) { return path.replace(/\.\/src\/(.+)?\//,'$1') }) const tagArr=[glob.sync("./src/tag1/*"),glob.sync("./src/tag2/*"),glob.sync("./src/tag3/*")] //发生事件map表 const rect=Bayes.getFeaPosRect(tagArr) // console.log(rect); const featureMap={} function getAllP(feature) { if(!featureMap[feature]){ featureMap[feature]=Bayes.getAllP(feature,rect) } return featureMap[feature]; } function getTagIndex(feature) { return getMaxIndex(getAllP(feature)); } module.exports={ getAllP, getTagIndex, tagNames, };
入口工具,存在了bayes公式相关运算 utils/Bayes.js
//贝叶斯分类器
//特征字典:一些词语数组
//类别的训练集:找出特征在类别中的概率
//->产出概率矩阵
//特征组合全展开,找出特征相同,概率最大的列表
//生产特征组合字典,做判断
const getFeaPosRect=require('./getFeaPosRect');
const execMathExpress=require('./execMathExpress');
//特征n存在,属于类别m的概率
function getPSW(n,m,rect) {
const PWS=rect[m][n];
const PWSArr=rect.map(function (arr) {
return arr[n];
})
const str=`${PWS}/(${PWSArr.join('+')})`;
return execMathExpress(str)
}
//联合SW1*SWn
function getPE(feature,m,rect){
const arr=[]
for(let i =0;i<feature.length;i++){
if(feature[i]=='1'){
const PSW=getPSW(i,m,rect)
arr.push(PSW)
}
}
return execMathExpress(arr.join('*'))
}
//获取全概率 p1*pn
function getPEN(feature,rect){
const arr=[]
for(let i=0;i<rect.length;i++){
arr.push(getPE(feature,i,rect))
}
const str=`${arr.join('+')}`;
return execMathExpress(str)
}
//特征feature属于类别m的概率
function getP(feature,m,rect){
const arr=[]
for(let i=0;i<rect.length;i++){
arr.push(getPE(feature,i,rect))
}
const pE=getPE(feature,m,rect);
const pEN=getPEN(feature,rect);
return execMathExpress(`(${pE})/(${pEN})`)
}
//特征feature属于所有类别的概率
function getAllP(feature,rect) {
const arr=[];
for(let i=0;i<rect.length;i++){
arr.push(getP(feature,i,rect))
}
return arr;
}
module.exports={
getFeaPosRect:getFeaPosRect,
getAllP:getAllP,
}
每个文件都有特征,将特征组合成矩阵
//贝叶斯分类器 //特征字典:一些词语数组 //类别的训练集:找出特征在类别中的概率 //->产出概率矩阵 //特征组合全展开,找出特征相同,概率最大的列表 //生产特征组合字典,做判断 const fs=require('fs'); function getText(filepath){ return fs.readFileSync(filepath).toString(); } const makeFeatures=require('../makeFeatures'); function makeFeatureRect(fileArr) { const rect=[] for(let i=0;i<fileArr.length;i++){ const html=getText(fileArr[i]) const feature=makeFeatures({html}) rect.push(feature) } return rect; } module.exports=makeFeatureRect;
单个特征存在的情况,属于类别的概率,getFeaPosInTag.js
//贝叶斯分类器 //特征字典:一些词语数组 //类别的训练集:找出特征在类别中的概率 //->产出概率矩阵 //特征组合全展开,找出特征相同,概率最大的列表 //生产特征组合字典,做判断 const makeFeatureRect=require('./makeFeatureRect') //找出特征在类别中的概率 function getFeaPosInTag(fileArr) { const featureRect=makeFeatureRect(fileArr); const arr=[]; for(let i=0;i<featureRect[0].length;i++){ let num=0; const den=featureRect.length; for(let j=0;j<featureRect.length;j++){ if(featureRect[j][i]==='1'){ num++; } } arr.push(num+'/'+den); } return arr; } module.exports=getFeaPosInTag;
找出每个特征存在时,每个类别的概率,getFeaPosRect.js
//贝叶斯分类器 //特征字典:一些词语数组 //类别的训练集:找出特征在类别中的概率 //->产出概率矩阵 //特征组合全展开,找出特征相同,概率最大的列表 //生产特征组合字典,做判断 const getFeaPosInTag=require('./getFeaPosInTag') //找出特征在类别中的概率,多个类别 function getFeaPosRect(tagArr) { const rect=[] for(let i=0;i<tagArr.length;i++){ rect.push(getFeaPosInTag(tagArr[i])) } return rect; } module.exports=getFeaPosRect;