26 releases

0.13.1 Jan 17, 2026
0.12.0 Aug 13, 2025
0.11.3 Apr 21, 2025
0.11.2 Jul 22, 2023
0.1.3 Feb 7, 2020

#223 in Encoding

Download history 1684/week @ 2025-11-22 1693/week @ 2025-11-29 1929/week @ 2025-12-06 2178/week @ 2025-12-13 1273/week @ 2025-12-20 1016/week @ 2025-12-27 1696/week @ 2026-01-03 2060/week @ 2026-01-10 2538/week @ 2026-01-17 2901/week @ 2026-01-24 2815/week @ 2026-01-31 2179/week @ 2026-02-07 2620/week @ 2026-02-14 12040/week @ 2026-02-21 210002/week @ 2026-02-28 9907/week @ 2026-03-07

234,877 downloads per month
Used in 18 crates (13 directly)

MIT/Apache

2.5MB
26K SLoC

C++ 25K SLoC // 0.1% comments Rust 1K SLoC // 0.0% comments Bitbake 370 SLoC // 0.5% comments Shell 4 SLoC

sentencepiece

This Rust crate is a binding for the sentencepiece unsupervised text tokenizer. The crate documentation is available online.

libsentencepiece dependency

This crate depends on the sentencepiece C++ library. By default, this dependency is treated as follows:

  • If sentencepiece could be found with pkg-config, the crate will link against the library found through pkg-config. Warning: dynamic linking only works correctly with sentencepiece 0.1.95 or later, due to a bug in earlier versions.
  • Otherwise, the crate's build script will do a static build of the sentencepiece library. This requires that cmake is available.

If you wish to override this behavior, the sentencepiece-sys crate offers two features:

  • system: always attempt to link to the sentencepiece library found with pkg-config.
  • static: always do a static build of the sentencepiece library and link against that.

lib.rs:

This crate binds the sentencepiece library. sentencepiece is an unsupervised text tokenizer.

The main data structure of this crate is SentencePieceProcessor, which is used to tokenize sentences:

use sentencepiece::SentencePieceProcessor;

let spp = SentencePieceProcessor::open("testdata/toy.model").unwrap();
let pieces = spp.encode("I saw a girl with a telescope.").unwrap()
  .into_iter().map(|p| p.piece).collect::<Vec<_>>();
assert_eq!(pieces, vec!["▁I", "▁saw", "▁a", "▁girl", "▁with",
  "▁a", "▁t", "el", "es", "c", "o", "pe", "."]);

Dependencies