Skip to main content

Python implementation of kakasi - kana kanji simple inversion library

Project description

Pykakasi

Overview

Documentation Status PyPI version Travis-CI Azure-Pipelines Coverage status

pykakasi is a Python Natural Language Processing (NLP) library to transliterate hiragana, katakana and kanji (Japanese text) into rōmaji (Latin/Roman alphabet). It can handle characters in NFC form.

It is based on the kakasi library, which is written in C.

Supported python versions

  • pykakasi 1.2 supports python 2.7, python 3.5, 3.6, 3.7

  • pykakasi 2.0 supports python 3.6, 3.7, 3.8, pypy3.6-7.1.1

Usage

Here is an usage of NewAPI for pykakasi v2.0.0 and later. Transliterate Japanese text to kana, hiragana and romaji:

import pykakasi
kks = pykakasi.kakasi()
text = "かな漢字"
result = kks.convert(text)
for item in result:
    print("{}: kana '{}', hiragana '{}', romaji: '{}'".format(item['orig'], item['kana'], item['hira'], item['hepburn']))

かな: kana 'カナ', hiragana: 'かな', romaji: 'kana'
漢字: kana 'カンジ', hiragana: 'かんじ', romaji: 'kanji'

Here is an example that output as similar with furigana mode.

import pykakasi
kks = pykakasi.kakasi()
text = "かな漢字交じり文"
result = kks.convert(text)
for item in result:
    print("{}[{}] ".format(item['orig'], item['hepburn'].capitalize()), end='')
print()

かな[Kana] 漢字[Kanji] 交じり[Majiri] [Bun]

Old API

There is also an old API for v1.2.

Transliterate Japanese text to rōmaji:

>>> import pykakasi
>>>
>>> text = u"かな漢字交じり文"
>>> kakasi = pykakasi.kakasi()
>>> kakasi.setMode("H","a") # Hiragana to ascii, default: no conversion
>>> kakasi.setMode("K","a") # Katakana to ascii, default: no conversion
>>> kakasi.setMode("J","a") # Japanese to ascii, default: no conversion
>>> kakasi.setMode("r","Hepburn") # default: use Hepburn Roman table
>>> kakasi.setMode("s", True) # add space, default: no separator
>>> kakasi.setMode("C", True) # capitalize, default: no capitalize
>>> conv = kakasi.getConverter()
>>> result = conv.do(text)
>>> print(result)
kana Kanji Majiri Bun

Tokenize Japanese text (split by word boundaries), equivalent to kakasi’s wakati gaki option:

>>> wakati = pykakasi.wakati()
>>> conv = wakati.getConverter()
>>> result = conv.do(text)
>>> print(result)
かな 漢字 交じり 文

Add furigana (pronounciation aid) in rōmaji to text:

>>> kakasi = pykakasi.kakasi()
>>> kakasi.setMode("J","aF") # Japanese to furigana
>>> kakasi.setMode("H","aF") # Japanese to furigana
>>> conv = kakasi.getConverter()
>>> result = conv.do(text)
>>> print(result)
かな[kana] 漢字[Kanji] 交じり[Majiri] 文[Bun]

Input mode values: “J” (Japanese: kanji, hiragana and katakana), “H” (hiragana), “K” (katakana).

Output mode values: “H” (hiragana), “K” (katakana), “a” (alphabet / rōmaji), “aF” (furigana in rōmaji).

There are other setMode switches which control output:

  • “r”: Romanisation table: Hepburn (default), Kunrei or Passport

  • “s”: Separator: False adds no spaces between words (default), True adds spaces between words

  • “C”: Capitalize: False adds no capital letters (default), True makes each word start with a capital letter

PyKakasi ChangeLog

All notable changes to this project will be documented in this file.

Unreleased

Added

Changed

Fixed

Deprecated

Removed

Security

v2.0.3 (25, Nov. 2020)

