-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
211: Enhance Quotation marks support r=ManyTheFish a=ManyTheFish # Pull Request This PR now segment and normalize all single-high-quotation-marks as single quotes (`'`). ## Related issue Related to [meilisearch#3689](meilisearch/meilisearch#3689), this PR is an hotfix for Meilisearch v1.2 before the `separator customization feature` that will refactor the Latin segmenter in v1.3 ## What does this PR do? - Make the Latin Segmenter segment on the other type of single high quotes - Normalize Unicode single high quotes into single quotes. Co-authored-by: ManyTheFish <[email protected]>
- Loading branch information
Showing
3 changed files
with
77 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use super::{CharNormalizer, CharOrStr}; | ||
use crate::detection::Script; | ||
use crate::Token; | ||
|
||
/// Latin specialized [`Normalizer`]. | ||
/// | ||
/// This Normalizer replaces unicode high quotation marks by a single quote. | ||
pub struct QuoteNormalizer; | ||
|
||
impl CharNormalizer for QuoteNormalizer { | ||
fn normalize_char(&self, c: char) -> Option<CharOrStr> { | ||
if is_unicode_high_quotation_mark(c) { | ||
Some('\''.into()) | ||
} else { | ||
Some(c.into()) | ||
} | ||
} | ||
|
||
fn should_normalize(&self, token: &Token) -> bool { | ||
token.script == Script::Latin && token.lemma.chars().any(is_unicode_high_quotation_mark) | ||
} | ||
} | ||
|
||
fn is_unicode_high_quotation_mark(c: char) -> bool { | ||
matches!(c, '’' | '‘' | '‛') | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use std::borrow::Cow::Owned; | ||
|
||
use crate::normalizer::test::test_normalizer; | ||
use crate::normalizer::{Normalizer, NormalizerOption}; | ||
|
||
// base tokens to normalize. | ||
fn tokens() -> Vec<Token<'static>> { | ||
vec![Token { | ||
lemma: Owned("l'l’l‘l‛".to_string()), | ||
char_end: 8, | ||
byte_end: 14, | ||
script: Script::Latin, | ||
..Default::default() | ||
}] | ||
} | ||
|
||
// expected result of the current Normalizer. | ||
fn normalizer_result() -> Vec<Token<'static>> { | ||
vec![Token { | ||
lemma: Owned("l'l'l'l'".to_string()), | ||
char_end: 8, | ||
byte_end: 14, | ||
script: Script::Latin, | ||
char_map: Some(vec![(1, 1), (1, 1), (1, 1), (3, 1), (1, 1), (3, 1), (1, 1), (3, 1)]), | ||
..Default::default() | ||
}] | ||
} | ||
|
||
// expected result of the complete Normalizer pieline. | ||
fn normalized_tokens() -> Vec<Token<'static>> { | ||
vec![Token { | ||
lemma: Owned("l'l'l'l'".to_string()), | ||
char_end: 8, | ||
byte_end: 14, | ||
script: Script::Latin, | ||
char_map: Some(vec![(1, 1), (1, 1), (1, 1), (3, 1), (1, 1), (3, 1), (1, 1), (3, 1)]), | ||
..Default::default() | ||
}] | ||
} | ||
|
||
test_normalizer!(QuoteNormalizer, tokens(), normalizer_result(), normalized_tokens()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters