forked from djc/instant-segment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_cases.rs
156 lines (148 loc) · 3.6 KB
/
test_cases.rs
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
use crate::{Search, Segmenter};
/// Run a segmenter against the built-in test cases
pub fn run(segmenter: &Segmenter) {
let mut search = Search::default();
assert_eq!(segmenter.segment("", &mut search).unwrap().len(), 0);
let mut success = true;
for test in TEST_CASES.iter().copied() {
success &= assert_segments(test, &mut search, segmenter);
}
for test in FAILED.iter().copied() {
success &= assert_segments(test, &mut search, segmenter);
}
assert!(success);
}
pub fn assert_segments(s: &[&str], search: &mut Search, segmenter: &Segmenter) -> bool {
let words = segmenter.segment(&s.join(""), search).unwrap();
let cmp = words.collect::<Vec<_>>();
let success = cmp == s;
if !success {
println!("expected: {:?}", s);
println!("actual: {:?}\n", cmp);
}
success
}
pub fn check_segments(s: &[&str], search: &mut Search, segmenter: &Segmenter) -> bool {
match segmenter.segment(&s.join(""), search) {
Ok(words) => s == words.collect::<Vec<_>>(),
Err(_) => false,
}
}
/// Built-in test cases
///
/// These are exposed so that you can test with different data sources.
pub const TEST_CASES: &[&[&str]] = &[
&["choose", "spain"],
&["this", "is", "a", "test"],
&["who", "represents"],
&["experts", "exchange"],
&["speed", "of", "art"],
&["now", "is", "the", "time", "for", "all", "good"],
&["it", "is", "a", "truth", "universally", "acknowledged"],
&[
"it", "was", "a", "bright", "cold", "day", "in", "april", "and", "the", "clocks", "were",
"striking", "thirteen",
],
&[
"when",
"in",
"the",
"course",
"of",
"human",
"events",
"it",
"becomes",
"necessary",
],
&[
"it",
"was",
"the",
"best",
"of",
"times",
"it",
"was",
"the",
"worst",
"of",
"times",
"it",
"was",
"the",
"age",
"of",
"wisdom",
"it",
"was",
"the",
"age",
"of",
"foolishness",
],
&[
"in", "a", "hole", "in", "the", "ground", "there", "lived", "a", "hobbit", "not", "a",
"nasty", "dirty", "wet", "hole", "filled", "with", "the", "ends", "of", "worms", "and",
"an", "oozy", "smell", "nor", "yet", "a", "dry", "bare", "sandy", "hole", "with",
"nothing", "in", "it", "to", "sit", "down", "on", "or", "to", "eat", "it", "was", "a",
"hobbit", "hole", "and", "that", "means", "comfort",
],
];
/// Incorrectly segmented test cases
const FAILED: &[&[&str]] = &[
&[
// The SCOWL word list (at size 60) data does not contain "unregarded"
"far",
"out",
"in",
"the",
"uncharted",
"backwaters",
"of",
"the",
"unfashionable",
"end",
"of",
"the",
"western",
"spiral",
"arm",
"of",
"the",
"galaxy",
"lies",
"a",
"small",
"un",
"regarded",
"yellow",
"sun",
],
&[
// The SCOWL word list (at size 60) does not contain "gregor"
"as",
"greg",
"or",
"sam",
"s",
"a",
"awoke",
"one",
"morning",
"from",
"uneasy",
"dreams",
"he",
"found",
"himself",
"transformed",
"in",
"his",
"bed",
"into",
"a",
"gigantic",
"insect",
],
];