Debian Codegraph @

plugins/jieba/deps/cppjieba/test/unittest/segments_test.cpp

raw symbols SCIP
1 #include "cppjieba/SegmentBase.hpp"
2 #include "cppjieba/MixSegment.hpp"
3 #include "cppjieba/MPSegment.hpp"
4 #include "cppjieba/HMMSegment.hpp"
5 #include "cppjieba/FullSegment.hpp"
6 #include "cppjieba/QuerySegment.hpp"
7 #include "gtest/gtest.h"
8 #include "test_paths.h"
9
10 using namespace cppjieba;
11
12 TEST(MixSegmentTest, Test1) {
13 MixSegment segment(DICT_DIR "/jieba.dict.utf8", DICT_DIR "/hmm_model.utf8");
14 string sentence;
15 vector<string> words;
16 string actual;
17 string expected;
18
19 {
20 sentence = "我来自北京邮电大学。。。学号123456,用AK47";
21 expected = "我/来自/北京邮电大学/。/。/。/学号/123456/,/用/AK47";
22 segment.Cut(sentence, words);
23 actual = Join(words.begin(), words.end(), "/");
24 ASSERT_EQ(actual, expected);
25 }
26
27 {
28 sentence = "B超 T恤";
29 expected = "B超/ /T恤";
30 segment.Cut(sentence, words);
31 actual = Join(words.begin(), words.end(), "/");
32 ASSERT_EQ(actual, expected);
33 }
34
35 {
36 // Digit+letter combinations should be kept together, matching Python jieba behavior
37 sentence = "5G网络速度快";
38 expected = "5G/网络/速度/快";
39 segment.Cut(sentence, words);
40 actual = Join(words.begin(), words.end(), "/");
41 ASSERT_EQ(actual, expected);
42 }
43
44 {
45 // Decimal number followed by letters: decimal and letters remain separate
46 sentence = "3.5KG重量";
47 expected = "3.5/KG/重量";
48 segment.Cut(sentence, words);
49 actual = Join(words.begin(), words.end(), "/");
50 ASSERT_EQ(actual, expected);
51 }
52
53 {
54 sentence = "他来到了网易杭研大厦";
55 expected = "他/来到/了/网易/杭/研/大厦";
56 segment.Cut(sentence, words, false);
57 actual = Join(words.begin(), words.end(), "/");
58 ASSERT_EQ(actual, expected);
59 }
60
61 {
62 sentence = "他来到了网易杭研大厦";
63 expected = "他/来到/了/网易/杭研/大厦";
64 segment.Cut(sentence, words);
65 actual = Join(words.begin(), words.end(), "/");
66 ASSERT_EQ(actual, expected);
67 }
68
69 {
70 sentence = "马克思主义和习总书记新时代中国特色社会主义";
71 expected = "马克思主义/和/习总书记/新/时代/中国/特色/社会主义";
72 segment.Cut(sentence, words);
73 actual = Join(words.begin(), words.end(), "/");
74 ASSERT_EQ(actual, expected);
75 }
76 }
77
78 TEST(MixSegmentTest, NoUserDict) {
79 MixSegment segment(TEST_DATA_DIR "/extra_dict/jieba.dict.small.utf8", DICT_DIR "/hmm_model.utf8");
80 const char* str = "令狐冲是云计算方面的专家";
81 vector<string> words;
82 segment.Cut(str, words);
83 string res;
84 ASSERT_EQ("[\"令狐冲\", \"是\", \"云\", \"计算\", \"方面\", \"的\", \"专家\"]", res << words);
85 }
86
87 TEST(MixSegmentTest, UserDict) {
88 MixSegment segment(TEST_DATA_DIR "/extra_dict/jieba.dict.small.utf8",
89 DICT_DIR "/hmm_model.utf8",
90 DICT_DIR "/user.dict.utf8");
91 {
92 const char* str = "令狐冲是云计算方面的专家";
93 vector<string> words;
94 segment.Cut(str, words);
95 string res;
96 ASSERT_EQ("[\"令狐冲\", \"是\", \"云计算\", \"方面\", \"的\", \"专家\"]", res << words);
97 }
98 {
99 const char* str = "小明先就职于IBM,后在日本京都大学深造";
100 vector<string> words;
101 segment.Cut(str, words);
102 string res;
103 res << words;
104 ASSERT_EQ("[\"小明\", \"先\", \"就职\", \"于\", \"IBM\", \",\", \"后\", \"在\", \"日本\", \"京都大学\", \"深造\"]", res);
105 }
106 {
107 const char* str = "IBM,3.14";
108 vector<string> words;
109 segment.Cut(str, words);
110 string res;
111 res << words;
112 ASSERT_EQ("[\"IBM\", \",\", \"3.14\"]", res);
113 }
114 }
115
116 TEST(MixSegmentTest, TestUserDict) {
117 MixSegment segment(TEST_DATA_DIR "/extra_dict/jieba.dict.small.utf8", DICT_DIR "/hmm_model.utf8",
118 TEST_DATA_DIR "/userdict.utf8");
119 vector<string> words;
120 string res;
121
122 segment.Cut("令狐冲是云计算方面的专家", words);
123 ASSERT_EQ("[\"令狐冲\", \"是\", \"云计算\", \"方面\", \"的\", \"专家\"]", res << words);
124
125 segment.Cut("小明先就职于IBM,后在日本京都大学深造", words);
126 res << words;
127 ASSERT_EQ("[\"小明\", \"先\", \"就职\", \"于\", \"I\", \"B\", \"M\", \",\", \"后\", \"在\", \"日本\", \"京都大学\", \"深造\"]", res);
128
129 segment.Cut("IBM,3.14", words);
130 res << words;
131 ASSERT_EQ("[\"I\", \"B\", \"M\", \",\", \"3.14\"]", res);
132
133 segment.Cut("忽如一夜春风来,千树万树梨花开", words);
134 res = Join(words.begin(), words.end(), "/");
135 ASSERT_EQ("忽如一夜春风来/,/千树/万树/梨花/开", res);
136
137 // rand input
138 {
139 const size_t ITERATION = 16;
140 const size_t MAX_LEN = 256;
141 string s;
142 srand(time(NULL));
143
144 for (size_t i = 0; i < ITERATION; i++) {
145 size_t len = rand() % MAX_LEN;
146 s.resize(len);
147 for (size_t j = 0; j < len; j++) {
148 s[rand() % len] = rand();
149 }
150 segment.Cut(s, words);
151 }
152 }
153 }
154
155 TEST(MixSegmentTest, TestMultiUserDict) {
156 MixSegment segment(TEST_DATA_DIR "/extra_dict/jieba.dict.small.utf8", DICT_DIR "/hmm_model.utf8",
157 TEST_DATA_DIR "/userdict.utf8;" TEST_DATA_DIR "/userdict.2.utf8");
158 vector<string> words;
159 string res;
160
161 segment.Cut("忽如一夜春风来,千树万树梨花开", words);
162 res = Join(words.begin(), words.end(), "/");
163 ASSERT_EQ("忽如一夜春风来/,/千树万树梨花开", res);
164 }
165
166 TEST(MPSegmentTest, Test1) {
167 MPSegment segment(DICT_DIR "/jieba.dict.utf8");
168 string s;
169 vector<string> words;
170 segment.Cut("我来自北京邮电大学。", words);
171 ASSERT_EQ("[\"我\", \"来自\", \"北京邮电大学\", \"。\"]", s << words);
172
173 segment.Cut("B超 T恤", words);
174 ASSERT_EQ(s << words, "[\"B超\", \" \", \"T恤\"]");
175
176 segment.Cut("南京市长江大桥", words);
177 ASSERT_EQ("[\"南京市\", \"长江大桥\"]", s << words);
178
179 // MaxWordLen
180 segment.Cut("南京市长江大桥", words, 3);
181 ASSERT_EQ("[\"南京市\", \"长江\", \"大桥\"]", s << words);
182
183 segment.Cut("南京市长江大桥", words, 0);
184 ASSERT_EQ("[\"南\", \"京\", \"市\", \"长\", \"江\", \"大\", \"桥\"]", s << words);
185
186 segment.Cut("湖南长沙市天心区", words);
187 s = Join(words.begin(), words.end(), "/");
188 ASSERT_EQ("湖南长沙市/天心区", s);
189
190 segment.Cut("湖南长沙市天心区", words, 3);
191 s = Join(words.begin(), words.end(), "/");
192 ASSERT_EQ("湖南/长沙市/天心区", s);
193 }
194
195 TEST(HMMSegmentTest, Test1) {
196 HMMSegment segment(DICT_DIR "/hmm_model.utf8");
197 {
198 const char* str = "我来自北京邮电大学。。。学号123456";
199 const char* res[] = {"我来", "自北京", "邮电大学", "。", "。", "。", "学号", "123456"};
200 vector<string> words;
201 segment.Cut(str, words);
202 ASSERT_EQ(words, vector<string>(res, res + sizeof(res)/sizeof(res[0])));
203 }
204
205 {
206 const char* str = "IBM,1.2,123";
207 const char* res[] = {"IBM", ",", "1.2", ",", "123"};
208 vector<string> words;
209 segment.Cut(str, words);
210 ASSERT_EQ(words, vector<string>(res, res + sizeof(res)/sizeof(res[0])));
211 }
212 }
213
214 TEST(HMMSegmentTest, AlphanumericCombinations) {
215 // Tests for digit+letter and letter+decimal combinations to align with
216 // Python jieba finalseg behavior: re_skip = "[a-zA-Z0-9]+(?:\.\d+)?"
217 HMMSegment segment(DICT_DIR "/hmm_model.utf8");
218 vector<string> words;
219
220 {
221 // digit followed by letter (no decimal) should stay together
222 const char* str = "5G";
223 const char* res[] = {"5G"};
224 segment.Cut(str, words);
225 ASSERT_EQ(words, vector<string>(res, res + sizeof(res)/sizeof(res[0])));
226 }
227
228 {
229 // digit followed by letter then Chinese
230 const char* str = "3D打印";
231 const char* res[] = {"3D", "打印"};
232 segment.Cut(str, words);
233 ASSERT_EQ(words, vector<string>(res, res + sizeof(res)/sizeof(res[0])));
234 }
235
236 {
237 // decimal number followed by letters: decimal and letters are separate
238 const char* str = "3.5KG";
239 const char* res[] = {"3.5", "KG"};
240 segment.Cut(str, words);
241 ASSERT_EQ(words, vector<string>(res, res + sizeof(res)/sizeof(res[0])));
242 }
243
244 {
245 // letter followed by digit followed by decimal
246 const char* str = "v1.2";
247 const char* res[] = {"v1.2"};
248 segment.Cut(str, words);
249 ASSERT_EQ(words, vector<string>(res, res + sizeof(res)/sizeof(res[0])));
250 }
251 }
252
253 TEST(FullSegment, Test1) {
254 FullSegment segment(TEST_DATA_DIR "/extra_dict/jieba.dict.small.utf8");
255 vector<string> words;
256 string s;
257
258 segment.Cut("我来自北京邮电大学", words);
259 s << words;
260 ASSERT_EQ(s, "[\"我\", \"来自\", \"北京\", \"北京邮电大学\", \"邮电\", \"电大\", \"大学\"]");
261
262
263 segment.Cut("上市公司CEO", words);
264 s << words;
265 ASSERT_EQ(s, "[\"上市\", \"公司\", \"C\", \"E\", \"O\"]");
266 }
267
268 TEST(FullSegment, NullDictUnitDoesNotSkipFollowingRune) {
269 FullSegment segment(DICT_DIR "/jieba.dict.utf8");
270 vector<string> words;
271
272 segment.Cut("崎岖的牙齿", words);
273 ASSERT_EQ("崎岖/的/牙齿", Join(words.begin(), words.end(), "/"));
274 }
275
276 TEST(QuerySegment, Test1) {
277 QuerySegment segment(DICT_DIR "/jieba.dict.utf8", DICT_DIR "/hmm_model.utf8", "");
278 vector<string> words;
279 string s1, s2;
280
281 segment.Cut("小明硕士毕业于中国科学院计算所,后在日本京都大学深造", words);
282 s1 = Join(words.begin(), words.end(), "/");
283 s2 = "小明/硕士/毕业/于/中国/科学/学院/科学院/中国科学院/计算/计算所/,/后/在/日本/京都/大学/日本京都大学/深造";
284 ASSERT_EQ(s1, s2);
285
286 segment.Cut("亲口交代", words);
287 s1 = Join(words.begin(), words.end(), "/");
288 s2 = "亲口/交代";
289 ASSERT_EQ(s1, s2);
290
291 segment.Cut("他心理健康", words);
292 s1 = Join(words.begin(), words.end(), "/");
293 s2 = "他/心理/健康/心理健康";
294 ASSERT_EQ(s1, s2);
295 }
296
297 TEST(QuerySegment, Test2) {
298 QuerySegment segment(TEST_DATA_DIR "/extra_dict/jieba.dict.small.utf8",
299 DICT_DIR "/hmm_model.utf8",
300 TEST_DATA_DIR "/userdict.utf8|" TEST_DATA_DIR "/userdict.english");
301 vector<string> words;
302 string s1, s2;
303
304 {
305 segment.Cut("小明硕士毕业于中国科学院计算所,后在日本京都大学深造", words);
306 s1 = Join(words.begin(), words.end(), "/");
307 s2 = "小明/硕士/毕业/于/中国/科学/学院/科学院/中国科学院/计算/计算所/,/后/在/日本/京都/大学/京都大学/深造";
308 ASSERT_EQ(s1, s2);
309 }
310
311 {
312 segment.Cut("小明硕士毕业于中国科学院计算所iPhone6", words);
313 s1 = Join(words.begin(), words.end(), "/");
314 s2 = "小明/硕士/毕业/于/中国/科学/学院/科学院/中国科学院/计算/计算所/iPhone6";
315 ASSERT_EQ(s1, s2);
316 }
317
318 {
319 segment.Cut("中国科学院", words);
320 s1 = Join(words.begin(), words.end(), "/");
321 s2 = "中国/科学/学院/科学院/中国科学院";
322 ASSERT_EQ(s1, s2);
323 }
324 }
325
326 TEST(MPSegmentTest, Unicode32) {
327 string s("天气很好,🙋 我们去郊游。");
328 vector<string> words;
329
330 MPSegment segment(DICT_DIR "/jieba.dict.utf8");
331 segment.Cut(s, words);
332
333 ASSERT_EQ(Join(words.begin(), words.end(), "/"), "天气/很/好/,/🙋/ /我们/去/郊游/。");
334 }
335
336 // Regression test for heap corruption ("corrupted size vs. prev_size") with
337 // input strings larger than 2114 bytes. The bug was caused by using
338 // LocalVector<RuneStr> (only safe for primitive types) for RuneStrArray.
339 TEST(MixSegmentTest, LongInput) {
340 // 2114 is the byte length threshold beyond which the original heap
341 // corruption was triggered when RuneStrArray used LocalVector<RuneStr>.
342 const size_t HEAP_CORRUPTION_THRESHOLD = 2114;
343
344 MixSegment segment(DICT_DIR "/jieba.dict.utf8", DICT_DIR "/hmm_model.utf8");
345 vector<string> words;
346
347 // Test with a long Chinese string (> HEAP_CORRUPTION_THRESHOLD bytes)
348 string phrase = "我来到北京清华大学进行学习和研究工作,非常愉快,让我有了很大的收获。";
349 string long_chinese;
350 while (long_chinese.size() < HEAP_CORRUPTION_THRESHOLD + 1000) {
351 long_chinese += phrase;
352 }
353 ASSERT_GT(long_chinese.size(), HEAP_CORRUPTION_THRESHOLD);
354 segment.Cut(long_chinese, words);
355 ASSERT_GT(words.size(), size_t(0));
356
357 // Test with a long ASCII string (> HEAP_CORRUPTION_THRESHOLD bytes)
358 string long_ascii(HEAP_CORRUPTION_THRESHOLD + 1000, 'a');
359 words.clear();
360 segment.Cut(long_ascii, words);
361 ASSERT_GT(words.size(), size_t(0));
362
363 // Test with a very long string (> 6000 bytes)
364 string very_long_chinese;
365 while (very_long_chinese.size() < 6000) {
366 very_long_chinese += phrase;
367 }
368 ASSERT_GT(very_long_chinese.size(), size_t(6000));
369 words.clear();
370 segment.Cut(very_long_chinese, words);
371 ASSERT_GT(words.size(), size_t(0));
372 }