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.1.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.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tokenkit-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 780c2a1639107a164a0f48820d8861e2dfd4000364d339b15dfc37b78c4d2683
MD5 4c635b84f83fdec5776b9e1920c04f20
BLAKE2b-256 8712fc50d773e3855be83356bd0c486e737e7fb5cdf0b493b2ade4a014e633bb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tokenkit-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9ec3f2aef6107706d22feeea4fa426eac4e4242f57ca5eb5d0918ae892ecbd5b
MD5 cb953a0e90a2e4e7a91b6a3fe647aaaa
BLAKE2b-256 8d08182f42f4b7c6ccb3aa3f99b0aec2db1376dbcfab9b44a1370e6d77d450f3

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