kakasi
is a Rust library to transliterate hiragana, katakana and kanji (Japanese text) into rōmaji (Latin/Roman alphabet).
It was ported from the pykakasi library which itself is a port of the original kakasi library written in C.
Transliterate:
rust
let res = kakasi::convert("こんにちは世界!");
assert_eq!(res.hiragana, "こんにちはせかい!");
assert_eq!(res.romaji, "konnichiha sekai!");
Check if a string contains Japanese characters:
```rust use kakasi::IsJapanese;
asserteq!(kakasi::isjapanese("Abc"), IsJapanese::False); asserteq!(kakasi::isjapanese("日本"), IsJapanese::Maybe); asserteq!(kakasi::isjapanese("ラスト"), IsJapanese::True); ```
```sh $ cargo install kakasi
$ kakasi こんにちは世界! konnichiha sekai!
$ kakasi -k こんにちは世界! こんにちはせかい!
$ kakasi -f rust_article.txt
$ echo "こんにちは世界!" | kakasi ```
CPU: AMD Ryzen 7 5700G
| Text | Conversion time | Speed | | -------------------------------- | --------------- | ---------- | | Sentence (161 B) | 7.0911 µs | 22.70 MB/s | | Rust wikipedia article (31705 B) | 1.5055 ms | 21.06 MB/s |
Time to convert a 100KB test file using the CLI:
| Library | Time | Speed | | ---------------------------------------------------------- | -------- | ---------- | | kakasi (Rust) | 7.4 ms | 13.5 MB/s | | kakasi (C) | 33.5 ms | 2.99 MB/s | | pykakasi (Python) | 810.6 ms | 0.123 MB/s |
Test commands:
CLI performance was measured with hyperfine.
sh
hyperfine --warmup 3 'cat 100K.txt | kakasi-rs'
hyperfine --warmup 3 'cat 100K.txt | kakasi -i utf-8 -Ka -Ha -Ja -Sa -s'
hyperfine --warmup 3 'cat 100K.txt | python bin/kakasi -Ka -Ha -Ja -Sa -s'
kakasi is published under the GNU GPL-3.0 license.
The Kakasi dictionaries (Files: codegen/dict/kakasidict.utf8
, codegen/dict/itajidict.utf8
,
codegen/dict/hepburn.utf8
)
were taken from the pykakasi project,
published under the GNU GPL-3.0 license.
pykakasi
Copyright (C) 2010-2021 Hiroshi Miura and contributors(see AUTHORS)
The dictionaries originate from the kakasi project, published under the GNU GPL-2.0 license.
original kakasi
Copyright (C) 1992 1993 1994
Hironobu Takahashi (takahasi@tiny.or.jp),
Masahiko Sato (masahiko@sato.riec.tohoku.ac.jp),
Yukiyoshi Kameyama, Miki Inooka, Akihiko Sasaki, Dai Ando, Junichi Okukawa,
Katsushi Sato and Nobuhiro Yamagishi
For testing I included a copy of the Japanese Rust wikipedia article
(tests/rust_article.txt
). The article is published under the Creative Commons
Attribution-ShareAlike License 3.0.