Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions tokenizers/tk-encode/src/vocab/bucket_added_vocabulary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ fn is_ws(cp: u32) -> bool {
char::from_u32(cp).is_some_and(|c| c.is_whitespace())
}
}
fn is_single_word(bytes: &[u8], search: usize, match_start: usize, match_end: usize) -> bool {
fn is_single_word(bytes: &[u8], match_start: usize, match_end: usize) -> bool {
// FIXME: we use chr conversion for now, this can be inproved by using bitmap.
// This is the equivalent of `\w`, so its letters, numbers and underscore
let s = unsafe { std::str::from_utf8_unchecked(bytes) };
let before_ok = s[search..match_start]
// The char before the match is looked up in the whole text, not just from where the scan
// resumed: the scan can resume inside a word (after a rejected candidate) or right after an
// earlier added token, and neither is a word boundary.
Comment thread
0z5a marked this conversation as resolved.
Resolved
let before_ok = s[..match_start]
.chars()
.next_back()
.is_none_or(|c| !is_word_char(c));
Expand Down Expand Up @@ -465,7 +468,7 @@ impl PipelinePatternMatcher for AddedVocabulary {
}
// single_word: reject unless the token is a standalone word (neither neighbour
// char is a word char). Checked on the raw byte bounds, before any strip.
if metadata.single_word && !is_single_word(bytes, search, match_start, match_end) {
if metadata.single_word && !is_single_word(bytes, match_start, match_end) {
search = match_start + 1;
continue;
}
Expand Down Expand Up @@ -869,6 +872,44 @@ mod tests {
);
}

#[test]
fn single_word_looks_before_where_the_scan_resumed() {
Comment thread
0z5a marked this conversation as resolved.
Resolved
let model = ModelMock::new(&[]);
let mut vocab = AddedVocabulary::new();
let normalizer: Option<&Lowercase> = None;
vocab
.add_tokens(
["ü", "zz", "foo"]
.map(|t| {
AddedToken::from(t, false)
.normalized(false)
.single_word(true)
})
.into_iter()
.chain([AddedToken::from("x", false).normalized(false)]),
model.get_vocab_size(),
|t| model.token_to_id(t),
normalizer,
)
.unwrap();

// The first `ü` is rejected, and the scan resumes inside its two UTF-8 bytes.
assert_eq!(
segments(&vocab, "üü ü", false, false),
vec![(Some("üü ".to_string()), None), (None, Some(0))]
);
// `zz` at 0..2 is rejected; `zz` at 1..3 still follows a `z`.
assert_eq!(
segments(&vocab, "zzz zz", false, false),
vec![(Some("zzz ".to_string()), None), (None, Some(1))]
);
// `foo` follows the added token `x`, whose last char is a word char.
assert_eq!(
segments(&vocab, "xfoo", false, false),
vec![(None, Some(3)), (Some("foo".to_string()), None)]
);
Comment thread
0z5a marked this conversation as resolved.
Resolved
}

#[test]
fn segment_iterator_applies_lstrip_rstrip() {
let model = ModelMock::new(&[]);
Expand Down
Loading