貝葉斯分類器

app.jsjavascript

//貝葉斯分類器
//特徵字典:一些詞語數組(改爲了特徵函數),定義生產特徵
//類別的訓練集:找出特徵在類別中的機率
//->產出機率矩陣
//特徵組合全展開,找出特徵相同,機率最大的列表
//生產特徵組合字典,作判斷
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.jshtml

//貝葉斯分類器
//特徵字典:一些詞語數組
//類別的訓練集:找出特徵在類別中的機率
//->產出機率矩陣
//特徵組合全展開,找出特徵相同,機率最大的列表
//生產特徵組合字典,作判斷
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.jsjava

//貝葉斯分類器
//特徵字典:一些詞語數組
//類別的訓練集:找出特徵在類別中的機率
//->產出機率矩陣
//特徵組合全展開,找出特徵相同,機率最大的列表
//生產特徵組合字典,作判斷
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,
}

  

每一個文件都有特徵,將特徵組合成矩陣app

//貝葉斯分類器
//特徵字典:一些詞語數組
//類別的訓練集:找出特徵在類別中的機率
//->產出機率矩陣
//特徵組合全展開,找出特徵相同,機率最大的列表
//生產特徵組合字典,作判斷

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.jsless

//貝葉斯分類器
//特徵字典:一些詞語數組
//類別的訓練集:找出特徵在類別中的機率
//->產出機率矩陣
//特徵組合全展開,找出特徵相同,機率最大的列表
//生產特徵組合字典,作判斷
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;
相關文章
相關標籤/搜索