-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathtest_jp.cpp
More file actions
488 lines (388 loc) · 15 KB
/
Copy pathtest_jp.cpp
File metadata and controls
488 lines (388 loc) · 15 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#include <string.h>
#include "../common.h"
#include "../tokenSystem.h"
#include "../cs_jp.h"
#include "catch2.h"
TEST_CASE("call get_jp_token_str_1 entry", "[jp]") {
int rv;
rv = malloc_jp_token_buffers();
REQUIRE( rv == 0 );
strcpy(language, "japanese");
char input[1024] = "太郎は次郎が持っている本を花子に渡した";
rv = jp_tokenize(input);
REQUIRE( rv == 0 );
char exp_tok[] = "太郎 は 次郎 が 持っ て いる 本 を 花 子 に 渡し た ";
const char* actual_token = get_jp_token_str();
REQUIRE( strcmp(actual_token, exp_tok) == 0);
char exp_can[] = "太郎 は 次郎 が 持つ て いる 本 を 花 子 に 渡す た ";
const char* actual_canon = get_jp_canon_str();
REQUIRE(strcmp(actual_canon, exp_can) == 0);
free_jp_token_buffers();
}
TEST_CASE("call get_jp_token_str_2 entries", "[jp]") {
int rv;
rv = malloc_jp_token_buffers();
REQUIRE( rv == 0 );
strcpy(language, "japanese");
char input[1024] = "ピンチの時には必ずヒーローが現れる"
"太郎は次郎が持っている本を花子に渡した";
rv = jp_tokenize(input);
REQUIRE( rv == 0 );
char exp_tok[] =
"ピンチ の 時 に は 必ず ヒーロー が 現れる "
"太郎 は 次郎 が 持っ て いる 本 を 花 子 に 渡し た ";
const char* actual_token = get_jp_token_str();
REQUIRE( strcmp(actual_token, exp_tok) == 0);
char exp_can[] =
"ピンチ の 時 に は 必ず ヒーロー が 現れる "
"太郎 は 次郎 が 持つ て いる 本 を 花 子 に 渡す た ";
const char* actual_canon = get_jp_canon_str();
REQUIRE(strcmp(actual_canon, exp_can) == 0);
free_jp_token_buffers();
}
TEST_CASE("call get_jp_token_str english", "[jp]") {
int rv;
rv = malloc_jp_token_buffers();
REQUIRE( rv == 0 );
strcpy(language, "japanese");
char input[1024] = "he lives.";
rv = jp_tokenize(input);
REQUIRE( rv == 0 );
char exp_tok[] = "he lives.";
const char* actual_token = get_jp_token_str();
REQUIRE( strcmp(actual_token, exp_tok) == 0);
char exp_can[] = "he lives.";
const char* actual_canon = get_jp_canon_str();
REQUIRE(strcmp(actual_canon, exp_can) == 0);
free_jp_token_buffers();
}
TEST_CASE("call get_jp_token_str url", "[jp]") {
int rv;
rv = malloc_jp_token_buffers();
REQUIRE( rv == 0 );
strcpy(language, "japanese");
char input[1024] = "www.go.com";
rv = jp_tokenize(input);
REQUIRE( rv == 0 );
char exp_tok[] = "www.go.com ";
const char* actual_token = get_jp_token_str();
REQUIRE( strcmp(actual_token, exp_tok) == 0);
char exp_can[] = "www.go.com ";
const char* actual_canon = get_jp_canon_str();
REQUIRE(strcmp(actual_canon, exp_can) == 0);
free_jp_token_buffers();
}
TEST_CASE("call get_jp_token_str jp 1", "[jp]") {
int rv;
rv = malloc_jp_token_buffers();
REQUIRE( rv == 0 );
strcpy(language, "japanese");
char input[1024] = "現在チャットサポートは大変混み合っております";
rv = jp_tokenize(input);
REQUIRE( rv == 0 );
char exp_tok[] = "現在 チャット サポート は 大変 混み 合っ て おり ます ";
const char* actual_token = get_jp_token_str();
REQUIRE( strcmp(actual_token, exp_tok) == 0);
char exp_can[] = "現在 チャット サポート は 大変 混む 合う て おる ます ";
const char* actual_canon = get_jp_canon_str();
REQUIRE(strcmp(actual_canon, exp_can) == 0);
char exp_pos[] = "名詞 名詞 名詞 助詞 名詞 動詞 動詞 助詞 動詞 助動詞 ";
const char* actual_pos = get_jp_pos_str();
REQUIRE(strcmp(actual_pos, exp_pos) == 0);
free_jp_token_buffers();
}
TEST_CASE("call get_jp_token_str jp 2", "[jp]") {
int rv;
rv = malloc_jp_token_buffers();
REQUIRE( rv == 0 );
strcpy(language, "japanese");
char input[1024] = "自然言語処理を勉強してください";
rv = jp_tokenize(input);
REQUIRE( rv == 0 );
char exp_tok[] = "自然 言語 処理 を 勉強 し て ください ";
const char* actual_token = get_jp_token_str();
REQUIRE( strcmp(actual_token, exp_tok) == 0);
char exp_can[] = "自然 言語 処理 を 勉強 する て くださる ";
const char* actual_canon = get_jp_canon_str();
REQUIRE(strcmp(actual_canon, exp_can) == 0);
char exp_pos[] = "名詞 名詞 名詞 助詞 名詞 動詞 助詞 動詞 ";
const char* actual_pos = get_jp_pos_str();
REQUIRE(strcmp(actual_pos, exp_pos) == 0);
free_jp_token_buffers();
}
TEST_CASE("call get_jp_token_str jp 3", "[jp]") {
int rv;
rv = malloc_jp_token_buffers();
REQUIRE( rv == 0 );
strcpy(language, "japanese");
char input[1024] = "ピンチの時には必ずヒーローが現れる";
rv = jp_tokenize(input);
REQUIRE( rv == 0 );
char exp_tok[] = "ピンチ の 時 に は 必ず ヒーロー が 現れる ";
const char* actual_token = get_jp_token_str();
REQUIRE( strcmp(actual_token, exp_tok) == 0);
char exp_can[] = "ピンチ の 時 に は 必ず ヒーロー が 現れる ";
const char* actual_canon = get_jp_canon_str();
REQUIRE(strcmp(actual_canon, exp_can) == 0);
char exp_pos[] = "名詞 助詞 名詞 助詞 助詞 副詞 名詞 助詞 動詞 ";
const char* actual_pos = get_jp_pos_str();
REQUIRE(strcmp(actual_pos, exp_pos) == 0);
free_jp_token_buffers();
}
TEST_CASE("call get_jp_token_str jp 4", "[jp]") {
int rv;
rv = malloc_jp_token_buffers();
REQUIRE( rv == 0 );
strcpy(language, "japanese");
char input[1024] = "今日もしないとね";
rv = jp_tokenize(input);
REQUIRE( rv == 0 );
char exp_tok[] = "今日 も し ない と ね ";
const char* actual_token = get_jp_token_str();
REQUIRE( strcmp(actual_token, exp_tok) == 0);
char exp_can[] = "今日 も する ない と ね ";
const char* actual_canon = get_jp_canon_str();
REQUIRE(strcmp(actual_canon, exp_can) == 0);
char exp_pos[] = "名詞 助詞 動詞 助動詞 助詞 助詞 ";
const char* actual_pos = get_jp_pos_str();
REQUIRE(strcmp(actual_pos, exp_pos) == 0);
free_jp_token_buffers();
}
TEST_CASE("call get_jp_token_str jp 5", "[jp]") {
int rv;
rv = malloc_jp_token_buffers();
REQUIRE( rv == 0 );
strcpy(language, "japanese");
char input[1024] = "すもももももももものうち";
rv = jp_tokenize(input);
REQUIRE( rv == 0 );
char exp_tok[] = "すもも も もも も もも の うち ";
char exp_can[] = "すもも も もも も もも の うち ";
const char* actual_token = get_jp_token_str();
REQUIRE( strcmp(actual_token, exp_tok) == 0);
const char* actual_canon = get_jp_canon_str();
REQUIRE(strcmp(actual_canon, exp_can) == 0);
char exp_pos[] = "名詞 助詞 名詞 助詞 名詞 助詞 名詞 ";
const char* actual_pos = get_jp_pos_str();
REQUIRE(strcmp(actual_pos, exp_pos) == 0);
free_jp_token_buffers();
}
TEST_CASE("call get_jp_token_str jp 6", "[jp]") {
int rv;
rv = malloc_jp_token_buffers();
REQUIRE( rv == 0 );
strcpy(language, "japanese");
char input[1024] = "毎日本を読まされました";
rv = jp_tokenize(input);
REQUIRE( rv == 0 );
char exp_tok[] = "毎 日本 を 読ま さ れ まし た ";
const char* actual_token = get_jp_token_str();
REQUIRE( strcmp(actual_token, exp_tok) == 0);
char exp_can[] = "毎 日本 を 読む する れる ます た ";
const char* actual_canon = get_jp_canon_str();
REQUIRE(strcmp(actual_canon, exp_can) == 0);
char exp_pos[] = "接頭詞 名詞 助詞 動詞 動詞 動詞 助動詞 助動詞 ";
const char* actual_pos = get_jp_pos_str();
REQUIRE(strcmp(actual_pos, exp_pos) == 0);
free_jp_token_buffers();
}
TEST_CASE("call tokenize with multiple sentences", "[jp]") {
maxHeapBytes = 10000000; // 10 MB
InitStackHeap();
malloc_jp_token_buffers();
strcpy(language, "japanese");
char buffer[1024] = "私の妻は良いです。x ?www.go.com!he lives.";
char* ptr = buffer;
// char buffer[1024] = "太郎は次郎が持っている本を花子に渡した。";
int count = 2;
char* starts[MAX_SENTENCE_LENGTH];
memset(starts, 0, sizeof(char*) * MAX_SENTENCE_LENGTH);
char* separators = NULL;
bool all1 = false;
bool oobStart = false;
int sentance_number = 0;
char expected_strings[4][100] = {
"私 の 妻 は 良い です ",
"x ",
"www.go.com ",
"he lives."
};
while (ptr && *ptr) {
REQUIRE( sentance_number < 4 );
ptr = Tokenize(ptr,
count,
starts,
separators,
all1,
oobStart);
const char* token = get_jp_token_str();
const char* canon = get_jp_canon_str();
const char* expected_str = expected_strings[sentance_number];
REQUIRE( strcmp(expected_str, token) == 0 );
REQUIRE( strcmp(expected_str, canon) == 0 );
sentance_number++;
}
free_jp_token_buffers();
FreeStackHeap();
}
TEST_CASE("JapaneseNLP", "[jp]") {
maxHeapBytes = 10000000; // 10 MB
InitStackHeap();
malloc_jp_token_buffers();
strcpy(language, "japanese");
char buffer[1024] = "私の妻は良いです。x ?www.go.com!he lives.";
char* ptr = buffer;
int count = 2;
char* starts[MAX_SENTENCE_LENGTH];
memset(starts, 0, sizeof(char*) * MAX_SENTENCE_LENGTH);
char* separators = NULL;
bool all1 = false;
bool oobStart = false;
int sentance_number = 0;
char* expected_first[4] = { "私", "x", "www.go.com", "he" };
char* expected_last[4] = { ".", "?", "!", "lives." };
int expected_word_counts[4] = { 7, 2, 2, 2 };
while (ptr && *ptr) {
REQUIRE( sentance_number < 4 );
wordCount = 0;
ptr = Tokenize(ptr,
count,
starts,
separators,
all1,
oobStart);
JapaneseNLP();
REQUIRE(count == expected_word_counts[sentance_number]);
REQUIRE(strcmp(expected_first[sentance_number], starts[1]) == 0);
REQUIRE(strcmp(expected_last[sentance_number], starts[count]) == 0);
sentance_number++;
}
free_jp_token_buffers();
FreeStackHeap();
}
TEST_CASE("call get_jp_token_str_1 english with commas", "[jp]") {
int rv;
rv = malloc_jp_token_buffers();
REQUIRE( rv == 0 );
strcpy(language, "japanese");
char input[1024] = "Hello, I'm, the Expert's "
"Assistant on JustAnswer. How can I help?";
rv = jp_tokenize(input);
REQUIRE( rv == 0 );
char expected_token[] = "Hello , I ' m , the Expert ' s "
"Assistant on JustAnswer.How can I help?";
const char* actual_token = get_jp_token_str();
REQUIRE(strcmp(expected_token, actual_token) == 0);
free_jp_token_buffers();
}
TEST_CASE("find_closest_jp_period 1", "[jp]") {
char* input = "私の妻は良いです。x";
char* result = find_closest_jp_period(input);
char* expected = "。x";
REQUIRE(strcmp(expected, result) == 0);
}
TEST_CASE("find_closest_jp_period 2", "[jp]") {
char* input = "私の妻は良いです。xす。x";
char* result = find_closest_jp_period(input);
char* expected = "。xす。x";
REQUIRE(strcmp(expected, result) == 0);
}
TEST_CASE("find_closest_jp_period 3", "[jp]") {
char* input = "私.の妻は良いです。xす。x";
char* result = find_closest_jp_period(input);
char* expected = ".の妻は良いです。xす。x";
REQUIRE(strcmp(expected, result) == 0);
}
TEST_CASE("find_closest_jp_period 4", "[jp]") {
char* input = "私の妻は良いですxすx";
char* result = find_closest_jp_period(input);
char* expected = NULL;
REQUIRE(expected == result);
}
TEST_CASE("check parts of speech, single token", "[jp]") {
int rv;
rv = malloc_jp_token_buffers();
REQUIRE( rv == 0 );
char input[1024] = "太郎";
rv = jp_tokenize(input);
REQUIRE( rv == 0 );
JapaneseNLP();
char testDictionary[MAX_BUFFER_SIZE] = { '\0' };
MarkJapaneseTags(1, testDictionary);
char* expected_pos = "~名詞 ";
REQUIRE(strcmp(expected_pos, testDictionary) == 0);
free_jp_token_buffers();
}
TEST_CASE("check parts of speech, partial list", "[jp]") {
int rv;
rv = malloc_jp_token_buffers();
REQUIRE( rv == 0 );
char input[1024] = "太郎は次郎が持っている本を花子に渡した";
rv = jp_tokenize(input);
REQUIRE( rv == 0 );
JapaneseNLP();
char testDictionary[MAX_BUFFER_SIZE] = { '\0' };
MarkJapaneseTags(2, testDictionary);
char* expected_pos = "~名詞 ~助詞 ";
REQUIRE(strcmp(expected_pos, testDictionary) == 0);
free_jp_token_buffers();
}
TEST_CASE("check parts of speech, full list", "[jp]") {
int rv;
rv = malloc_jp_token_buffers();
REQUIRE( rv == 0 );
char input[1024] = "太郎は";
rv = jp_tokenize(input);
REQUIRE( rv == 0 );
JapaneseNLP();
char testDictionary[MAX_BUFFER_SIZE] = { '\0' };
MarkJapaneseTags(2, testDictionary);
char* expected_pos = "~名詞 ~助詞 ";
REQUIRE(strcmp(expected_pos, testDictionary) == 0);
free_jp_token_buffers();
}
TEST_CASE("check parts of speech, bad index", "[jp]") {
int rv;
rv = malloc_jp_token_buffers();
REQUIRE( rv == 0 );
char input[1024] = "太郎";
rv = jp_tokenize(input);
REQUIRE( rv == 0 );
JapaneseNLP();
char testDictionary[MAX_BUFFER_SIZE] = { '\0' };
MarkJapaneseTags(2, testDictionary);
char* expected_pos = "~名詞 ";
REQUIRE(strcmp(expected_pos, testDictionary) == 0);
free_jp_token_buffers();
}
TEST_CASE("check parts of speech, english", "[jp]") {
int rv;
rv = malloc_jp_token_buffers();
REQUIRE( rv == 0 );
char input[1024] = "help me.";
rv = jp_tokenize(input);
REQUIRE( rv == 0 );
JapaneseNLP();
char testDictionary[MAX_BUFFER_SIZE] = { '\0' };
MarkJapaneseTags(3, testDictionary);
char* expected_pos = "~名詞 ~名詞 ";
REQUIRE(strcmp(expected_pos, testDictionary) == 0);
free_jp_token_buffers();
}
TEST_CASE("check ConvertJapanText patterns", "[jp]") {
char input[1024] = "abcBYby09。.-_。。(){}[]def";
ConvertJapanText(input, true);
char expected[] = "abcBYby09..-_..(){}()def";
REQUIRE(strcmp(expected, input) == 0);
}
TEST_CASE("check ConvertJapanText output in variables", "[jp]") {
char input[1024] = "abc$BYby09。.-_。。(){}[]def";
ConvertJapanText(input, false);
char expected[] = "abc$BYby09..-_..(){}()def";
REQUIRE(strcmp(expected, input) == 0);
}
TEST_CASE("check ConvertJapanText output in variables 2", "[jp]") {
char input[1024] = "$BY-BY BY-BY";
ConvertJapanText(input, false);
char expected[] = "$BY-BY BY-BY";
}