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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dsquare-gbu/kuroshiro

v1.1.2-iedev-1

Published

kuroshiro is a Japanese language library for converting Japanese sentence to Hiragana, Katakana or Romaji with furigana and okurigana modes supported.

Downloads

57

Readme

kuroshiro

kuroshiro

Build Status Coverage Status npm version Join the chat at https://gitter.im/hexenq/kuroshiro License

kuroshiroは日本語文をローマ字や仮名なとに変換できるライブラリです。フリガナ・送り仮名の機能も搭載します。

ほかの言語:English, 日本語, 简体中文, 繁體中文

デモ

オンラインデモはこちらです。

特徴

  • 日本語文 => ひらがな、カタカナ、ローマ字
  • フリガナ、送り仮名サポート
  • 🆕複数の形態素解析器をサポート
  • 🆕複数のローマ字表記法をサポート
  • 実用ツール付き

バッジョン1.xでの重大な変更

  • 形態素解析器がルビロジックから分離される。それゆえ、様々な形態素解析器(レディーメイドカスタマイズも)を利用できることになります。
  • ES2017の新機能「async/await」を利用します
  • CommonJSからES Modulesへ移行します

形態素解析器プラグイン

始まる前にプラグインの適合性をチェックしてください

| 解析器 | Node.js サポート | ブラウザ サポート | レポジトリ | 開発者 | |---|---|---|---|---| |Kuromoji|✓|✓|kuroshiro-analyzer-kuromoji|Hexen Qi| |Mecab|✓|✗|kuroshiro-analyzer-mecab|Hexen Qi| |Yahoo Web API|✓|✓|kuroshiro-analyzer-yahoo-webapi|Hexen Qi|

使い方

Node.js (又はWebpackなどのモジュールバンドラを使ってる時)

npmでインストール:

$ npm install kuroshiro

kuroshiroをロードします:

ES6 Module import と CommonJS require、どちらでもOK

import Kuroshiro from "kuroshiro";

インスタンス化します:

const kuroshiro = new Kuroshiro();

形態素解析器のインスタンスを引数にしてkuroshiroを初期化する (API説明を参考にしてください):

// この例では,まずnpm installとimportを通じてkuromojiの形態素解析器を導入します
import KuromojiAnalyzer from "kuroshiro-analyzer-kuromoji";

// ...

// 初期化
// ここでasync/awaitを使ってますが, Promiseも使えます
await kuroshiro.init(new KuromojiAnalyzer());

変換の実行:

const result = await kuroshiro.convert("感じ取れたら手を繋ごう、重なるのは人生のライン and レミリア最高!", { to: "hiragana" });

ブラウザ

dist/kuroshiro.min.jsを導入し (その前にnpm installnpm run buildを通じてkuroshiro.min.jsを生成します)、そしてHTMLに:

<script src="url/to/kuroshiro.min.js"></script>

この例ではkuroshiro-analyzer-kuromoji.min.jsの導入は必要です。詳しくはkuroshiro-analyzer-kuromojiを参考にしてください

<script src="url/to/kuroshiro-analyzer-kuromoji.min.js"></script>

インスタンス化します:

var kuroshiro = new Kuroshiro();

形態素解析器のインスタンスを引数にしてkuroshiroを初期化するから,変換を実行します:

kuroshiro.init(new KuromojiAnalyzer({ dictPath: "url/to/dictFiles" }))
    .then(function () {
        return kuroshiro.convert("感じ取れたら手を繋ごう、重なるのは人生のライン and レミリア最高!", { to: "hiragana" });
    })
    .then(function(result){
        console.log(result);
    })

APIの説明

コンストラクタ

const kuroshiro = new Kuroshiro();

インスタンス関数

init(analyzer)

形態素解析器のインスタンスを引数にしてkuroshiroを初期化する。先に形態素解析器の導入と初期化は必要です。前述の形態素解析器プラグインを利用できます。形態素解析器の初期化方法は各自のドキュメントを参照してください。

引数

  • analyzer - 形態素解析器のインスタンス。

await kuroshiro.init(new KuromojiAnalyzer());

convert(str, [options])

文字列を目標音節文字に変換します(変換モードが設置できます)。

引数

  • str - 変換される文字列。
  • options - 任意 変換のパラメータ。下表の通り。

