Awesome community module

Rakuten MA

Japanese README (日本语ドキュメント)


Rakuten MA (morphological analyzer) is a morphological analyzer (word segmentor + PoS Tagger) for Chinese and Japanese written purely in JavaScript.

Rakuten MA has the following unique features:

  • Pure JavaScript implementation. Works both on modern browsers and node.js.
  • Implements a language independent character tagging model. Outputs word segmentation and PoS tags for Chinese/Japanese.
  • Supports incremental update of models by online learning (Soft Confidence Weighted, Wang et al. ICML 2012).
  • Customizable feature set.
  • Supports feature hashing, quantization, and pruning for compact model representation.
  • Bundled with Chinese and Japanese models trained from general corpora (CTB [Xue et al. 2005] and BCCWJ [Maekawa 2008]) and E-commerce corpora.


You can try Rakuten MA on the demo page . (It may take a while to load this page.)


Download & Install

Since Rakuten MA is a JavaScript library, there's no need for installation. Clone the git repository as

git clone

or download the zip archive from here:

If you have Node.js installed, you can run the demo by

node demo.js

which is identical to the usage example below.

npm package

You can also use Rakuten MA as an npm package. You can install it by:

npm install rakutenma

The model files can be found under node_modules/rakutenma/.

Usage Example (on Node.js)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
// RakutenMA demo // Load necessary libraries var RakutenMA = require('./rakutenma'); var fs = require('fs'); // Initialize a RakutenMA instance // with an empty model and the default ja feature set var rma = new RakutenMA(); rma.featset = RakutenMA.default_featset_ja; // Let's analyze a sample sentence (from // With a disastrous result, since the model is empty! console.log(rma.tokenize("彼は新しい仕事できっと成功するだろう。")); // Feed the model with ten sample sentences from var tatoeba = JSON.parse(fs.readFileSync("tatoeba.json")); for (var i = 0; i < 10; i ++) { rma.train_one(tatoeba[i]); } // Now what does the result look like? console.log(rma.tokenize("彼は新しい仕事できっと成功するだろう。")); // Initialize a RakutenMA instance with a pre-trained model var model = JSON.parse(fs.readFileSync("model_ja.json")); rma = new RakutenMA(model, 1024, 0.007812); // Specify hyperparameter for SCW (for demonstration purpose) rma.featset = RakutenMA.default_featset_ja; // Set the feature hash function (15bit) rma.hash_func = RakutenMA.create_hash_func(15); // Tokenize one sample sentence console.log(rma.tokenize("うらにわにはにわにわとりがいる")); // Re-train the model feeding the right answer (pairs of [token, PoS tag]) var res = rma.train_one( [["うらにわ","N-nc"], ["に","P-k"], ["は","P-rj"], ["にわ","N-n"], ["にわとり","N-nc"], ["が","P-k"], ["いる","V-c"]]); // The result of train_one contains: // sys: the system output (using the current model) // ans: answer fed by the user // update: whether the model was updated console.log(res); // Now what does the result look like? console.log(rma.tokenize("うらにわにはにわにわとりがいる"));

Usage Example (on browsers)

Include the following code snippet in the <head>of your HTML.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<script type="text/javascript" src="rakutenma.js" charset="UTF-8"></script> <script type="text/javascript" src="model_ja.js" charset="UTF-8"></script> <script type="text/javascript" src="hanzenkaku.js" charset="UTF-8"></script> <script type="text/javascript" charset="UTF-8"> function Segment() { rma = new RakutenMA(model); rma.featset = RakutenMA.default_featset_ja; rma.hash_func = RakutenMA.create_hash_func(15); var textarea = document.getElementById("input"); var result = document.getElementById("output"); var tokens = rma.tokenize(HanZenKaku.hs2fs(HanZenKaku.hw2fw(HanZenKaku.h2z(textarea.value)))); = 'block'; result.innerHTML = RakutenMA.tokens2string(tokens); } </script>

The analysis and result looks like this:

1 2 3
<textarea id="input" cols="80" rows="5"></textarea> <input type="submit" value="Analyze" onclick="Segment()"> <div id="output"></div>

