kuroshiro is a Japanese language library for converting Japanese sentence to Hiragana, Katakana or Romaji with furigana and okurigana modes supported.
Read this in other languages: English, 日本語, 简体中文, 繁體中文.
You can check the demo here.
- Japanese Sentence => Hiragana, Katakana or Romaji
- Furigana and okurigana supported
- 🆕Multiple morphological analyzers supported
- 🆕Multiple romanization systems supported
- Useful Japanese utils
- Seperate morphological analyzer from phonetic notation logic to make it possible that we can use different morphological analyzers (ready-made or customized)
- Embrace ES8/ES2017 to use async/await functions
- Use ES6 Module instead of CommonJS
You should check the environment compatibility of each analyzer before you start working with them
Analyzer | Node.js Support | Browser Support | Plugin Repo | Developer |
---|---|---|---|---|
Kuromoji | ✓ | ✓ | kuroshiro-analyzer-kuromoji | Hexen Qi |
Mecab | ✓ | ✗ | kuroshiro-analyzer-mecab | Hexen Qi |
Yahoo Web API | ✓ | ✓ | kuroshiro-analyzer-yahoo-webapi | Hexen Qi |
Install with npm package manager:
$ npm install kuroshiro
Load the library:
Support both ES6 Module import
and CommonJS require
import Kuroshiro from "kuroshiro";
Instantiate:
const kuroshiro = new Kuroshiro();
Initialize kuroshiro with an instance of analyzer (You could check the apidoc for more information):
// For this example, you should npm install and import the kuromoji analyzer first
import KuromojiAnalyzer from "kuroshiro-analyzer-kuromoji";
// ...
// Initialize
// Here uses async/await, you could also use Promise
await kuroshiro.init(new KuromojiAnalyzer());
Convert what you want:
const result = await kuroshiro.convert("感じ取れたら手を繋ごう、重なるのは人生のライン and レミリア最高!", { to: "hiragana" });
Add dist/kuroshiro.min.js
to your frontend project (you may first build it from source with npm run build
after npm install
), and in your HTML:
<script src="url/to/kuroshiro.min.js"></script>
For this example, you should also include kuroshiro-analyzer-kuromoji.min.js
which you could get from kuroshiro-analyzer-kuromoji
<script src="url/to/kuroshiro-analyzer-kuromoji.min.js"></script>
Instantiate:
var kuroshiro = new Kuroshiro();
Initialize kuroshiro with an instance of analyzer, then convert what you want:
kuroshiro.init(new KuromojiAnalyzer({ dictPath: "url/to/dictFiles" }))
.then(function () {
return kuroshiro.convert("感じ取れたら手を繋ごう、重なるのは人生のライン and レミリア最高!", { to: "hiragana" });
})
.then(function(result){
console.log(result);
})
Examples
const kuroshiro = new Kuroshiro();
Initialize kuroshiro with an instance of analyzer. You should first import an analyzer and initialize it. You can make use of the Ready-made Analyzers listed above. And please refer to documentation of analyzers for analyzer initialization instructions
Arguments
analyzer
- An instance of analyzer.
Examples
await kuroshiro.init(new KuromojiAnalyzer());
Convert given string to target syllabary with options available
Arguments
str
- A String to be converted.options
- Optional kuroshiro has several convert options as below.
Options | Type | Default | Description |
---|---|---|---|
to | String | "hiragana" | Target syllabary [hiragana , katakana , romaji ] |
mode | String | "normal" | Convert mode [normal , spaced , okurigana , furigana ] |
romajiSystem* | String | "hepburn" | Romanization system [nippon , passport , hepburn ] |
delimiter_start | String | "(" | Delimiter(Start) |
delimiter_end | String | ")" | Delimiter(End) |
*: Param romajiSystem
is only applied when the value of param to
is romaji
. For more about it, check Romanization System
Examples
// normal
await kuroshiro.convert("感じ取れたら手を繋ごう、重なるのは人生のライン and レミリア最高!", {mode:"okurigana", to:"hiragana"});
// result:かんじとれたらてをつなごう、かさなるのはじんせいのライン and レミリアさいこう!
// spaced
await kuroshiro.convert("感じ取れたら手を繋ごう、重なるのは人生のライン and レミリア最高!", {mode:"okurigana", to:"hiragana"});
// result:かんじとれ たら て を つなご う 、 かさなる の は じんせい の ライン and レミ リア さいこう !
// okurigana
await kuroshiro.convert("感じ取れたら手を繋ごう、重なるのは人生のライン and レミリア最高!", {mode:"okurigana", to:"hiragana"});
// result: 感(かん)じ取(と)れたら手(て)を繋(つな)ごう、重(かさ)なるのは人生(じんせい)のライン and レミリア最高(さいこう)!
// furigana await kuroshiro.convert("感じ取れたら手を繋ごう、重なるのは人生のライン and レミリア最高!", {mode:"furigana", to:"hiragana"}); // result: 感じ取れたら手を繋ごう、重なるのは人生のライン and レミリア最高!
Examples
const result = Kuroshiro.Util.isHiragana("あ"));
Check if input char is hiragana.
Check if input char is katakana.
Check if input char is kana.
Check if input char is kanji.
Check if input char is Japanese.
Check if input string has hiragana.
Check if input string has katakana.
Check if input string has kana.
Check if input string has kanji.
Check if input string has Japanese.
Convert input kana string to hiragana.
Convert input kana string to katakana.
Convert input kana string to romaji. Param system
accepts "nippon"
, "passport"
, "hepburn"
(Default: "hepburn").
kuroshiro supports three kinds of romanization systems.
nippon
: Nippon-shiki romanization. Refer to ISO 3602 Strict.
passport
: Passport-shiki romanization. Refer to Japanese romanization table published by Ministry of Foreign Affairs of Japan.
hepburn
: Hepburn romanization. Refer to BS 4812 : 1972.
There is a useful webpage for you to check the difference between these romanization systems.
Since it's impossible to fully automatically convert furigana directly to romaji because furigana lacks information on pronunciation (Refer to なぜ フリガナでは ダメなのか?).
kuroshiro will not handle chōon when processing directly furigana (kana) -> romaji conversion with every romanization system (Except that Chōonpu will be handled)
For example, you'll get "kousi", "koushi", "koushi" respectively when converts kana "こうし" to romaji
using nippon
, passport
, hepburn
romanization system.
The kanji -> romaji conversion with/without furigana mode is unaffected by this logic.
Please check CONTRIBUTING.
- kuromoji
- wanakana
MIT