Skip to main content

Calculate and visualize fertility and parity metrics for tokenizers

Project description

TokenKit

TokenKit is a Python library for computing fertility and parity metrics using large language model (LLM) tokenizers like LLaMA, T5, Gemma, and more. It helps you quantify and visualize how many tokens a sentence breaks into and how aligned a translation is in length across languages. Model-agnostic for any Hugging Face AutoTokenizer. Ideal for NLP evaluation, translation research, or just exploring how tokenizers behave across models.


Installation

In the terminal:

pip install tokenkit

Import:

from tokenkit import fertilize, paritize, TokenMetrics
# or
from tokenkit import *

Examples

# Fertility score: tokens per word
score, tokens = fertilize("I love classical music.")
print(score)
print(tokens)

# Parity score: token length ratio between original + translation
parity = paritize("Bonjour tout le monde", "Hello everyone")
print(parity)
# Token metrics across a dataset
import pandas as pd
df = pd.DataFrame({
    "language": ["English", "French"],
    "text": ["This is a test sentence.", "Ceci est une phrase de test."],
    "translation": ["C'est une phrase de test.", "This is a test sentence."]
})

tm = TokenMetrics(data=df)
tm.fertilize(text_col="text", language_col="language")
tm.visualize_fertilities()
tm.paritize(text_col1="text", text_col2="translation")

Note that can use any Hugging Face AutoTokenizer:

from transformers import AutoTokenizer
custom_tok = AutoTokenizer.from_pretrained("google/flan-t5-xxl")
fertilize("Tokenizing with a custom model.", tokenizer=custom_tok)

License

MIT License © Bill & Melinda Gates Foundation

Ada Zhang, Nihal Karim, Hamza Louzan, Victor Wei, Cody Carroll, Jessica Lundin

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

tokenkit-0.1.2.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

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

tokenkit-0.1.2-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file tokenkit-0.1.2.tar.gz.

File metadata

  • Download URL: tokenkit-0.1.2.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for tokenkit-0.1.2.tar.gz
Algorithm Hash digest
SHA256 c0c9249d748e5700e9bbe87ba8deca07ba2e7b86b825a19689ba891fe3137a1c
MD5 ff0115f5051719c04dbb65424678dc59
BLAKE2b-256 c6900c49ddb00b6500b04167c1b13cf08a6f6b42d83774c283e508338f30f330

See more details on using hashes here.

File details

Details for the file tokenkit-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: tokenkit-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for tokenkit-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a62ab42f2f5f37d1591ef6cb87a52751887ac3f0fba0a5ef011b1868e8aad571
MD5 cc91ea1e8c02b7c46171d7dc03130bfb
BLAKE2b-256 86a7095fa2f8afb1e830ffa20ac23feba3f5e6c01b2310b830e83de959b37472

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