Skip to main content

simple-sentencepiece

A simple sentencepiece encoder and decoder.

Note: This is not a new sentencepiece toolkit, it just uses google's sentencepiece model as input and encode the string to ids/pieces or decode the ids to string. The advantage of this tool is that it doesn't have any dependency (no protobuf), so it will be easier to integrate it into a C++ project.

Installation

pip install simple-sentencepiece

Usage

The usage is very similar to sentencepiece, it also has encode and decode interface.

from ssentencepiece import Ssentencepiece

# you can get bpe.vocab from a trained bpe model, see google's sentencepiece for details
ssp = Ssentencepiece("/path/to/bpe.vocab")

# you can also use the default models provided by this package, see below for details
ssp = Ssentencepiece("gigaspeech-500")
ssp = Ssentencepiece("zh-en-10381")

# output ids (support both str and list of strs)
# if it is list of strs, the strs are encoded in parallel
ids = ssp.encode("HELLO WORLD")
ids = ssp.encode(["HELLO WORLD", "LOVE AND PIECE"])

# output string pieces
# if it is list of strs, the strs are encoded in parallel
pieces = ssp.encode("HELLO WORLD", out_type=str)
pieces = ssp.encode(["HELLO WORLD", "LOVE AND PIECE"], out_type=str)

# decode (support list of ids or list of list of ids)
# if it is list of list of ids, the ids are decoded in parallel
res = ssp.decode([1,2,3,4,5])
res = ssp.decode([[1,2,3,4], [4,5,6,7]])

# get vocab size
res = ssp.vocab_size()

# piece to id (support both str of list of strs)
id = ssp.piece_to_id("<sos>")
ids = ssp.piece_to_id(["<sos>", "<blk>", "H", "L"])

# id to piece (support both int of list of ints)
piece = ssp.id_to_piece(5)
pieces = ssp.id_to_piece([5, 10, 15])

Default models

Model Name Description Link
alphabet-33 <blk>,<unk>, <sos>, <eos>, <pad>, ', ▁ and 26 alphabets. alphabet-33
librispeech-500 500 unigram pieces trained on Librispeech. librispeech-500
librispeech-5000 5000 unigram pieces trained on Librispeech. librispeech-5000
gigaspeech-500 500 unigram pieces trained on Gigaspeech. gigaspeech-500
gigaspeech-2000 2000 unigram pieces trained on Gigaspeech. gigaspeech-2000
gigaspeech-5000 5000 unigram pieces trained on Gigaspeech. gigaspeech-5000
zh-en-3876 3500 Chinese characters, 256 fallback bytes, 10 numbers, 10 punctuations, 100 English unigram pieces. zh-en-3876
zh-en-6876 6500 Chinese characters, 256 fallback bytes, 10 numbers, 10 punctuations, 100 English unigram pieces. zh-en-6876
zh-en-8481 8105 Chinese characters, 256 fallback bytes, 10 numbers, 10 punctuations, 100 English unigram pieces. zh-en-8481
zh-en-5776 3500 Chinese characters, 256 fallback bytes, 10 numbers, 10 punctuations, 2000 English unigram pieces. zh-en-5776
zh-en-8776 6500 Chinese characters, 256 fallback bytes, 10 numbers, 10 punctuations, 2000 English unigram pieces. zh-en-8776
zh-en-10381 8105 Chinese characters, 256 fallback bytes, 10 numbers, 10 punctuations, 2000 English unigram pieces. zh-en-10381
zh-en-yue-9761 8105 + 1280 Chinese characters(Cantonese included), 256 fallback bytes, 10 numbers, 10 punctuations, 100 English unigram pieces. zh-en-yue-9761
zh-en-yue-11661 8105 + 1280 Chinese characters(Cantonese included), 256 fallback bytes, 10 numbers, 10 punctuations, 2000 English unigram pieces. zh-en-yue-11661
chn_jpn_yue_eng_ko_spectok.bpe bpe tokens used in sensevoice ASR, support Chinese, Japanese, Cantonese, English, Korean chn_jpn_yue_eng_ko_spectok.bpe

