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/.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simple_sentencepiece-0.14.tar.gz (1.1 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

simple_sentencepiece-0.14-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (729.1 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

simple_sentencepiece-0.14-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (729.1 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

simple_sentencepiece-0.14-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (729.6 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

simple_sentencepiece-0.14-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (728.1 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

simple_sentencepiece-0.14-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (728.6 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

simple_sentencepiece-0.14-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (728.1 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

Details for the file simple_sentencepiece-0.14.tar.gz.

File metadata

  • Download URL: simple_sentencepiece-0.14.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.12

File hashes

Hashes for simple_sentencepiece-0.14.tar.gz
Algorithm Hash digest
SHA256 83d808b9deeaf61d53ce3661b86aeb7a0116bb54427b9a79b11be604d333affc
MD5 42db545c03dce997b31c90ec794845a5
BLAKE2b-256 91206a94533178927056a6d4bf3253e00a86c2596b9aff44ad52f51d25ba615c

See more details on using hashes here.

File details

Details for the file simple_sentencepiece-0.14-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for simple_sentencepiece-0.14-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 dcabc8875df85959648b232dd2b9677e2cb224de823c5d1e6767ac7246cab500
MD5 d39e8cedc335f2f2107ca180894941cc
BLAKE2b-256 7c1edf292ced06a8679a50192ec305c2d21537f340529334dc3ac5cc23add5eb

See more details on using hashes here.

File details

Details for the file simple_sentencepiece-0.14-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for simple_sentencepiece-0.14-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1d4e8381b222f684bdd40479a5ca0973cf1f3e9e059526eaf942c1ac4a05b338
MD5 872a9969c7e6a4a215dba5b1d820f18e
BLAKE2b-256 e2f157e5d7d1cce327e8355f3d84a6325bb68e1c03887e13ad8d9cd6e86ea790

See more details on using hashes here.

File details

Details for the file simple_sentencepiece-0.14-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for simple_sentencepiece-0.14-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e369f0ef2e4c86ed9a0870ef71c326b8a82844fd6aa0799bd535cce17141c823
MD5 71d77f6341751cb69f44000dda3ab890
BLAKE2b-256 62e79b0538ce1a9016c0d17ed16514280bbeeda2bd1831fdb724fe5d53185141

See more details on using hashes here.

File details

Details for the file simple_sentencepiece-0.14-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for simple_sentencepiece-0.14-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cbee6c38d7d034b4102c1500a752edef84b54328d3016472f57e34c148759445
MD5 fc8ebb97aa612a79940b9bd8676f28e2
BLAKE2b-256 ce1931c90bf5114d001ab7c448653f59f8606bd55c2dfe9844947a939f0b9889

See more details on using hashes here.

File details

Details for the file simple_sentencepiece-0.14-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for simple_sentencepiece-0.14-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cf3a84793208781e2f1522455e557eb59728be3e935fd86b11be89a53d3c68fe
MD5 2b98defd3d8818c91b9ce60bf6880958
BLAKE2b-256 062633f2c01e3fb0a1cf67ca5e033a69c77c850de2d37cf5c1d81a3c5b6c3eda

See more details on using hashes here.

File details

Details for the file simple_sentencepiece-0.14-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for simple_sentencepiece-0.14-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5a900732acdbfb49c6e9fd2aa47d0c07f82a8d1e805806e0c7b21789bb7bf6bc
MD5 88e83ed985750ef6e8b1dd110d7c10fd
BLAKE2b-256 e8b7a33c01e39234251ef8f2ace1d4ae659ace042469feeb2cb15eaaa884c4db

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.14 This release

7 files

0.13

7 files

0.12

7 files

0.11

7 files

0.10

7 files

0.9

7 files

0.8

13 files

0.7

7 files

0.6

13 files

0.5

7 files

0.4

7 files

0.3

7 files

0.2

19 files

0.1

16 files

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