Using bundled models to analyze Chinese/Japanese sentences

  1. Load an existing model, eg, model = JSON.parse(fs.readFileSync("model_file"));then rma = new RakutenMA(model);orrma.set_model(model);
  2. Specify featsetdepending on your langage (eg, rma.featset = RakutenMA.default_featset_zh;for Chinese and rma.featset = RakutenMA.default_featset_ja;for Japanese).
  3. Remember to use 15-bit feature hashing function ( rma.hash_func = RakutenMA.create_hash_func(15);) when using the bundled models ( model_zh.jsonand model_ja.json).
  4. Use rma.tokenize(input)to analyze your input.

Training your own analysis model from scratch

  1. Prepare your training corpus (a set of training sentences where a sentence is just an array of correct [token, PoS tag].)
  2. Initialize a RakutenMA instance with new RakutenMA().
  3. The Specify featset. (And OPTIONALLY, ctype_func, hash_func, etc.)
  4. Feed your training sentences one by one (from the first one to the last) to the train_one(sent)method.
  5. Usually SCW converges enough after one epoch(one pass through the entire training corpus) but you can repeat Step 4. to achieve even better performance.

See scripts/train_zh.js(for Chinese) and scripts/train_ja.js(for Japanese) to see an example showing how to train your own model.

Re-training an existing model (domain adaptation, fixing errors, etc.)

  1. Load an existing model and initialize a RakutenMA instance. (see "Using bundled models to analyze Chinese/Japanese sentences" above)
  2. Prepare your training data (this could be as few as a couple of sentences, depending on what and how much you want to "re-train".)
  3. Feed your training sentences one by one to the train_one(sent)method.

Reducing the model size

The model size could still be a problem for client-side distribution even after applying feature hashing. We included a script scripts/minify.jswhich applies feature quantization (see [Hagiwara and Sekine COLING 2014] for the details) to reduce the trained model size.

You can run it node scripts/minify.js [input_model_file] [output_model_file]to make a minified version of the model file. Remember: it also deletes the "sigma" part of the trained model, meaning that you are no longer able to re-train the minified model. If necessary, re-train the model first, then minify it.

API Documentation

Constructor Description
RakutenMA(model, phi, c) . Creates RakutenMA A new new instance model(optional) specifies to The Object Model instance with the initialize The RakutenMA. phiAnd c(both optional) are Hyper Parameters of the SCW (default: phi = 2048, c = 0.003906).
Methods Description
tokenize(input) Tokenizes input(string) and returns tokenized result ([token, PoS tag] pairs).
train_one(sent) The Updates The Current Model (IF Necessary) the using The GIVEN answer sent([token, the PoS Tag] pairs). Of The return value IS AN Object with Three Properties ans, sys, and updated, WHERE ansIS The GIVEN answer (Same AS sent), sysIS The System Output the using the (old) model, and updatedis a binary (True/False) flag meaning whether the model was updated (because syswas different from ans) or not.
set_model(model) Sets the Rakuten MA instance's model to model.
set_tag_scheme(scheme) Sets the sequential labeling tag scheme. Currently, "IOB2"and "SBIEO"are supported. Specifying other tag schemes causes an exception.
Properties Description
featset Specifies an array of feature templates (string) used for analysis. You can use RakutenMA.default_featset_jaand RakutenMA.default_featset_zhas the default feature sets for Japanese and Chinese, respectively. See below ("Supported feature templates") for the details of feature templates.
ctype_func Specifies the function used to convert a character to its character type. RakutenMA.ctype_ja_default_funcis the default character type function used for Japanese. Alternatively, you can call RakutenMA.create_ctype_chardic_func(chardic)to create a character type function which takes a character to look it up in chardicand return its value. (For example, RakutenMA.create_ctype_chardic_func({"A": "type1"})returns a function fwhere f("A")returns "type1"and []otherwise.)
hash_func Specifies the hash function to use for feature hashing. Default = undefined(no feature hashing). A feature hashing function with bit-bit hash space can be created by calling RakutenMA.create_hash_func(bit).

