Nodejs学习笔记NodeJS 学习笔记

余弦算法nodejs实现

2016-12-22  本文已影响7人  comeluder

// 载入模块
var Segment = require('segment');
// 创建实例
var segment = new Segment();
// 使用默认的识别模块及字典,载入字典文件需要1秒,仅初始化时执行一次即可
segment.useDefault();

// 开始分词

var str_total = [];

var str1 = segment.doSegment('我是一个人', {stripPunctuation: true});
str1.forEach(function(single_random){

console.log(single_random);
console.log(single_random.w);

if(str_total.indexOf(single_random.w)<0)
    str_total.push(single_random.w);

});

console.log('中国人');

var str2 = segment.doSegment('我不是人', {stripPunctuation: true});
str2.forEach(function(single_random){
console.log(single_random);
console.log(single_random.w);

if(str_total.indexOf(single_random.w)<0)
    str_total.push(single_random.w);

});

console.log(str_total);

str1_frequency = [];
str2_frequency = [];
for (var i=0;i<str_total.length;i++)
{
str1_frequency[i] = 0;
str2_frequency[i] = 0;
}

console.log(str1_frequency);
console.log(str2_frequency);

str1.forEach(function(single_random){

var index = str_total.indexOf(single_random.w);
//console.log(index);
if(index >=0)
    str1_frequency[index] = str1_frequency[index] + 1;

});

str2.forEach(function(single_random){

var index = str_total.indexOf(single_random.w);
//console.log(index);
if(index >=0)
    str2_frequency[index] = str2_frequency[index] + 1;

});
console.log(str1_frequency);
console.log(str2_frequency);

var fenzi = 0;
var fenmu1 = 0;
var fenmu2 = 0;
for (var i=0;i<str_total.length;i++)
{
fenzi += str1_frequency[i] * str2_frequency[i];
fenmu1 += str1_frequency[i] * str1_frequency[i];
fenmu2 += str2_frequency[i] * str2_frequency[i];

}
console.log(fenzi);
console.log(fenmu1);
console.log(fenmu2);
fenmu1 = Math.sqrt(fenmu1);
fenmu2 = Math.sqrt(fenmu2);
console.log(fenmu1);
console.log(fenmu2);

var result = fenzi/(fenmu1 * fenmu2);
console.log(result);

上一篇 下一篇

猜你喜欢

热点阅读