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
|
// |reftest| skip-if(!this.hasOwnProperty('Intl')||!this.Intl.Segmenter)
// https://www.unicode.org/reports/tr29/#Word_Boundary_Rules
const strings = {
// WB1, WB2
"": [],
// WB3
"\r\n": ["\r\n"],
// WB3a, WB3b
"\n": ["\n"],
"\r": ["\r"],
"\v": ["\v"],
"\f": ["\f"],
"\x85": ["\x85"],
// WB3d
" ": [" "],
" ": [" "],
// WB4
"\xAD": ["\xAD"],
"\xAD\xAD": ["\xAD\xAD"],
// WB5
"a": ["a"],
"ab": ["ab"],
// WB6, WB7
"a:b": ["a:b"],
"a·b": ["a·b"],
"a.b": ["a.b"],
"a'b": ["a'b"],
// WB8
"1": ["1"],
"12": ["12"],
// WB9
"a1": ["a1"],
// WB10
"1a": ["1a"],
// WB11, WB12
"1,2": ["1,2"],
"1;2": ["1;2"],
"1.2": ["1.2"],
"1'2": ["1'2"],
// WB13a
"a_": ["a_"],
"1_": ["1_"],
"__": ["__"],
// WB13b
"_a": ["_a"],
"_1": ["_1"],
// WB999
"\0": ["\0"],
"?": ["?"],
"??": ["?", "?"],
};
function assertSegments(string, words) {
let seg = segmenter.segment(string);
let segments = [...seg];
// The computed segments match the expected value.
assertEqArray(segments.map(({segment}) => segment), words);
// |containing()| should return the same result.
for (let expected of segments) {
let {segment, index} = expected;
for (let i = index; i < index + segment.length; ++i) {
let actual = seg.containing(i);
assertDeepEq(actual, expected);
}
}
}
let segmenter = new Intl.Segmenter("en", {granularity: "word"});
for (let [string, words] of Object.entries(strings)) {
assertSegments(string, words);
}
// WB3, WB3a, WB3b and WB4
for (let string of ["\r\n", "\n", "\r", "\v", "\f", "\x85"]) {
assertSegments(string + "\xAD", [string, "\xAD"]);
assertSegments("\xAD" + string, ["\xAD", string]);
}
// WB3d and WB4
for (let string of [" ", " "]) {
assertSegments(string + "\xAD", [string + "\xAD"]);
assertSegments("\xAD" + string, ["\xAD", string]);
}
assertSegments(" \xAD ", [" \xAD", " "]);
assertSegments(" \xAD\xAD ", [" \xAD\xAD", " "]);
// WB5-WB13 and WB4
for (let string of [
// WB5
"a", "ab",
// WB6, WB7
"a:b",
"a·b",
"a.b",
"a'b",
// WB8
"1",
"12",
// WB9
"a1",
// WB10
"1a",
// WB11, WB12
"1,2",
"1;2",
"1.2",
"1'2",
// WB13a
"a_",
"1_",
"__",
// WB13b
"_a",
"_1",
// WB999
"?",
]) {
assertSegments(string + "\xAD", [string + "\xAD"]);
assertSegments("\xAD" + string, ["\xAD", string]);
if (string === "a.b") {
// ICU4X incorrectly splits the result into three words.
// https://github.com/unicode-org/icu4x/issues/4417
assertSegments(string.split("").join("\xAD"), ["a\xAD", ".\xAD", "b"]);
assertSegments(string.split("").join("\xAD\xAD"), ["a\xAD\xAD", ".\xAD\xAD", "b"]);
} else {
assertSegments(string.split("").join("\xAD"), [string.split("").join("\xAD")]);
assertSegments(string.split("").join("\xAD\xAD"), [string.split("").join("\xAD\xAD")]);
}
}
assertSegments("?\xAD?", ["?\xAD", "?"]);
for (let string of [
// WB6, WB7
"a:b",
"a·b",
"a.b",
"a'b",
// WB11, WB12
"1,2",
"1;2",
"1.2",
"1'2",
]) {
let prefix = string.slice(0, -1);
let suffix = string.slice(1);
assertSegments(prefix, prefix.split(""));
assertSegments(suffix, suffix.split(""));
}
// MidNum with ALetter
assertSegments("a,b", ["a", ",", "b"]);
assertSegments("a;b", ["a", ";", "b"]);
// MidLetter with Numeric
assertSegments("1:2", ["1", ":", "2"]);
assertSegments("1·2", ["1", "·", "2"]);
// MidNumLet with mixed ALetter and Numeric
assertSegments("a.2", ["a", ".", "2"]);
assertSegments("1.b", ["1", ".", "b"]);
assertSegments("a'2", ["a", "'", "2"]);
assertSegments("1'b", ["1", "'", "b"]);
// MidNum with ExtendNumLet
assertSegments("_,_", ["_", ",", "_"]);
assertSegments("_;_", ["_", ";", "_"]);
// MidLetter with ExtendNumLet
assertSegments("_:_", ["_", ":", "_"]);
assertSegments("_·_", ["_", "·", "_"]);
// MidNumLet with ExtendNumLet
assertSegments("_._", ["_", ".", "_"]);
assertSegments("_'_", ["_", "'", "_"]);
// CLDR has locale-dependent word segmentation for the "en-posix" locale. This
// locale is currently not selectable, so the Latin-1 fast-paths don't need to
// implement it. If one of the two below assertions ever fail, please update
// the Latin-1 fast-paths for word segmentation to implement the "en-posix"
// changes.
assertEq(new Intl.Segmenter("en-posix").resolvedOptions().locale, "en");
assertEq(new Intl.Segmenter("en-u-va-posix").resolvedOptions().locale, "en");
if (typeof reportCompare === "function")
reportCompare(0, 0);
|