Terms and Conditions

Distribution, modification, and academic/commercial use of Rakuten MA is permitted, provided that you conform with Apache License version 2.0 .

If you are using Rakuten MA for research purposes, please cite our paper on Rakuten MA [Hagiwara and Sekine 2014]

FAQ (Frequently Asked Questions)

Q. What are supported browsers and Node.js versions?

  • A. We confirmed that Rakuten MA runs in the following environments:
    • Internet Explorer 8 (ver. 8.0.7601.17414 or above)
    • Google Chrome (ver. 35.0.1916.153 or above)
    • Firefox (ver. 16.0.2 or above)
    • Safari (ver. 6.1.5 or above)
    • Node.js (ver. 0.10.13 or above)

Q. Is commercial use permitted?

  • A. Yes, as long as you follow the terms and conditions. See "Terms and Conditions" above for the details.

Q. I found a bug / analysis error / etc. Where should I report?

  • A. Please create an issue at Github issues .
  • Alternatively, you can create a pull request if you modify the code. Rakuten MA has a test suite using Jasmine . Please make sure all the tests pass (no errors after running jasmine-node spec) and write your own ( if necessary) before submitting a pull request.
  • Finally, if your question is still not solved, please contact us at prj-rakutenma [at]

Q. Tokenization results look strange (specifically, the sentence is split up to individual characters with no PoS tags)

  • A. Check if you are using the same feature set ( featset) and the feature hashing function ( hash_func) used for training. Remember to use 15-bit feature hashing function ( rma.hash_func = RakutenMA.create_hash_func(15);) when using the bundled models ( model_zh.jsonand model_ja.json).

Q. What scripts (Simplified/Traditional) are supported for Chinese?

  • A. Currently only simplified Chinese is supported.

Q. Can we use the same model file in the JSON format for browsers?

  • A. Yes and no. Although internal data structure of models is the same, you need to add assignment (eg, var model = [JSON representation];) in order to refer to it on browsers. See the difference between model_zh.json(for Node.js) and model_zh.js(for browsers). There is a mini script scripts/convert_for_browser.jswhich does this for you. We recommend you work on Node.js for model training etc. and then convert it for browser uses.


Supported feature templates

