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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use crate::{
Match,
StandardDictionary,
TextRange,
BehaviorForUnmatched,
utils::split_as_char_ranges,
};
pub fn segment_fully<T: AsRef<str>>(
text: T,
dict: &StandardDictionary,
behavior_for_unmatched: BehaviorForUnmatched,
) -> Vec<Match> {
let text = text.as_ref().to_lowercase();
match behavior_for_unmatched {
BehaviorForUnmatched::Ignore => {
dict.acdat
.find_overlapping_iter(&text)
.map(|mat| {
Match::new(
TextRange::new(mat.start(), mat.end()),
dict.value_to_tf_idf
.get(mat.value())
.map(|x| *x),
)
})
.collect()
},
| BehaviorForUnmatched::KeepAsChars
| BehaviorForUnmatched::KeepAsWords => {
let mut results: Vec<Match> = vec![];
let mut last_match_end_index = 0;
for mat in dict.acdat.find_overlapping_iter(&text) {
if mat.start() > last_match_end_index {
match behavior_for_unmatched {
BehaviorForUnmatched::Ignore => panic!("Rust is stupid."),
BehaviorForUnmatched::KeepAsWords => {
results.push(
Match::new(
TextRange::new(last_match_end_index, mat.start()),
None
)
);
},
BehaviorForUnmatched::KeepAsChars => {
for range in split_as_char_ranges(
&text[last_match_end_index..mat.start()]
) {
results.push(Match::new(range, None));
}
},
}
last_match_end_index = mat.end();
} else {
if mat.end() > last_match_end_index {
last_match_end_index = mat.end();
}
}
let result = Match::new(
TextRange::new(mat.start(), mat.end()),
dict.value_to_tf_idf
.get(mat.value())
.map(|x| *x),
);
results.push(result);
}
if last_match_end_index < text.len() {
match behavior_for_unmatched {
BehaviorForUnmatched::KeepAsWords => {
results.push(Match::new(
TextRange::new(last_match_end_index, text.len()),
None
))
},
BehaviorForUnmatched::KeepAsChars => {
for range in split_as_char_ranges(
&text[last_match_end_index..]
) {
let result = Match::new(
TextRange::new(
last_match_end_index + range.start_index(),
last_match_end_index + range.end_index(),
),
None,
);
results.push(result);
}
},
BehaviorForUnmatched::Ignore => panic!("Rust is stupid."),
}
}
results
}
}
}
#[cfg(test)]
mod tests {
use crate::{
segment_fully,
StandardDictionary,
BehaviorForUnmatched,
};
#[test]
fn test_ignore_unmatched() {
let text = " 南京市长江大桥, hello world ";
let dict = StandardDictionary::new(
vec!["南京", "南京市", "市长", "长江", "大桥", "你好世界"]
);
let result = segment_fully(
text,
&dict,
BehaviorForUnmatched::Ignore
);
assert_eq!(
result
.iter()
.map(|x| x.range().extract(text))
.collect::<Vec<_>>(),
vec!["南京", "南京市", "市长", "长江", "大桥"]
);
}
#[test]
fn test_keep_unmatched_as_chars() {
let text = " 南京市长江大桥, hello world ";
let dict = StandardDictionary::new(
vec!["南京", "南京市", "市长", "长江", "大桥", "你好世界"]
);
let result = segment_fully(
text,
&dict,
BehaviorForUnmatched::KeepAsChars
);
assert_eq!(
result
.iter()
.map(|x| x.range().extract(text))
.collect::<Vec<_>>(),
vec![
" ",
"南京",
"南京市",
"市长",
"长江",
"大桥",
",",
" ",
"h",
"e",
"l",
"l",
"o",
" ",
"w",
"o",
"r",
"l",
"d",
" ",
]
);
}
#[test]
fn test_keep_unmatched_as_words() {
let text = " 南京市长江大桥, hello world ";
let dict = StandardDictionary::new(
vec!["南京", "南京市", "市长", "长江", "大桥", "你好世界"]
);
let result = segment_fully(
text,
&dict,
BehaviorForUnmatched::KeepAsWords
);
assert_eq!(
result
.iter()
.map(|x| x.range().extract(text))
.collect::<Vec<_>>(),
vec![
" ",
"南京",
"南京市",
"市长",
"长江",
"大桥",
", hello world ",
]
);
}
#[test]
fn test_tf_idf() {
let text = " 南京市长江大桥, hello world ";
let dict = StandardDictionary::new_with_tf_idf(
vec![
("南京", 0f64),
("南京市", 1f64),
("市长", 2f64),
("长江", 3f64),
("大桥", 4f64),
("你好世界", 5f64),
]
);
let result = segment_fully(
text,
&dict,
BehaviorForUnmatched::Ignore
);
assert_eq!(
result
.iter()
.map(|x| x.tf_idf().unwrap())
.collect::<Vec<_>>(),
vec![
0f64,
1f64,
2f64,
3f64,
4f64
]
);
}
}