SentencePiece is an unsupervised text tokenizer and detokenizer, primarily designed for Neural Network-based text generation systems where the vocabulary size is predetermined. It implements subword units like Byte-Pair Encoding (BPE) and Unigram Language Model, capable of training directly from raw sentences without pre-tokenization. The library is actively maintained with regular updates. The current version is 0.2.1.
pip install sentencepieceVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to train a SentencePiece model from a text file, load the trained model, and then use it to encode text into subword pieces and IDs, and decode IDs back to text. The `input` parameter for training expects a file path.
Upgrade your Python environment to 3.9 or later, or pin `sentencepiece` to `0.1.99` or an earlier compatible version.
Ensure that necessary build tools (like `cmake`, C++ compiler) and Python development headers are installed for your environment if `pip install` fails. It's often easier to use a Python version for which pre-built wheels are readily available.
Users encountering this issue with `v0.2.0` should upgrade to `v0.2.1` or a newer version, as the fix has been merged.
If using `sentencepiece` in a free-threaded environment and calling non-const methods like `load()`, ensure appropriate explicit locks are implemented to prevent data races.
Prepare your training data in a plain text file, with one sentence per line, and pass the file path to the `input` argument of `SentencePieceTrainer.train()`.
Ensure your training corpus is sufficiently large and diverse to support the desired `vocab_size`. If the corpus is intentionally small, reduce the `vocab_size` parameter in `SentencePieceTrainer.train()` to a value less than or equal to the maximum allowed size specified in the error message (e.g., `<= 33` in this case).
Run `pip install sentencepiece` in your terminal to install the library.
Verify that the `.model` file exists at the provided path, is accessible, and is a valid SentencePiece model file. Double-check the path spelling and file permissions.
Ensure your input text files are saved with UTF-8 encoding. If not possible, read the file with its correct encoding and then process the resulting string, or try specifying the encoding if the SentencePiece method supports it.
Confirm that the input file(s) specified in the `input` argument for `SentencePieceTrainer.train` contain valid text data and that their paths are correct and accessible.
No dependency data recorded yet.