微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

nodejieba "结巴"分词的Node.js版本

程序名称:nodejieba

授权协议: MIT

操作系统: 跨平台

开发语言: JavaScript

nodejieba 介绍

NodeJieba “结巴”分词的Node.js版本

Introduction

NodeJieba只是CppJieba简单包装而成的node扩展,用来进行中文分词。

详见NodeJiebaBlog

Install

npm install nodejieba

因为npm速度很慢而且经常因为墙的原因出现莫名其妙的问题,在此强烈建议使用cnpm,命令如下:

npm --registry=http://r.cnpmjs.org install nodejieba

Usage

认分词算法

初始化

var segment = require("nodejieba");
segment.loadDict("./node_modules/nodejieba/dict/jieba.dict.utf8", "./node_modules/nodejieba/dict/hmm_model.utf8");

阻塞式调用

var wordList = segment.cutSync("阻塞模式分词"); if (wordList.constructor == Array) // just for tutorial, this is always be true  {
    wordList.forEach(function(word) { console.log(word);     
    });
}

非阻塞式调用

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");

阻塞式调用

var wordList = segment.queryCutSync("阻塞模式分词"); if (wordList.constructor == Array) // just for tutorial, this is always be true  {
    wordList.forEach(function(word) { console.log(word);     
    });
}

非阻塞式调用

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

Jieba中文分词

nodejieba 官网

https://github.com/aszxqw/nodejieba

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