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
use std::iter::once;
use super::{CharNormalizer, CharOrStr};
use crate::detection::Script;
use crate::Token;
/// A global [`Normalizer`] lowercasing characters.
///
pub struct LowercaseNormalizer;
impl CharNormalizer for LowercaseNormalizer {
fn normalize_char(&self, c: char) -> Option<CharOrStr> {
let mut normalized = c.to_lowercase();
// if the original character is converted in exactly one character,
// then we return the character directly instead of creating a string for it.
match (normalized.next(), normalized.next()) {
(Some(c), None) => Some(c.into()),
(Some(first), Some(second)) => {
let normalized: String =
once(first).chain(once(second)).chain(normalized).collect();
Some(normalized.into())
}
(None, _) => None,
}
}
fn should_normalize(&self, token: &Token) -> bool {
// https://en.wikipedia.org/wiki/Letter_case#Capitalisation
matches!(
token.script,
Script::Latin | Script::Cyrillic | Script::Greek | Script::Georgian | Script::Armenian
) && token.lemma.chars().any(char::is_uppercase)
}
}
#[cfg(test)]
mod test {
use std::borrow::Cow::Owned;
use crate::normalizer::test::test_normalizer;
use crate::normalizer::{Normalizer, NormalizerOption};
use crate::token::TokenKind;
fn tokens() -> Vec<Token<'static>> {
vec![
Token {
lemma: Owned("PascalCase".to_string()),
char_end: 10,
byte_end: 10,
script: Script::Latin,
..Default::default()
},
Token {
lemma: Owned("ՀայասՏան".to_string()),
char_end: 8,
byte_end: 16,
script: Script::Armenian,
..Default::default()
},
]
}
fn normalizer_result() -> Vec<Token<'static>> {
vec![
Token {
lemma: Owned("pascalcase".to_string()),
char_end: 10,
byte_end: 10,
script: Script::Latin,
char_map: Some(vec![
(1, 1),
(1, 1),
(1, 1),
(1, 1),
(1, 1),
(1, 1),
(1, 1),
(1, 1),
(1, 1),
(1, 1),
]),
..Default::default()
},
Token {
lemma: Owned("հայաստան".to_string()),
char_end: 8,
byte_end: 16,
script: Script::Armenian,
char_map: Some(vec![
(2, 2),
(2, 2),
(2, 2),
(2, 2),
(2, 2),
(2, 2),
(2, 2),
(2, 2),
]),
..Default::default()
},
]
}
fn normalized_tokens() -> Vec<Token<'static>> {
vec![
Token {
lemma: Owned("pascalcase".to_string()),
char_end: 10,
byte_end: 10,
script: Script::Latin,
kind: TokenKind::Word,
char_map: Some(vec![
(1, 1),
(1, 1),
(1, 1),
(1, 1),
(1, 1),
(1, 1),
(1, 1),
(1, 1),
(1, 1),
(1, 1),
]),
..Default::default()
},
Token {
lemma: Owned("հայաստան".to_string()),
char_end: 8,
byte_end: 16,
script: Script::Armenian,
kind: TokenKind::Word,
char_map: Some(vec![
(2, 2),
(2, 2),
(2, 2),
(2, 2),
(2, 2),
(2, 2),
(2, 2),
(2, 2),
]),
..Default::default()
},
]
}
test_normalizer!(LowercaseNormalizer, tokens(), normalizer_result(), normalized_tokens());
}