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
use super::{CharNormalizer, CharOrStr};
use crate::{Script, Token};
/// This module contains the implementation of the `AeOeNormalizer` struct, which is a character normalizer
/// that replaces the characters 'œ', 'æ', 'Œ', and 'Æ' with their respective replacements 'oe', 'ae', 'OE', and 'AE'.
/// It also provides a test suite to validate the normalizer's functionality.
pub struct AeOeNormalizer;
// All normalizers only need to implement the method `normalize_char` and the method `should_normalize` of the `CharNormalizer` trait.
impl CharNormalizer for AeOeNormalizer {
// Creates the normalized version of the provided char.
fn normalize_char(&self, c: char) -> Option<CharOrStr> {
match c {
'œ' | 'Œ' => Some("oe".to_string().into()),
'æ' | 'Æ' => Some("ae".to_string().into()),
_ => Some(c.into()),
}
}
// Returns `true` if the Normalizer should be used.
fn should_normalize(&self, token: &Token) -> bool {
token.script == Script::Latin && token.lemma.chars().any(is_should_normalize)
}
}
fn is_should_normalize(c: char) -> bool {
matches!(c, 'œ' | 'æ' | 'Œ' | 'Æ')
}
// Test the normalizer:
#[cfg(test)]
mod test {
use std::borrow::Cow::Owned;
use crate::normalizer::test::test_normalizer;
use crate::normalizer::{Normalizer, NormalizerOption};
use crate::token::TokenKind;
// base tokens to normalize.
fn tokens() -> Vec<Token<'static>> {
vec![
Token {
lemma: Owned("œ".to_string()),
char_end: 2,
byte_end: 2,
script: Script::Latin,
..Default::default()
},
Token {
lemma: Owned("Œ".to_string()),
char_end: 2,
byte_end: 2,
script: Script::Latin,
..Default::default()
},
Token {
lemma: Owned("æ".to_string()),
char_end: 2,
byte_end: 2,
script: Script::Latin,
..Default::default()
},
Token {
lemma: Owned("Æ".to_string()),
char_end: 2,
byte_end: 2,
script: Script::Latin,
..Default::default()
},
]
}
// expected result of the current Normalizer.
fn normalizer_result() -> Vec<Token<'static>> {
vec![
Token {
lemma: Owned("oe".to_string()),
char_end: 2,
byte_end: 2,
script: Script::Latin,
char_map: Some(vec![(2, 2)]),
..Default::default()
},
Token {
lemma: Owned("oe".to_string()),
char_end: 2,
byte_end: 2,
script: Script::Latin,
char_map: Some(vec![(2, 2)]),
..Default::default()
},
Token {
lemma: Owned("ae".to_string()),
char_end: 2,
byte_end: 2,
script: Script::Latin,
char_map: Some(vec![(2, 2)]),
..Default::default()
},
Token {
lemma: Owned("ae".to_string()),
char_end: 2,
byte_end: 2,
script: Script::Latin,
char_map: Some(vec![(2, 2)]),
..Default::default()
},
]
}
// expected result of the complete Normalizer pieline.
fn normalized_tokens() -> Vec<Token<'static>> {
vec![
Token {
lemma: Owned("oe".to_string()),
char_end: 2,
byte_end: 2,
script: Script::Latin,
char_map: Some(vec![(2, 2)]),
kind: TokenKind::Word,
..Default::default()
},
Token {
lemma: Owned("oe".to_string()),
char_end: 2,
byte_end: 2,
script: Script::Latin,
char_map: Some(vec![(2, 2)]),
kind: TokenKind::Word,
..Default::default()
},
Token {
lemma: Owned("ae".to_string()),
char_end: 2,
byte_end: 2,
script: Script::Latin,
char_map: Some(vec![(2, 2)]),
kind: TokenKind::Word,
..Default::default()
},
Token {
lemma: Owned("ae".to_string()),
char_end: 2,
byte_end: 2,
script: Script::Latin,
char_map: Some(vec![(2, 2)]),
kind: TokenKind::Word,
..Default::default()
},
]
}
test_normalizer!(AeOeNormalizer, tokens(), normalizer_result(), normalized_tokens());
}