Skip to content

Commit 6fd2fcc

Browse files
Merge #317
317: Latin camelcase wrong segmentation r=ManyTheFish a=PedroTurik # Pull Request All interesting changes are in charabia/src/segmenter/latin/camel_case.rs other changes are import reordering caused by cargo fmt. This change definitely looks like a small speed regression, but I will be opening for review nonetheless. First time contributor to any meilisearch repo. ## Related issue Fixes [#289](#289) ## What does this PR do? # This PR changes the Latin camelCase segmentation to address the following cases: - openSSL should be segmented as ['open', 'SSL'] - openSSLError should be segmented as ['open', 'SSL', 'Error'] This is done by introducing a helper iteration for the main iterator to "peek" the next char, and this is needed in the currently solution with `StrGroupBy::linear_group_by` afaik since the segmentation sometimes depends on the existence of a lowercase letter after the one currently being analyzed ( like openSSLError ) ## Benchmarks the benchmarks ran once WITH the changes, and once more after commenting the changes out. The output here is from after removing the changes ```bash cargo bench ``` And what I believe is the relevant output is the following ```bash segment/132/Latin/Eng time: [2.5651 µs 2.5714 µs 2.5786 µs] change: [+0.6549% +0.8593% +1.0487%] (p = 0.00 < 0.05) Change within noise threshold. Found 6 outliers among 100 measurements (6.00%) 1 (1.00%) low mild 2 (2.00%) high mild 3 (3.00%) high severe segment/132/Latin/Fra time: [2.3521 µs 2.3547 µs 2.3572 µs] change: [-0.3826% -0.0443% +0.2662%] (p = 0.79 > 0.05) No change in performance detected. Found 2 outliers among 100 measurements (2.00%) 2 (2.00%) low mild segment/131/Latin/Deu time: [2.1346 µs 2.1372 µs 2.1403 µs] change: [+0.4418% +0.5896% +0.7584%] (p = 0.00 < 0.05) Change within noise threshold. Found 5 outliers among 100 measurements (5.00%) 4 (4.00%) high mild 1 (1.00%) high severe segment/363/Latin/Eng time: [6.6601 µs 6.6749 µs 6.6911 µs] change: [+3.1214% +3.4133% +3.7267%] (p = 0.00 < 0.05) Performance has regressed. Found 18 outliers among 100 measurements (18.00%) 8 (8.00%) low mild 3 (3.00%) high mild 7 (7.00%) high severe segment/363/Latin/Fra time: [6.5405 µs 6.5481 µs 6.5566 µs] change: [-0.4942% -0.2417% +0.0139%] (p = 0.07 > 0.05) No change in performance detected. Found 6 outliers among 100 measurements (6.00%) 5 (5.00%) high mild 1 (1.00%) high severe segment/365/Latin/Vie time: [5.6049 µs 5.6109 µs 5.6170 µs] change: [-4.2406% -3.9811% -3.7043%] (p = 0.00 < 0.05) Performance has improved. Found 5 outliers among 100 measurements (5.00%) 1 (1.00%) low mild 4 (4.00%) high mild tokenize/132/Latin/Eng time: [6.1291 µs 6.1349 µs 6.1406 µs] change: [-1.4925% -1.3344% -1.1740%] (p = 0.00 < 0.05) Performance has improved. Found 6 outliers among 100 measurements (6.00%) 4 (4.00%) high mild 2 (2.00%) high severe tokenize/132/Latin/Fra time: [6.9543 µs 6.9617 µs 6.9696 µs] change: [-1.5821% -1.3067% -1.0380%] (p = 0.00 < 0.05) Performance has improved. Found 8 outliers among 100 measurements (8.00%) 3 (3.00%) low mild 4 (4.00%) high mild 1 (1.00%) high severe tokenize/131/Latin/Deu time: [6.4793 µs 6.4916 µs 6.5050 µs] change: [-3.1861% -2.9351% -2.6852%] (p = 0.00 < 0.05) Performance has improved. Found 1 outliers among 100 measurements (1.00%) 1 (1.00%) low mild tokenize/363/Latin/Eng time: [15.510 µs 15.523 µs 15.535 µs] change: [-8.2776% -7.9652% -7.6477%] (p = 0.00 < 0.05) Performance has improved. Found 9 outliers among 100 measurements (9.00%) 9 (9.00%) high mild tokenize/363/Latin/Fra time: [18.026 µs 18.044 µs 18.064 µs] change: [-2.4157% -1.5634% -1.0380%] (p = 0.00 < 0.05) Performance has improved. Found 5 outliers among 100 measurements (5.00%) 2 (2.00%) low mild 3 (3.00%) high mild tokenize/365/Latin/Vie time: [27.604 µs 27.626 µs 27.651 µs] change: [-2.5536% -1.9732% -1.4007%] (p = 0.00 < 0.05) Performance has improved. Found 3 outliers among 100 measurements (3.00%) 1 (1.00%) low mild 1 (1.00%) high mild 1 (1.00%) high severe tokenize/354/Latin/Deu time: [17.874 µs 17.904 µs 17.936 µs] change: [+2.2411% +2.4528% +2.6719%] (p = 0.00 < 0.05) Performance has regressed. Found 1 outliers among 100 measurements (1.00%) 1 (1.00%) high mild ``` ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Co-authored-by: Pedro Turik Firmino <[email protected]>
2 parents 4a16cd3 + 6d8ae94 commit 6fd2fcc

