#sentence-piece #esaxx

esaxx-rs

Wrapping around sentencepiece's esaxxx library

11 releases

0.1.10 Oct 5, 2023
0.1.9 Oct 5, 2023
0.1.8 Jun 6, 2022
0.1.7 Sep 2, 2021
0.1.1 Jun 6, 2020

#103 in Data structures

Download history 18017/week @ 2023-10-31 19241/week @ 2023-11-07 15989/week @ 2023-11-14 14239/week @ 2023-11-21 16514/week @ 2023-11-28 14642/week @ 2023-12-05 15695/week @ 2023-12-12 11783/week @ 2023-12-19 9090/week @ 2023-12-26 13454/week @ 2024-01-02 17094/week @ 2024-01-09 22314/week @ 2024-01-16 19154/week @ 2024-01-23 21980/week @ 2024-01-30 22369/week @ 2024-02-06 21643/week @ 2024-02-13

89,108 downloads per month
Used in 54 crates (2 directly)

Apache-2.0

175KB
857 lines

esaxx-rs

This code implements a fast suffix tree / suffix array.

This code is taken from sentencepiece and to be used by hugging face.

Small wrapper around sentencepiece's esaxx suffix array C++ library. Usage

let string = "abracadabra";
let suffix = esaxx_rs::suffix(string).unwrap();
let chars: Vec<_> = string.chars().collect();
let mut iter = suffix.iter();
assert_eq!(iter.next().unwrap(), (&chars[..4], 2)); // abra
assert_eq!(iter.next(), Some((&chars[..1], 5))); // a
assert_eq!(iter.next(), Some((&chars[1..4], 2))); // bra
assert_eq!(iter.next(), Some((&chars[2..4], 2))); // ra
assert_eq!(iter.next(), Some((&chars[..0], 11))); // ''
assert_eq!(iter.next(), None);

lib.rs:

Small wrapper around sentencepiece's esaxx suffix array C++ library. Usage

#[cfg(feature="cpp")]
{
let string = "abracadabra";
let suffix = esaxx_rs::suffix(string).unwrap();
let chars: Vec<_> = string.chars().collect();
let mut iter = suffix.iter();
assert_eq!(iter.next().unwrap(), (&chars[..4], 2)); // abra
assert_eq!(iter.next(), Some((&chars[..1], 5))); // a
assert_eq!(iter.next(), Some((&chars[1..4], 2))); // bra
assert_eq!(iter.next(), Some((&chars[2..4], 2))); // ra
assert_eq!(iter.next(), Some((&chars[..0], 11))); // ''
assert_eq!(iter.next(), None);
}

The previous version uses unsafe optimized c++ code. There exists another implementation a bit slower (~2x slower) that uses safe rust. It's a bit slower because it uses usize (mostly 64bit) instead of i32 (32bit). But it does seems to fix a few OOB issues in the cpp version (which never seemed to cause real problems in tests but still.)

let string = "abracadabra";
let suffix = esaxx_rs::suffix_rs(string).unwrap();
let chars: Vec<_> = string.chars().collect();
let mut iter = suffix.iter();
assert_eq!(iter.next().unwrap(), (&chars[..4], 2)); // abra
assert_eq!(iter.next(), Some((&chars[..1], 5))); // a
assert_eq!(iter.next(), Some((&chars[1..4], 2))); // bra
assert_eq!(iter.next(), Some((&chars[2..4], 2))); // ra
assert_eq!(iter.next(), Some((&chars[..0], 11))); // ''
assert_eq!(iter.next(), None);

No runtime deps