1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//! `morpheme` — fast, pure-Rust subword tokenization compatible with
//! Hugging Face [`tokenizers`](https://github.com/huggingface/tokenizers).
//!
//! morpheme loads, runs, trains and saves tokenizers in the Hugging Face
//! `tokenizer.json` format (BPE, WordPiece, WordLevel and Unigram) and
//! produces the same ids, tokens, offsets and decoded text as the
//! reference implementation.
//!
//! # Quick start
//!
//! Load a `tokenizer.json` (from disk, or from the Hub with the `hub`
//! feature) and encode:
//!
//! ```no_run
//! use morpheme::Tokenizer;
//!
//! let tokenizer = Tokenizer::from_file("tokenizer.json")?;
//! let encoding = tokenizer.encode("Hello, world!", true)?;
//! println!("{:?} {:?}", encoding.tokens(), encoding.ids());
//! let text = tokenizer.decode(encoding.ids(), true)?;
//! # Ok::<(), morpheme::Error>(())
//! ```
//!
//! Or build one in code, train it, and save it:
//!
//! ```
//! use morpheme::models::WordPiece;
//! use morpheme::normalizers::BertNormalizer;
//! use morpheme::pre_tokenizers::BertPreTokenizer;
//! use morpheme::trainers::WordPieceTrainer;
//! use morpheme::{AddedToken, Tokenizer};
//!
//! let mut tokenizer = Tokenizer::new(WordPiece::default())
//! .with_normalizer(BertNormalizer::default())
//! .with_pre_tokenizer(BertPreTokenizer);
//! let trainer = WordPieceTrainer::builder()
//! .vocab_size(100)
//! .special_tokens(vec![AddedToken::new("[UNK]", true)])
//! .show_progress(false)
//! .build()?;
//! tokenizer.train(trainer, ["Hello world!", "hello again, World"].into_iter())?;
//!
//! let encoding = tokenizer.encode("HELLO, world", false)?;
//! assert_eq!(encoding.tokens(), ["hello", ",", "world"]);
//! assert_eq!(encoding.offsets()[1], (5, 6)); // byte offsets into the input
//!
//! let json = tokenizer.to_json(false)?; // a valid Hugging Face tokenizer.json
//! let reloaded = Tokenizer::from_json(&json)?;
//! assert_eq!(reloaded.encode("HELLO, world", false)?, encoding);
//! # Ok::<(), morpheme::Error>(())
//! ```
//!
//! # The pipeline
//!
//! ```text
//! input ─▶ added tokens ─▶ Normalizer ─▶ PreTokenizer ─▶ Model
//! ─▶ truncation ─▶ PostProcessor ─▶ padding ─▶ Encoding
//!
//! ids ─▶ (skip special tokens) ─▶ Decoder ─▶ text
//! ```
//!
//! | Stage | Trait | Built-in components |
//! | --- | --- | --- |
//! | Normalize text | [`Normalizer`] | [`normalizers`] |
//! | Split into words | [`PreTokenizer`] | [`pre_tokenizers`] |
//! | Words → tokens | [`Model`] | [`models`] |
//! | Add special tokens, merge pairs | [`PostProcessor`] | [`processors`] |
//! | Tokens → text | [`Decoder`] | [`decoders`] |
//! | Learn a model | [`Trainer`] | [`trainers`] |
//!
//! Each module has a wrapper enum ([`NormalizerWrapper`], [`ModelWrapper`],
//! …) that a [`Tokenizer`] stores and that (de)serializes with the
//! Hugging Face `"type"` tag. Offsets always point into the original input,
//! even after normalization, thanks to [`NormalizedString`]'s alignment
//! tracking: [`Tokenizer::encode`] returns byte offsets and
//! [`Tokenizer::encode_char_offsets`] returns char offsets (what the Python
//! library returns).
//!
//! # Cargo features
//!
//! | Feature | Default | Enables |
//! | --- | --- | --- |
//! | `progressbar` | yes | Trainer progress bars on stderr (`show_progress`) |
//! | `hub` | no | [`Tokenizer::from_pretrained`]: download from the Hugging Face Hub into the cache shared with Python |
//!
//! # Compatibility
//!
//! Behavior is verified against 11 real tokenizers (BERT, GPT-2, RoBERTa,
//! Llama, T5, XLM-R, …) recorded from `tokenizers` 0.23. Known, deliberate
//! differences are listed in the repository's `docs/interop.md`.
/// Library version, mirrored from `Cargo.toml`.
pub const VERSION: &str = env!;
/// A `(start, end)` byte or char range.
pub type Offsets = ;
pub use ;
pub use DecoderWrapper;
pub use ;
pub use ;
pub use ModelWrapper;
pub use ;
pub use NormalizerWrapper;
pub use ;
pub use PreTokenizerWrapper;
pub use PostProcessorWrapper;
pub use FromPretrainedParameters;
pub use ;
pub use ;
/// A token produced by a [`Model`].