// 載入模塊
var Segment = require('segment');
// 創(chuàng)建實(shí)例
var segment = new Segment();
// 使用默認(rèn)的識別模塊及字典购公,載入字典文件需要1秒,僅初始化時執(zhí)行一次即可
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);