| オプション | タイプ | デフォルト値 | 説明 | |---|---|---|---| | to | String | 'hiragana' | 目標音節文字hiragana (ひらがな),katakana (カタカナ),romaji (ローマ字) | | mode | String | 'normal' | 変換モードnormal (一般),spaced (スペースで組み分け),okurigana (送り仮名),furigana (フリガナ) | | romajiSystem* | String | "hepburn" | ローマ字nippon (日本式),passport (パスポート式),hepburn (ヘボン式) | | delimiter_start | String | '(' | 区切り文字 (始め) | | delimiter_end | String | ')' | 区切り文字 (終り) |

*: 引数romajiSystemは引数toromajiに設定されてる場合にのみ有効です。詳細については, ローマ字表記法を参考にしてください。

// normal (一般)
kuroshiro.convert("感じ取れたら手を繋ごう、重なるのは人生のライン and レミリア最高!", {mode:"okurigana", to:"hiragana"});
// 結果:かんじとれたらてをつなごう、かさなるのはじんせいのライン and レミリアさいこう!
// spaced (スペースで組み分け)
kuroshiro.convert("感じ取れたら手を繋ごう、重なるのは人生のライン and レミリア最高!", {mode:"okurigana", to:"hiragana"});
// 結果:かんじとれ たら て を つなご う 、 かさなる の は じんせい の ライン   and   レミ リア さいこう !
// okurigana (送り仮名)
kuroshiro.convert("感じ取れたら手を繋ごう、重なるのは人生のライン and レミリア最高!", {mode:"okurigana", to:"hiragana"});
// 結果: 感(かん)じ取(と)れたら手(て)を繋(つな)ごう、重(かさ)なるのは人生(じんせい)のライン and レミリア最高(さいこう)!

実用ツール

const result = Kuroshiro.Util.isHiragana("あ"));

isHiragana(char)

入力文字はひらがなかどうかを判断します。

isKatakana(char)

入力文字はカタカナかどうかを判断します。

isKana(char)

入力文字は仮名かどうかを判断します。

isKanji(char)

入力文字は漢字かどうかを判断します。

isJapanese(char)

入力文字は日本語かどうかを判断します。

hasHiragana(str)

入力文字列にひらがながあるかどうかを確認する。

hasKatakana(str)

入力文字列にカタカナがあるかどうかを確認する。

hasKana(str)

入力文字列に仮名があるかどうかを確認する。

hasKanji(str)

入力文字列に漢字があるかどうかを確認する。

hasJapanese(str)

入力文字列に日本語があるかどうかを確認する。

kanaToHiragna(str)

入力仮名文字列をひらがなへ変換します。

kanaToKatakana(str)

入力仮名文字列をカタカナへ変換します。

kanaToRomaji(str, system)

入力仮名文字列をローマ字へ変換します。引数systemの指定可能値は"nippon", "passport", "hepburn" (デフォルト値: "hepburn")

ローマ字表記法

kuroshiroは三種類のローマ字表記法をサポートします。

nippon: 日本式ローマ字。ISO 3602 Strict を参照。

passport: パスポート式ローマ字。 日本外務省が発表した ヘボン式ローマ字綴方表 を参照。

hepburn: ヘボン式ローマ字。BS 4812 : 1972 を参照。

各種ローマ字表の比較はこちらを参考にしてください。

ローマ字変換のお知らせ

フリガナは音声を正確にあらわしていないため、フリガナローマ字 に完全自動的に変換することは不可能です。(なぜフリガナではダメなのか?を参照)

そのゆえ、nipponhepburnのローマ字表記法を使って、フリガナ(仮名)-> ローマ字 変換を行うとき、kuroshiroは長音の処理を実行しません。(passport表記法そのものが長音を無視します)

例えばnippon passporthepburnのローマ字表記法を使って フリガナ->ローマ字 変換を行うと、それぞれ"kousi"、 "koshi"、 "koushi"が得られます。

フリガナモードを使うかどうかにかかわらず、漢字->ローマ字の変換はこの仕組みに影響を与えられないです。

貢献したい方

CONTRIBUTING を参考にしてみてください。

感謝

  • kuromoji
  • wanakana

ライセンス

MIT