Skip to main content

Kanji Converter to Hiragana, Katakana, Roman alphabet

Project description

kanjiconv

Python License PyPI Downloads

Japanese REAMED is here. (日本語のREADMEはこちらです。)
https://github.com/sea-turt1e/kanjiconv/blob/main/README_ja.md

kanjiconv

Kanji Converter to Hiragana, Katakana, Roman alphabet.
You can get the reading and pronunciation of Japanese sentences based on sudachidict.
Sudachidict is a regularly updated dictionary, so it can relatively handle new proper nouns and other terms.

Environment

3.10 <= Python <= 3.13

Install

Install kanjiconv

pip install kanjiconv

If you want to use the UniDic dictionary with the use_unidic option, please download the unidic dictionary.

python -m unidic download

How to use

CLI usage

After installing kanjiconv, you can use the kanjiconv command from your terminal.

kanjiconv "幽☆遊☆白書は、最高の漫画デス。"

By default, the CLI converts text to roman alphabet and inserts a single space between token readings.

yuuyuuhakusho ha ,  saikou no manga desu .

Use -m/--mode to choose the output format:

# Convert to hiragana
kanjiconv "幽☆遊☆白書は、最高の漫画デス。" --mode hiragana

# Convert to katakana
kanjiconv "幽☆遊☆白書は、最高の漫画デス。" --mode katakana

# Convert to roman alphabet (which is already the default)
kanjiconv "幽☆遊☆白書は、最高の漫画デス。" --mode roman

Use -s/--separator to change the separator inserted between token readings:

kanjiconv "幽☆遊☆白書は、最高の漫画デス。" --mode hiragana --separator "/"
# ゆうゆうはくしょ/は/、/さいこう/の/まんが/です/。

kanjiconv "幽☆遊☆白書は、最高の漫画デス。" --mode hiragana --separator ""
# ゆうゆうはくしょは、さいこうのまんがです。

Additional options:

# Use UniDic as a fallback for readings when available
kanjiconv "東京に行く" --mode hiragana --use-unidic

# Disable the custom reading fallback
kanjiconv "激を飛ばす" --mode hiragana --no-custom-readings

CLI flags/options

Option Description
-m/--mode {roman,hiragana,katakana} Conversion mode. Defaults to roman.
-s/--separator SEPARATOR Separator inserted between token readings. Defaults to a single space.
--use-unidic Use UniDic as a fallback for readings when available.
--no-custom-readings Disable custom reading fallback.
--version Show the installed version.
-h/--help Show help.

Import & Create Instance

from kanjiconv import KanjiConv

# Basic usage
kanji_conv = KanjiConv(separator="/")

# Using UniDic for improved kanji reading accuracy
kanji_conv = KanjiConv(separator="/", use_unidic=True)

# Using custom dictionary for kanji readings not covered by SudachiDict or UniDic
kanji_conv = KanjiConv(separator="/", use_custom_readings=True)

Get Reading

# convert to hiragana
text = "幽☆遊☆白書は、最高の漫画デス。"
print(kanji_conv.to_hiragana(text))
ゆうゆうはくしょ///さいこう//まんが/です/

# convert to katakana
text = "幽☆遊☆白書は、最高の漫画デス。"
print(kanji_conv.to_katakana(text))
ユウユウハクショ///サイコウ//マンガ/デス/

# convert to Roman alphabet
text = "幽☆遊☆白書は、最高の漫画デス。"
print(kanji_conv.to_roman(text))
yuuyuuhakusho/ha/, /saikou/no/manga/desu/. 

# You can change separator to another character or None
kanji_conv = KanjiConv(separator="_")
print(kanji_conv.to_hiragana(text))
ゆうゆうはくしょ_は__さいこう_の_まんが_です_

kanji_conv = KanjiConv(separator="")
print(kanji_conv.to_hiragana(text))
ゆうゆうはくしょはさいこうのまんがです

Using Custom Kanji Reading Dictionary

KanjiConv supports a custom dictionary for handling special kanji readings that are not properly recognized by SudachiDict or UniDic. This is particularly useful for:

  1. Special expressions with unique readings
  2. Technical terms or proper nouns
  3. Ambiguous kanji with multiple readings based on context

The custom dictionary is automatically loaded from the package if available, but you can also define your own:

from kanjiconv import KanjiConv

# Create instance with custom readings enabled (enabled by default)
kanji_conv = KanjiConv(separator="/", use_custom_readings=True)

# You can also define your own custom readings
kanji_conv.custom_readings = {
    "single": {
        "激": ["げき"],
        "飛": ["と", "ひ"]
    },
    "compound": {
        "激を飛ばす": "げきをとばす",
        "飛ばす": "とばす"
    }
}

# Now the special expression will be properly converted
print(kanji_conv.to_hiragana("激を飛ばす"))
# Output: げき/を/とばす

Custom Dictionary Structure

The custom dictionary uses the following format:

  • single: A dictionary mapping individual kanji to their reading(s)
    • Each kanji can have multiple readings as a list
    • The first reading in the list is used as default
  • compound: A dictionary mapping multi-character expressions to their reading
    • These are processed before tokenization and given priority

(Optional) Installing sudachidict other than the default

The default dictionary is sudachidict_full. If you want to use a lighter dictionary, you can install either sudachidict_small or sudachidict_core.

  • If you need detailed readings, we recommend using sudachidict_full. The default is set to sudachidict_full.
  • If you prefer lighter operation, sudachidict_small is recommended.
  • sudachidict_core offers a balanced option between speed and accuracy.
pip install sudachidict_small
pip install sudachidict_core
  • If using sudachidict_small or sudachidict_core, specify it like this:
kanji_conv = KanjiConv(sudachi_dict_type="small", separator="/")
kanji_conv = KanjiConv(sudachi_dict_type="core", separator="/")

Update Dict

kanjiconv reading function is based on SudachiDict, and you need to update SudachiDict regularly via pip.

pip install -U sudachidict_full
pip install -U sudachidict_small
pip install -U sudachidict_core

Local MCP Server

If you want to use kanjiconv as a local MCP Server, see kanjicon-mcp

License

This project is licensed under the Apache License 2.0.

Open Source Software Used

This library uses SudachiPy and its dictionary SudachiDict for morphological analysis. These are also distributed under the Apache License 2.0.

For detailed license information, please refer to the LICENSE files of each project:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kanjiconv-0.2.7.tar.gz (18.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kanjiconv-0.2.7-py3-none-any.whl (18.2 kB view details)

Uploaded Python 3

File details

Details for the file kanjiconv-0.2.7.tar.gz.

File metadata

  • Download URL: kanjiconv-0.2.7.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for kanjiconv-0.2.7.tar.gz
Algorithm Hash digest
SHA256 4931509026d9c0fdec18f81c179bdefaf3e42d801c758ec7b13013ba0d37aea6
MD5 5fa66ac5987804ef219c5e0b37a2b594
BLAKE2b-256 e907dab5a40b471dcab6651c03e94f491c862b53a15874abcc0d03e4c219fa73

See more details on using hashes here.

File details

Details for the file kanjiconv-0.2.7-py3-none-any.whl.

File metadata

  • Download URL: kanjiconv-0.2.7-py3-none-any.whl
  • Upload date:
  • Size: 18.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for kanjiconv-0.2.7-py3-none-any.whl
Algorithm Hash digest
SHA256 153ba6682135d90585a97048ef00d2586944ab59e349d186a5e44db4b57f563b
MD5 2251a1681d171d4180f904d7275b8f25
BLAKE2b-256 b26c1059e764f0dacd85e0f89c6d0045392f0a7b615669f21d759f70010ada7c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page