Fixed

  • CLI: Fix -v and -h option crash (#108).

v2.0.2 (23, Jul. 2020)

Fixed

  • Fix convert() to handle Katakana correctly.(#103)

v2.0.1 (23, Jul. 2020)

Changed

  • Update setup.py, setup.cfg, tox.ini(#102)

Fixed

  • Fix convert() misses last part of a text (#99, #100)

  • Fix CI, coverage, and coveralls configurations(#101)

v2.0.0 (31, May. 2020)

Changed

  • Update test formatting.

v2.0.0b1 (9, May. 2020)

Changed

  • Update test.

v2.0.0a6 (30, Mar. 2020)

Added

  • Understand more kanji variations.

Fixed

  • Fix IVS handling to return correct word length to consume.

v2.0.0a5 (23, Mar. 2020)

Changed

  • Recognize UNICODE standard Ideographic Variation Selector(IVS) and transiliterate when used.(#97)

v2.0.0a4 (20, Mar. 2020)

Added

  • Add type hinting.

Changed

  • Refactoring dictionary generation classes.

  • call super() from wakati.__init__()

  • test: detection whether tox or raw pytest by TOX_ENV environment variable. When raw pytest, generate dictionaries as fixture. Previous versions uses –runenv option for pytest.

Fixed

  • NewAPI: fix return value when empty input string.

v2.0.0a3 (18, Mar. 2020)

Changed

  • Update test cases.

Fixed

  • Add guard for unknown symbol code point which lead NoneType error.

v2.0.0a2 (16, Mar. 2020)

Added

  • NewAPI: support kunrei and passport roman conversion rule.

Changed

  • CI: test by github actions

Fixed

  • Support an extended kana(#77) (U0001b150-U0001b152, U0001b164-U0001b167)

v2.0.0a1 (14, Mar. 2020)

Added

  • Structured interface of Kakasi class.(#21)

Changed

  • Github workflows for packaging and release.(#91)

Fixed

  • fix data kakasidict.utf8: “本蓮沼”

Deprecated

  • Drop python 2.7 support.

v1.2 (26, Sep, 2019)

Fixed

  • Fix out-of-index error when kana-dash is placed on first of same character group.(#85)

v1.1 (16, Sep, 2019)

v1.1b2 (14, Sep, 2019)

Fixed

  • Fix Long symble issue(#58) (thanks @northernbird and @ta9ya)

v1.1b1 (6, Sep, 2019)

Added

  • Add conversions: kya, kyu, kyo

Changed

  • Rewording README document

v1.1a1 (8, Jul, 2019)

Changed

  • pytest: now run on project root without tox, by generating dictionary as a test fixture.

  • tox: run tox test with installed dictionary instead of a generated fixture.

  • Optimize kana conversion function.

  • Move kakasidict.py to src and conftest.py to tests

Fixed

  • Version naming follows PEP386.

  • Sometimes fails to insert space after punctuation(#79).

  • Special case in kana-roman passport conversion such as ‘etchu’ etc.

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

pykakasi-2.0.3.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

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

pykakasi-2.0.3-py3-none-any.whl (2.4 MB view details)

Uploaded Python 3

File details

Details for the file pykakasi-2.0.3.tar.gz.

File metadata

  • Download URL: pykakasi-2.0.3.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.6

File hashes

Hashes for pykakasi-2.0.3.tar.gz
Algorithm Hash digest
SHA256 50e78c36540c756566cf79cfc1a48b67cba3fc28b0b77972774ffb35a455a944
MD5 ff2aa2c4b83ba9cd166fe3374b6dabce
BLAKE2b-256 e82389b14ab0f2abc2f5c51551b78d5d894b40be38dbf7eeba13f08e954129d0

See more details on using hashes here.

File details

Details for the file pykakasi-2.0.3-py3-none-any.whl.

File metadata

  • Download URL: pykakasi-2.0.3-py3-none-any.whl
  • Upload date:
  • Size: 2.4 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.6

File hashes

Hashes for pykakasi-2.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 57995910e2d2a000e02e999a358597ee9fdd18b2c99839c1593edbaf561e89ab
MD5 4581af47201d73a9a870a689515b3a55
BLAKE2b-256 f01a1749afe5fa4913135273aa4d50e918cc0fbdeb769878bee13130ba0101b0

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