nodejieba"結(jié)巴"分詞的Node.js版本
NodeJieba "結(jié)巴"分詞的Node.js版本
Introduction
NodeJieba只是CppJieba簡單包裝而成的node擴展,用來進(jìn)行中文分詞。
Install
npm install nodejieba
因為npm速度很慢而且經(jīng)常因為墻的原因出現(xiàn)莫名其妙的問題,在此強烈建議使用cnpm,命令如下:
npm --registry=http://r.cnpmjs.org install nodejieba
Usage
默認(rèn)分詞算法
初始化
var segment = require("nodejieba");
segment.loadDict("./node_modules/nodejieba/dict/jieba.dict.utf8", "./node_modules/nodejieba/dict/hmm_model.utf8");
阻塞式調(diào)用
var wordList = segment.cutSync("阻塞模式分詞"); if (wordList.constructor == Array) // just for tutorial, this is always be true {
wordList.forEach(function(word) { console.log(word);
});
}
非阻塞式調(diào)用
segment.cut("非阻塞模式分詞", function(wordList) {
wordList.forEach(function(word) { console.log(word);
});
});
搜索引擎分詞算法
初始化
var segment = require("nodejieba");
segment.queryLoadDict("./node_modules/nodejieba/dict/jieba.dict.utf8", "./node_modules/nodejieba/dict/hmm_model.utf8");
阻塞式調(diào)用
var wordList = segment.queryCutSync("阻塞模式分詞"); if (wordList.constructor == Array) // just for tutorial, this is always be true {
wordList.forEach(function(word) { console.log(word);
});
}
非阻塞式調(diào)用
segment.queryCut("非阻塞模式分詞", function(wordList) {
wordList.forEach(function(word) { console.log(word);
});
});
具體用法可以參考 test/segment.js test/query_segment.js
Testing
在node v0.10.2下測試通過
Demo
http://cppjieba-webdemo.herokuapp.com/ (chrome is suggested)
Thanks
評論
圖片
表情
