Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix char boundary panic #281

Merged
merged 4 commits into from
Apr 17, 2024
Merged
Changes from all commits
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
13 changes: 6 additions & 7 deletions charabia/src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,26 +313,25 @@ impl<'tb, A: AsRef<[u8]>> TokenizerBuilder<'tb, A> {
// TODO: avoid recreating the automaton if nothing changed
match (self.normalizer_option.classifier.separators, self.words_dict) {
(Some(separators), None) => {
let pattern = separators.iter().filter(|s| !s.is_empty());
let aho = AhoCorasick::builder()
.match_kind(MatchKind::LeftmostLongest)
.build(separators)
.build(pattern)
.unwrap();

self.segmenter_option.aho = Some(aho);
self.segmenter_option.aho = Some(aho).filter(|aho| aho.patterns_len() != 0);
}
(separators, Some(words)) => {
// use the default separators' list if a custom words' list is given but no custom separators' list.
let separators = separators.unwrap_or(DEFAULT_SEPARATORS);
// merge both lists together and create the Aho-Corasick automaton.
let mut vec = Vec::with_capacity(separators.len() + words.len());
vec.extend_from_slice(words);
vec.extend_from_slice(separators);
let pattern = words.iter().chain(separators).filter(|s| !s.is_empty());
let aho = AhoCorasick::builder()
.match_kind(MatchKind::LeftmostLongest)
.build(vec)
.build(pattern)
.unwrap();

self.segmenter_option.aho = Some(aho);
self.segmenter_option.aho = Some(aho).filter(|aho| aho.patterns_len() != 0);
}
// reset the state in case the builder is reused.
(None, None) => self.segmenter_option.aho = None,
Expand Down
Loading