Skip to main content

grapheme-kit

Grapheme-aware evaluation metrics and text processing utilities for any language.

PyPI Python License: MIT

Live Demo · Documentation


Why grapheme-kit?

Unicode text is complex: a single visible character (grapheme cluster) often spans multiple code points. Naive string operations get lengths, distances, and evaluation metrics wrong when combining marks, diacritics, or conjuncts are present.

grapheme-kit operates at the grapheme-cluster level, ensuring all measurements reflect human-perceived text structure, not raw byte or code point counts.

from grapheme_kit import Graphemizer

# Burmese
g = Graphemizer("ကျွန်ုပ်")
len("ကျွန်ုပ်")     # 8 code points (naive)
len(g)           # 3 grapheme clusters (correct)

# Bengali
g = Graphemizer("নমস্কার")
len("নমস্কার")    # 5 code points (naive)
len(g)           # 7 grapheme clusters (correct)

Installation

pip install grapheme-kit
# development
git clone https://github.com/vmenan/grapheme-kit.git
cd grapheme-kit
uv sync

Features

Segmentation

Split text from any script into correct grapheme clusters:

from grapheme_kit import Graphemizer

# Hindi
g = Graphemizer("किताब")
g.graphemes   # ['कि', 'ता', 'ब']
len(g)        # 3

# Burmese
g = Graphemizer("ကျွန်ုပ်")
g.graphemes   # ['ကျွ', 'န်ု', 'ပ်']
len(g)        # 3

String Distance

Grapheme-aware implementations of popular distance and similarity algorithms:

from grapheme_kit import levenshtein
from grapheme_kit.distance import jaro_winkler, damerau_levenshtein

# Hebrew
levenshtein("שָׁלוֹם", "שָׁלוֹב")          # 1 (only one cluster differs)

# Arabic 
levenshtein("مُعَلِّمٌ", "مُعَلِّمُ")          # 1 (diacritic-level edit, one grapheme)

# Hindi
levenshtein("किताब", "कताब")          # 1 (one grapheme cluster removed)

# Latin/English
levenshtein("kitten", "sitting")       # 3
jaro_winkler("martha", "marhta")       # 0.9611

Evaluation Metrics

Compute machine translation or text generation metrics based on grapheme clusters rather than character code points:

from grapheme_kit.metric import GraphemeCHRF, CER, charbleu

# chrF
GraphemeCHRF().sentence_score("நல்ல", ["நல்ல மாணவன்"]).score  # 37.1051

# Character Error Rate (CER)
CER("كِتَابٌ", "كِتَابَ")  # 0.25

# CharBLEU
charbleu("the quick brown fox", "the quick red fox")  # 0.7086

Decompose / Compose

Phonetic decomposition and composition (currently supported for select Indic scripts like Tamil and Sinhala):

from grapheme_kit import decompose, compose

decompose("කා")                   # 'ක්ආ'
compose("ක්ආ")                    # 'කා'
compose(decompose("வணக்கம்")) == "வணக்கம்"  # True

Command Line

The package exposes a grapheme-kit executable with a short alias gkit:

gkit graphemize "مَرْحَبًا" --count
gkit graphemize "किताब" --count
gkit distance "שָׁלוֹם" "שָׁלוֹב" --level both
gkit evaluate "நல்ல மாணவன்" "நல்ல" --metric chrf
gkit decompose "ලංකාව" --round-trip

Use gkit --help or gkit <command> --help for the full list of commands and options.


Documentation

Full documentation, guides, and API reference: grapheme-kit-docs.pages.dev

Try it interactively in the live demo.


Contributing

Issues and pull requests are welcome. Please open an issue first for major changes.

License

MIT © grapheme-kit contributors. See LICENSE for details.

Download files

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

Source Distribution

grapheme_kit-0.1.1.tar.gz (17.2 kB view details)

Uploaded Source

Built Distribution

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

grapheme_kit-0.1.1-py3-none-any.whl (22.2 kB view details)

Uploaded Python 3

File details

Details for the file grapheme_kit-0.1.1.tar.gz.

File metadata

  • Download URL: grapheme_kit-0.1.1.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for grapheme_kit-0.1.1.tar.gz
Algorithm Hash digest
SHA256 20fd1f96b4a8ccb76467a91bbe3959650969ba4b92908e6cb5d4147aa798cb1e
MD5 ae6223519122e36c9801cbc15fa2e06a
BLAKE2b-256 6a95348bbf2462e59252e920ff34bf52e950b6a3f5d075c5d0993b15e4b6b69b

See more details on using hashes here.

File details

Details for the file grapheme_kit-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: grapheme_kit-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 22.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for grapheme_kit-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 217b6d496335290d73293e967981d3591cf46d0a87191763c7120b37e86af2c5
MD5 a73e6fcad526535fa5e0ccdbe1349f14
BLAKE2b-256 704b8f8f7613b2d597af941b974cb3ab2dea3e48a5bd7d9177492562e5206661

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page