Feature template Description
w7 Character unigram (c-3)
w8 Character unigram (c-2)
w9 Character unigram (c-1)
w0 Character unigram (c0)
w1 Character unigram (c+1)
w2 Character unigram (c+2)
w3 Character unigram (c+3)
c7 Character type unigram (t-3)
c8 Character type unigram (t-2)
c9 Character type unigram (t-1)
c0 Character type unigram (t0)
c1 Character type unigram (t+1)
c2 Character type unigram (t+2)
c3 Character type unigram (t+3)
b7 Character bigram (c-3 c-2)
b8 Character bigram (c-2 c-1)
b9 Character bigram (c-1 c0)
b1 Character bigram (c0 c+1)
b2 Character bigram (c+1 c+2)
b3 Character bigram (c+2 c+3)
d7 Character type bigram (t-3 t-2)
d8 Character type bigram (t-2 t-1)
d9 Character type bigram (t-1 t0)
d1 Character type bigram (t0 t+1)
d2 Character type bigram (t+1 t+2)
d3 Character type bigram (t+2 t+3)
others If you specify a customized feature function in the featsetarray, the function will be called with two arguments _tand i, where _tis a function which takes a position jand returns the character object at that position, and iis the current position. A character object is an object with two properties cand twhich are character and character type, respectively. The return value of that function is used as the feature value. (For example, if you specify a function f(_t, i)which returns _t(i).t;, then it's returning the character type of the current position, which is basically the same as the template c0.)

PoS tag list in Chinese

Tag Description
AD Adverb
AS Aspect Particle
BA ba3 (in ba-construction)
CC Coordinating conjunction
CD Cardinal number
CS Subordinating conjunction
DEC de5 (Complementizer/Nominalizer)
DEG de5 (Genitive/Associative)
DER de5 (Resultative)
DEV de5 (Manner)
DT Determiner
ETC Others
FW Foreign word
IJ Interjection
JJ Other noun-modifier
LB bei4 (in long bei-construction)
LC Localizer
M Measure word
MSP Other particle
NN Other noun
NN-SHORT Other noun (abbrev.)
NR Proper noun
NR-SHORT Proper noun (abbrev.)
NT Temporal noun
NT-SHORT Temporal noun (abbrev.)
OD Ordinal number
ON Onomatopoeia
P Preposition
PN Pronoun
PU Punctuation
SB bei4 (in short bei-construction)
SP Sentence-final Particle
VA Predicative adjective
VC Copula
VE you3 (Main verb)
VV Other verb
X Others

PoS tag list in Japanese and correspondence to BCCWJ tags

Tag Original JA name English
Ac Adjective-general Adjective-Common
A-dp Adjectives-not self-reliant Adjective-Dependent
C Connecting words Conjunction
D synonym Pronoun
E English English word
F adverb Adverb
Ic Touching Verbs-General Interjection-Common
Jc Shape words-general Adjectival Noun-Common
J-tari Shape word-タリ Adjectival Noun-Tari
J-xs Shape words-auxiliary verb stem Adjectival Noun-AuxVerb stem
M-aa Subsidy mark-AA Auxiliary sign-AA
Mc Subsidy sign-general Auxiliary sign-Common
M-cp Subsidy mark-closed parenthesis Auxiliary sign-Open Parenthesis
M-op Subsidy mark-open in parentheses Auxiliary sign-Close Parenthesis
Mp Subsidy mark-period Auxiliary sign-Period
Nn Noun-noun Noun-Noun
N-nc Noun-common noun Noun-Common Noun
N-pn Noun-proper noun Noun-Proper Noun
N-xs Noun-auxiliary verb stem Noun-AuxVerb stem
O その他 Others
P Joint words Prefix
P-fj Auxiliary-Adverbial Particle-Adverbial
P-jj Auxiliary Words-Quasi-Subjective Auxiliary Words Particle-Phrasal
Pk Auxiliary Words-Case Auxiliary Words Particle-Case Marking
P-rj Auxiliary Words-Auxiliary Words Particle-Binding
P-sj Auxiliary Words-Auxiliary Words Particle-Conjunctive
Qa Suffix-adjective Suffix-Adjective
Qj Suffix-shaped words Suffix-Adjectival Noun
Qn Suffix-noun Suffix-Noun
Qv Suffix-verbal Suffix-Verb
R Conjoined words Adnominal adjective
Sc Mark-general Sign-Common
Sl Mark-text Sign-Letter
Vc Verb-general Verb-Common
V-dp Verbs-Independence Possibility Verb-Dependent
W blank Whitespace
X Auxiliary verb AuxVerb


The developers would like to thank Satoshi Sekine, Satoko Marumoto, Yoichi Yoshimoto, Keiji Shinzato, Keita Yaegashi, and Soh Masuko for their contribution to this project.


Masato Hagiwara and Satoshi Sekine. Lightweight Client-Side Chinese/Japanese Morphological Analyzer Based on Online Learning. COLING 2014 Demo Session, pages 39-43, 2014. [ PDF ]

Kikuo Maekawa. Compilation of the Kotonoha-BCCWJ corpus (in Japanese). Nihongo no kenkyu (Studies in Japanese), 4(1):82–95, 2008. (Some English information can be found here .) [ Site ]

Jialei Wang, Peilin Zhao, and Steven C. Hoi. Exact soft confidence-weighted learning. In Proc. of ICML 2012, pages 121–128, 2012. [ PDF ]

Naiwen Xue, Fei Xia, Fu-dong Chiou, and Marta Palmer. The Penn Chinese treebank: Phrase structure annotation of a large corpus. Natural Language Engineering, 11(2):207–238, 2005. [ PDF ] [ Site ]

© 2014, 2015 Rakuten NLP Project. All Rights Reserved. / Sponsored by Rakuten, Inc. and Rakuten Institute of Technology .