npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

node-jptokenizer

v0.1.12

Published

Beta version

Readme

node-jptokenizer

Pure JS Japanese tokenizer.

特徴

mecabに色々不満があって作ったので、殆どの特徴はmecabに対する不満そのものです。

Pure JS の形態素解析器

npm install 一発でインストールでき、JITのパフォーマンスを期待した作りになっています。

辞書に忠実

mecabで一番厄介なのは、辞書のカスタマイズです。 mecabは複雑な接続スコアにより精度を上げているため、辞書を編集しても意図した結果を得る事が困難なのです。

当tokenizerは辞書をカスタマイズした際に結果が想像し易い事を目指して作られています。

  1. 基本的には最長マッチ
  2. 多少の接続パターンは実装しているが、助詞や接続詞等の最小限の判別のみ
  3. Ascii文字は全てunicodeの日本語領域に寄せて扱われます。(AはAとみなされる)

結果として品詞判定の精度は悪くなりますがトレードオフです。

辞書の種類

JSONファイル

スタンドアローン用にはJSONファイルによる辞書が使えます。

https://raw.githubusercontent.com/crumbjp/momonger/master/test/fixture/dictionary.json

MongoDB baseの辞書

collection そのものを辞書として使う事が出来ます。

MongoDB baseの辞書では単語毎にユニークなID(_id field)が振られます。 そのため、通常必要な、単語 => IDの変換処理をせずに辞書のIDをそのまま使ってベクトル化できます。

よって同じ辞書を使ってtokenizeした結果は、いつでも互換性を保ったID化が出来ます。

※ 詳細は後述

未知語の扱い

カタカナ・アルファベット・数値・日付は自動的に単語(未知語)として扱われます。

MongoDB baseの辞書の場合は未知語として保存され、以降のtokenizeで利用されます。 これにより、tokenize結果に未知語を含めたベクトルが生成できます。

※ JSON辞書を使用している場合は、このような更新処理はしません。

クイックスタート

mkdir -p /tmp/jptokenizer
cd /tmp/jptokenizer
npm install node-jptokenizer
wget https://raw.githubusercontent.com/crumbjp/momonger/master/test/fixture/dictionary.json
echo 'var JPTokenizer = require("node-jptokenizer");
var tokenizer = new JPTokenizer({
  type: "file",
  file: {
    path: "dictionary.json"
  }
});
tokenizer.modifier = function(token, candidate, done) {
  if ( candidate.t.indexOf("名詞") >= 0 ) {
    return done(null, token);
  }
  done(null, null);
}
tokenizer.init(function(){
 tokenizer.parse_doc("本日は晴天なり", function(err, tokens){
    for (var i in tokens) {
      var token = tokens[i];
      console.log(token.i + " : " + token.w);
    }
  });
});' > test.js
node test.js

Reference

constructor ( config )

config:
  type          : <string>: "file" or "mongo"
  file          : when specify type == "file"
    path        : <path to JSON dictionary>
  mongo         : when specify type == "mongo"
    host        :
    port        :
    authdbname  : specify null when no auth
    user        :
    password    :
    database    : dictionary DB
    collection  : dictionary Collection

init

Must call before calling parse_doc

parse_doc (sentence, callback)

sentence: <target sentence>

callback  : function(err, tokens)
  tokens  : token array

token     :
  i       : word index in the document
  w       : word
  c       : dictionaryId

modifier (token, candidate, callback)

token     : See bellow

candidate : Dictionary element
  _id     : elementID
  t       : Part of speech array
  w       : word

example

var JPTokenizer = require("node-jptokenizer");
var tokenizer = new JPTokenizer({
  type: "file",
  file: {
    path: "conf/dictionary.json"
  }
});
tokenizer.modifier = function(token, candidate, done) {
  if ( candidate.t.indexOf("名詞") >= 0 ) {
    return done(null, token);
  }
  done(null, null);
}
tokenizer.init(function(){
 tokenizer.parse_doc("本日は晴天なり", function(err, tokens){
    for (var i in tokens) {
      var token = tokens[i];
      console.log(token.i + " : " + token.w);
    }
  });
});