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.3.tar.gz (11.0 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.3-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tokenkit-0.1.3.tar.gz
  • Upload date:
  • Size: 11.0 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.3.tar.gz
Algorithm Hash digest
SHA256 e4f52f19bf73fddc12674d8e9eee290b2882f266fd40dcb39eb46f7648ae2d00
MD5 caf382a93442c3571c9f40a81d38454a
BLAKE2b-256 77be011ba7ef5f63f40c29c68245687fa9c3f544f0eb44549265c1f2a9560fa6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tokenkit-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 4.9 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6d39dcc69dde0e9a831c71e4020aa25f93e942a8a3d70c229904d352d5d78093
MD5 364d9d6d3a947b87a414d45a4cad9319
BLAKE2b-256 c28fc0df4823085c0dff4d6fda22b056d6d54403525aa5d52329d13f77a9c141

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