在我认识范围内atilika完成的Kuromoji
是在日文文本分析方面的最强王者。javascript
kuromoji
是一个开源的日文词法分析的JAVA
库。html
但也有Ruby
,javascript
,go
等语言的版本,你能够在github
或则语言对应的库管理工具中找到。前端
官网有更多的信息。java
你能够用它来对日文文本进行以下的处理,包括且不只包括:node
分词git
给出读音( 包括汉字)github
词性npm
词干提取json
…...segmentfault
你也能够在kuromoji.js的playground尝试kuromoji
。
做为一名前端小卒,我就以js
版的kuromoji
来作示范吧,JAVA同胞们能够看这里JAVA简要教程。
kuromoji的npm页面在这里。
在 Node.js 中使用:
先经过npm
来安装到项目中
npm install kuromoji
再在JS文件中引用其。
var kuromoji = require("kuromoji");
接来下你能够以下来实例化一个解析器。
kuromoji.builder({ dicPath: "指定/对应的路径/到你使用/的/词典上去" }).build(function (err, tokenizer) { // tokenizer is ready var path = tokenizer.tokenize("すもももももももものうち"); console.log(path); }); /* !!注意 !! * * Kuromoji能够指定不一样的词典,所以在实例化解析器的过程当中须要指定使用 * 词典的路径,`kuromoji.builder`中的参数的`dicPath`的值应指向使用 * 得词典。 * 若在node.js中使用,通常来讲直接使用该库中的字典便可,即设置 * {dicPath:"./node_modules/kuromoji/dict/"} */
在 浏览器 中使用:
若是使用npm
安装,那么你须要使用到包中的build/kuromoji.js
和 dict/*.dat.gz
下的文件。
你能够直接使用前端模块管理Bower
来安装浏览器专用的kuromoji
。
bower install kuromoji
你也能够直接使用github、CDN等其余网络连接的方式来引入该文件。
<script src="url/to/kuromoji.js"></script>
再在你的JS中这样使用
设置路径到kuromoji.builder({ dicPath: "指定/对应的路径/到你使用/的/词典上去" }).build(function (err, tokenizer) { // tokenizer is ready var path = tokenizer.tokenize("すもももももももものうち"); console.log(path); }); /* !!注意 !! * * Kuromoji能够指定不一样的词典,所以在实例化解析器的过程当中须要指定使用 * 词典的路径,`kuromoji.builder`中的参数的`dicPath`的值应指向使用 * 得词典。 * 若在浏览器中使用,则设置路径到对应的网络位置就好。 * 好比字典放在了 http://apps.bdimg.com/libs/kuromoji.js/0.3.2/dict/ * 就设置{ dicPath:"http://apps.bdimg.com/libs/kuromoji.js/0.3.2/dict/" } */
tokenize()
函数将会返回一个以下格式的JSON数组:
[ { word_id: 509800, // 词典中的词所在ID word_type: 'KNOWN', // 单词类型(存在词典的为KNOWN,不存在的为UNKNOWN) word_position: 1, // 单词开始的位置 surface_form: '黒文字', // 单词的表面(不知什么意思) pos: '名詞', // 词性 pos_detail_1: '通常', // 词性细分类别1 pos_detail_2: '*', // 词性细分类别2 pos_detail_3: '*', // 词性细分类别3 conjugated_type: '*', // 活用型 conjugated_form: '*', // 活用形 basic_form: '黒文字', // 基本型 reading: 'クロモジ', // 阅读 pronunciation: 'クロモジ' // 发育 } ]
以处理 “世界の神” 为例子
tokenizer.tokenize("世界の神");
将会返回
[ { word_id: 2633350, word_type: 'KNOWN', word_position: 6, surface_form: '世界', pos: '名詞', pos_detail_1: '通常', pos_detail_2: '*', pos_detail_3: '*', conjugated_type: '*', conjugated_form: '*', basic_form: '世界', reading: 'セカイ', pronunciation: 'セカイ' }, { word_id: 93100, word_type: 'KNOWN', word_position: 8, surface_form: 'の', pos: '助詞', pos_detail_1: '連体化', pos_detail_2: '*', pos_detail_3: '*', conjugated_type: '*', conjugated_form: '*', basic_form: 'の', reading: 'ノ', pronunciation: 'ノ' }, { word_id: 2771160, word_type: 'KNOWN', word_position: 9, surface_form: '神', pos: '名詞', pos_detail_1: '通常', pos_detail_2: '*', pos_detail_3: '*', conjugated_type: '*', conjugated_form: '*', basic_form: '神', reading: 'カミ', pronunciation: 'カミ' }, ]
你尝试了一下,而后会发现kuromoji
虽然强大,可是有一个问题——慢。
不过别担忧,kuromojin
(node.js)能够帮你解决这个问题。
kuromojin
kuromojin
是一个对kuromoji
进行了高度包装的语法糖,而且还实现了缓冲层来保证速度。
使用kuromojin
,你能够:
不用设置词典的地址。
实例化解析器后,除了第一次以外,以后的速度会很是块。
基于Promise实现了APi。
使用方面它的文档已经很详细,RDD我就很少做介绍了。