Note: The number of 3500, 6500 and 8105 is from 通用规范汉字表.

C++ Integration

The C++ core has no protobuf dependency — only a C++14 compiler and pthread are required. This makes it easy to embed simple-sentencepiece in any existing C++ system without conflicting with other protobuf versions.

A complete integration demo with CMake build scripts is provided in example/.

Metadata

Release files for simple-sentencepiece 0.14

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for simple-sentencepiece 0.14
File Size Uploaded Provenance
simple_sentencepiece-0.14.tar.gz 1.1 MB Without provenance Details

Built distributions (wheels)

Table of built distributions (wheels) for simple-sentencepiece 0.14
File
simple_sentencepiece-0.14-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.17+ x86-64 Details
simple_sentencepiece-0.14-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.17+ x86-64 Details
simple_sentencepiece-0.14-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.17+ x86-64 Details
simple_sentencepiece-0.14-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.17+ x86-64 Details
simple_sentencepiece-0.14-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.17+ x86-64 Details
simple_sentencepiece-0.14-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.17+ x86-64 Details

Total release size: 5.4 MB

Release files / simple_sentencepiece-0.14.tar.gz

Download URL simple_sentencepiece-0.14.tar.gz
Size 1.1 MB
Tags Source
SHA-256 checksum
How to use checksums
83d808b9deeaf61d53ce3661b86aeb7a0116bb54427b9a79b11be604d333affc
BLAKE2b-256 checksum
How to use checksums
91206a94533178927056a6d4bf3253e00a86c2596b9aff44ad52f51d25ba615c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.10.12

Release files / simple_sentencepiece-0.14-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL simple_sentencepiece-0.14-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 729.1 kB
Tags CPython 3.13 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
dcabc8875df85959648b232dd2b9677e2cb224de823c5d1e6767ac7246cab500
BLAKE2b-256 checksum
How to use checksums
7c1edf292ced06a8679a50192ec305c2d21537f340529334dc3ac5cc23add5eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.10.12

Release files / simple_sentencepiece-0.14-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL simple_sentencepiece-0.14-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 729.1 kB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
1d4e8381b222f684bdd40479a5ca0973cf1f3e9e059526eaf942c1ac4a05b338
BLAKE2b-256 checksum
How to use checksums
e2f157e5d7d1cce327e8355f3d84a6325bb68e1c03887e13ad8d9cd6e86ea790
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.10.12

Release files / simple_sentencepiece-0.14-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL simple_sentencepiece-0.14-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 729.6 kB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
e369f0ef2e4c86ed9a0870ef71c326b8a82844fd6aa0799bd535cce17141c823
BLAKE2b-256 checksum
How to use checksums
62e79b0538ce1a9016c0d17ed16514280bbeeda2bd1831fdb724fe5d53185141
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.10.12

Release files / simple_sentencepiece-0.14-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL simple_sentencepiece-0.14-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 728.1 kB
Tags CPython 3.10 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
cbee6c38d7d034b4102c1500a752edef84b54328d3016472f57e34c148759445
BLAKE2b-256 checksum
How to use checksums
ce1931c90bf5114d001ab7c448653f59f8606bd55c2dfe9844947a939f0b9889
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.10.12

Release files / simple_sentencepiece-0.14-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL simple_sentencepiece-0.14-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 728.6 kB
Tags CPython 3.9 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
cf3a84793208781e2f1522455e557eb59728be3e935fd86b11be89a53d3c68fe
BLAKE2b-256 checksum
How to use checksums
062633f2c01e3fb0a1cf67ca5e033a69c77c850de2d37cf5c1d81a3c5b6c3eda
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.10.12

Release files / simple_sentencepiece-0.14-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL simple_sentencepiece-0.14-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 728.1 kB
Tags CPython 3.8 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
5a900732acdbfb49c6e9fd2aa47d0c07f82a8d1e805806e0c7b21789bb7bf6bc
BLAKE2b-256 checksum
How to use checksums
e8b7a33c01e39234251ef8f2ace1d4ae659ace042469feeb2cb15eaaa884c4db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.10.12

Security for simple-sentencepiece 0.14

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page