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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
780c2a1639107a164a0f48820d8861e2dfd4000364d339b15dfc37b78c4d2683
|
|
| MD5 |
4c635b84f83fdec5776b9e1920c04f20
|
|
| BLAKE2b-256 |
8712fc50d773e3855be83356bd0c486e737e7fb5cdf0b493b2ade4a014e633bb
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ec3f2aef6107706d22feeea4fa426eac4e4242f57ca5eb5d0918ae892ecbd5b
|
|
| MD5 |
cb953a0e90a2e4e7a91b6a3fe647aaaa
|
|
| BLAKE2b-256 |
8d08182f42f4b7c6ccb3aa3f99b0aec2db1376dbcfab9b44a1370e6d77d450f3
|