Kin-Tokenizer
kin-tokenizer is a Python library designed for tokenizing Kinyarwanda language text, it can also tokenizer other languages like English, French or similar languages but with low compression rate since the tokenizer was trained on Kinyarwanda text only.
There is an option for training your own custom tokenizer using defined function or method. It is covered in the section of training your own tokenizer.
Hence, you can use that way to train your own tokenizer using dataset for a different language. It is based on the Byte Pair Encoding (BPE) algorithm.
It can both encode and decode text in Kinyarwanda. The metric used to measure the accuracy of this tokenizer is the compression rate and ability to encode and decode texts
Compression rate is the ratio of the total original number of characters in the text to the number of tokens in the encoded text.
Example:
For the sentence: "Nagiye gusura abanyeshuri."
- The sentence has 26 characters.
- Suppose the sentence is tokenized into the following tokens:
[23, 45, 67, 89, 23, 123, 44, 22, 55, 22, 45]. - The total number of tokens is 11.
$$ \text{Compression Rate} = \frac{26}{11} $$
So, the compression rate is 2.36X (where X indicates that the number is approximate).
Special Tokens
<|PAD|> is a special token for padding, it is represented by 0 in the vocab
<|EOS|> is a special token for indicating the end of sequence, it is represented by vocab_size - 1 in the vocab
Kin-Tokenizer 3.3
Version 3.3 has a vocabulary size of 20,257. It was the first version used for testing but had poor accuracy in tokenizing text. The compression rate was low due to a bug in the algorithm and was trained on a dataset with over 445 million characters.
Kin-Tokenizer 3.3.1
Version 3.3.1 features a vocabulary size of 4001 with improved words or subwords. It was trained on 1 million Kinyarwanda characters and shows a significantly improved compression rate compared to the previous version. The bug from the previous algorithm has been fixed.
Installation
You can install the package using pip:
pip install kin-tokenizer
Basis Usage
from kin_tokenizer import KinTokenizer # Importing Tokenizer class
# Creating an instance of tokenizer
tokenizer = KinTokenizer()
# Loading the state of the tokenizer (pretrained tokenizer)
tokenizer.load()
# Encoding
text = """
Nuko Semugeshi akunda uwo mutwe w'abatwa, bitwaga Ishabi, awugira intore ze. Bukeye
bataha biyereka ingabo; dore ko hambere nta mihamirizo yindi yabaga mu Rwanda; guhamiriza
byaje vuba biturutse i Burundi. Ubwo bataha Semugeshi n'abatware be barabitegereza basanga
ari abahanga bose, ariko Waga akaba umuhanga w'imena muri bo; nyamara muri ubwo buhanga
bwe akagiramo intege nke ku mpamvu yo kunanuka, yari afite uruti ruke."""
tokens = tokenizer.encode(text)
print(tokens)
# Calculating compression rate
text_len = len(text)
tokens_len = len(tokens)
compression_rate = text_len / tokens_len
print(f"Compression rate: {compression_rate:.2f}X")
# Creating sequences from tokens encoded from text
x_seq, y_seq = create_sequences(tokens, seq_len=20)
# Decoding
decoded_text = tokenizer.decode(tokens)
print(decoded_text)
# Decoding one sequence from created sequences
print(f"Decoded sequence:\n {tokenizer.decode(x_seq[0])}")
# Printing the vocab size
print(tokenizer.vocab_size)
# Print vocabulary (first 200 items)
count = 0
for k, v in tokenizer.vocab.items():
print("{} : {}".format(k, v))
count += 1
if count > 200:
break
Training Your Own Tokenizer
You can also train your own tokenizer using the utils module, which provides two functions: a training function and a function for creating sequences after encoding your text. N.B: Your chosen vocab_size will be met depening on the amount of data you have used for training. The vocab_size is a hyperparameter to be adjusted for better vocabularies in your vocab, and also the size of your dataset and diversity matters. The vocab is initialized by count of 256 from 1-255 unicode characters and 0 for <|PAD|> and 1 for special token <|EOS|> which marks the end of squence and it will be assigned the last token in the vocabulary.
from kin_tokenizer import KinTokenizer
from kin_tokenizer.utils import train_kin_tokenizer, create_sequences
# Training the tokenizer
tokenizer = train_kin_tokenizer(training_text, vocab_size=512, save=True, tokenizer_path=SAVE_PATH_ROOT)
# Encoding text using custom trained tokenizer
tokens = tokenizer.encode(text)
# Creating sequences
x_seq, y_seq = create_sequences(tokens, seq_len=20)
Contributing
The project is still being updated and contributions are welcome. You can contribute by:
- Reporting bugs
- Suggesting features
- Writing or improving documentation
- Submitting pull requests
Metadata
Release files for kin-tokenizer 3.3.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| kin_tokenizer-3.3.2.tar.gz | 56.0 kB | Without provenance | Details for kin_tokenizer-3.3.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| kin_tokenizer-3.3.2-py3-none-any.whl | Python 3 | none | any | Details for kin_tokenizer-3.3.2-py3-none-any.whl |
Total release size: 109.6 kB
Release files / kin_tokenizer-3.3.2.tar.gz
| Download URL | kin_tokenizer-3.3.2.tar.gz |
|---|---|
| Size | 56.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a84a8fecfe304b5e896fd75d2a51b6acd241243d908d491fdf533228a5dd02c5
|
|
BLAKE2b-256 checksum How to use checksums |
e12a3d64c2800bfb02d9d50c0f902ad6c134f4fbb7abadf6ff48eaec07305026
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.2
|
Release files / kin_tokenizer-3.3.2-py3-none-any.whl
| Download URL | kin_tokenizer-3.3.2-py3-none-any.whl |
|---|---|
| Size | 53.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5d83c532f6dc6e486ebd85036570ed50b5bb761132a9b4e785923b9b2e48b456
|
|
BLAKE2b-256 checksum How to use checksums |
f01897263163714b98a4f434aa3a4361835b3db62c72641855dc038e6010c391
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.2
|
Security for kin-tokenizer 3.3.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.