File tree

5 files changed

+25
-11
lines changed

5 files changed

+25
-11
lines changed

charabia/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ mod tokenizer;
5959
pub use detection::{Language, Script, StrDetection};
6060
pub use normalizer::Normalize;
6161
pub use segmenter::Segment;
62-
pub use token::{SeparatorKind, Token, TokenKind};
63-
6462
#[cfg(test)]
6563
pub use token::StaticToken;
64+
pub use token::{SeparatorKind, Token, TokenKind};
6665

6766
pub use crate::tokenizer::{ReconstructedTokenIter, Tokenize, Tokenizer, TokenizerBuilder};

charabia/src/normalizer/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::borrow::Cow;
22

33
use once_cell::sync::Lazy;
44

5+
pub use self::ae_oe_normalizer::AeOeNormalizer;
56
pub use self::arabic::ArabicNormalizer;
67
#[cfg(feature = "chinese-normalization")]
78
pub use self::chinese::ChineseNormalizer;
@@ -24,8 +25,6 @@ pub use self::vietnamese::VietnameseNormalizer;
2425
use crate::segmenter::SegmentedTokenIter;
2526
use crate::Token;
2627

27-
pub use self::ae_oe_normalizer::AeOeNormalizer;
28-
2928
mod arabic;
3029
#[cfg(feature = "chinese-normalization")]
3130
mod chinese;

charabia/src/segmenter/latin/camel_case.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,26 @@ use slice_group_by::StrGroupBy;
55
/// For instance, "camelCase" is split into ["camel", "Case"].
66
/// A camelCase boundary constitutes a lowercase letter directly followed by an uppercase letter
77
/// optionally with any number of non-spacing marks in between.
8+
/// Two consecutive uppercase letters constitute a boundary only if the following letter is lowercase
9+
/// (eg., "MongoDBError" is split into ["Mongo", "DB", "Error"])
810
pub(crate) fn split_camel_case_bounds(str: &str) -> impl Iterator<Item = &str> {
9-
let mut last_char_was_lowercase = str.chars().next().map_or(false, |c| c.is_lowercase());
11+
let mut peek_char = str.chars().map(|c| c.is_lowercase());
12+
let mut last_char_was_lowercase: bool = peek_char.next().unwrap_or_default();
13+
14+
peek_char.next();
1015

1116
str.linear_group_by(move |_, char| {
17+
let peek_char_is_lowercase: bool = peek_char.next().unwrap_or_default();
18+
1219
if char.is_mark_nonspacing() {
1320
return true;
1421
}
1522

16-
if last_char_was_lowercase && char.is_letter_uppercase() {
17-
return false;
18-
}
23+
let should_group =
24+
!((last_char_was_lowercase || peek_char_is_lowercase) && char.is_letter_uppercase());
1925

2026
last_char_was_lowercase = char.is_letter_lowercase();
21-
true
27+
should_group
2228
})
2329
}
2430

@@ -48,4 +54,10 @@ mod test {
4854
non_spacing_marks_are_respected
4955
);
5056
test_segmentation!("a\u{0301}B", ["a\u{0301}", "B"], non_spacing_mark_after_first_letter);
57+
test_segmentation!("openSSL", ["open", "SSL"], consecutive_uppercase_is_not_split);
58+
test_segmentation!(
59+
"MongoDBDatabase",
60+
["Mongo", "DB", "Database"],
61+
last_uppercase_from_non_final_sequence
62+
);
5163
}

irg-kvariants/build.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
use std::env;
2+
use std::error::Error;
3+
use std::path::Path;
4+
15
use serde::Deserialize;
2-
use std::{env, error::Error, path::Path};
36

47
#[derive(Deserialize)]
58
pub struct TsvRow {

irg-kvariants/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
use std::collections::HashMap;
2+
13
use once_cell::sync::Lazy;
24
use serde::Deserialize;
3-
use std::collections::HashMap;
45

56
#[derive(Debug, Eq, PartialEq)]
67
pub enum KVariantClass {

0 commit comments

Comments
 (0)