File: testtable.cpp

package info (click to toggle)
libime 1.1.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,612 kB
  • sloc: cpp: 40,794; ansic: 952; python: 130; sh: 32; makefile: 11
file content (431 lines) | stat: -rw-r--r-- 14,862 bytes parent folder | download
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
/*
 * SPDX-FileCopyrightText: 2017-2017 CSSlayer <wengxt@gmail.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include <unistd.h>
#include <cstddef>
#include <cstdint>
#include <ios>
#include <iostream>
#include <optional>
#include <ostream>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <vector>
#include <fcitx-utils/log.h>
#include "libime/core/segmentgraph.h"
#include "libime/table/tablebaseddictionary.h"
#include "libime/table/tabledecoder.h"
#include "libime/table/tableoptions.h"
#include "libime/table/tablerule.h"
#include "testdir.h"

using namespace libime;

void testMatch(const TableBasedDictionary &dict, std::string_view code,
               const std::set<std::string> &expect, bool exact) {
    std::set<std::string> actual;
    dict.matchWords(code,
                    exact ? TableMatchMode::Exact : TableMatchMode::Prefix,
                    [&actual](std::string_view, std::string_view word, uint32_t,
                              PhraseFlag) {
                        actual.insert(std::string{word});
                        return true;
                    });
    FCITX_ASSERT(expect == actual)
        << "Expect: " << expect << " Actual: " << actual;
}

void testMatchIndex(const TableBasedDictionary &dict, std::string_view code,
                    uint32_t index) {
    std::optional<uint32_t> actual;
    dict.matchWords(code, TableMatchMode::Prefix,
                    [&actual](std::string_view, std::string_view,
                              uint32_t index, PhraseFlag) {
                        actual = index;
                        return true;
                    });
    FCITX_ASSERT(index == actual)
        << "Expect: " << index << " Actual: " << actual;
}

void testWubi() {

    std::string test = "KeyCode=abcdefghijklmnopqrstuvwxy\n"
                       "Length=4\n"
                       "Pinyin=@\n"
                       "[Rule]\n"
                       "e2=p11+p12+p21+p22\n"
                       "e3=p11+p21+p31+p32\n"
                       "a4=p11+p21+p31+n11\n"
                       "[Data]\n"
                       "xycq 统\n"
                       "yfh 计\n"
                       "nnkd 局\n";

    std::stringstream ss(test);

    try {
        libime::TableBasedDictionary table;
        table.load(ss, libime::TableFormat::Text);
        FCITX_ASSERT(table.hasRule());
        FCITX_ASSERT(table.hasPinyin());
        std::string key;
        FCITX_ASSERT(!table.generate("你好", key));
        FCITX_ASSERT(table.generate("统计局", key));
        FCITX_ASSERT(table.wordExists("xynn", "统计局") == PhraseFlag::Invalid);
        FCITX_ASSERT(key == "xynn");

        FCITX_ASSERT(table.insert("wq", "你"));
        FCITX_ASSERT(table.insert("wqiy", "你"));
        FCITX_ASSERT(table.insert("v", "好"));
        FCITX_ASSERT(table.insert("vbg", "好"));

        table.save(LIBIME_BINARY_DIR "/test/testtable.dict");
        table.statistic();

        table.load(LIBIME_BINARY_DIR "/test/testtable.dict");
        table.statistic();
        // table.save(std::cout, libime::TableFormat::Text);

        std::string key2;
        FCITX_ASSERT(table.generate("统计局", key2));
        FCITX_ASSERT(key == key2);
        FCITX_ASSERT(table.generate("你好", key2));
        FCITX_ASSERT(key2 == "wqvb") << key2;
        std::string key3;
        FCITX_ASSERT(table.generateWithHint("你好", {"abcd", "efgh"}, key3));
        FCITX_ASSERT(key3 == "abef");
        FCITX_ASSERT(table.generateWithHint("你好", {"abcd"}, key3));
        FCITX_ASSERT(key3 == "abvb");
        FCITX_ASSERT(table.generateWithHint("你好", {"", "abcd"}, key3));
        FCITX_ASSERT(key3 == "wqab");
        FCITX_ASSERT(table.generateWithHint("你好", {"", "a"}, key3));
        FCITX_ASSERT(key3 == "wqvb");
        FCITX_ASSERT(table.insert("你好"));
        testMatch(table, "wqvb", {"你好"}, false);
        testMatch(table, "wqvb", {"你好"}, true);
        testMatch(table, "w", {"你", "你好"}, false);
        testMatch(table, "wq", {"你", "你好"}, false);
        testMatch(table, "w", {}, true);
        table.insert("wo", "我", PhraseFlag::Pinyin);
        testMatch(table, "w", {"你", "你好", "我"}, false);

        FCITX_ASSERT(table.reverseLookup("你") == "wqiy");
        FCITX_ASSERT(table.reverseLookup("好") == "vbg");
        table.statistic();
        table.save(std::cout, TableFormat::Text);

        TableOptions options;
        options.setAutoRuleSet({"e2"});
        table.setTableOptions(options);

        graphForCode("wqvb", table)
            .dfs([](const SegmentGraphBase &graph,
                    const std::vector<size_t> &path) {
                size_t s = 0;
                for (auto e : path) {
                    std::cout << graph.segment(s, e) << " ";
                    s = e;
                }
                std::cout << std::endl;
                return true;
            });
        FCITX_ASSERT(table.wordExists("wqvb", "你好") == PhraseFlag::None);
        FCITX_ASSERT(table.wordExists("xyyf", "统计") == PhraseFlag::Invalid);
        FCITX_ASSERT(table.insert("统计", PhraseFlag::User));
        FCITX_ASSERT(table.wordExists("xyyf", "统计") == PhraseFlag::User);
    } catch (std::ios_base::failure &e) {
        std::cout << e.what() << std::endl;
    }
}

void testCangjie() {

    std::string test = "KeyCode=abcdefghijklmnopqrstuvwxyz\n"
                       "Length=6\n"
                       "Prompt=&\n"
                       "[Data]\n"
                       "&a 日\n"
                       "&b 月\n"
                       "&c 金\n"
                       "a 日\n"
                       "a 曰\n"
                       "aa 昌\n"
                       "aaa 晶\n"
                       "abac 暝\n";

    std::stringstream ss(test);

    try {
        libime::TableBasedDictionary table;
        table.load(ss, libime::TableFormat::Text);
        table.save(std::cout, TableFormat::Text);
        FCITX_ASSERT(!table.hasRule());
        std::string key;
        FCITX_ASSERT(!table.generate("你好", key));
        FCITX_ASSERT(table.hint("abac") == "日月日金");
    } catch (std::ios_base::failure &e) {
        std::cout << e.what() << std::endl;
    }
}

void testCangjiePhrase() {

    std::string test = "KeyCode=abcdefghijklmnopqrstuvwxyz\n"
                       "Length=6\n"
                       "Prompt=&\n"
                       "[Rule]\n"
                       "e2=p11+p1z+p21+p22+p2z\n"
                       "e3=p11+p1z+p21+p2z+p3z\n"
                       "a4=p11+p2z+p31+n2z+n1z\n"
                       "[Data]\n"
                       "&a 日\n"
                       "&b 月\n"
                       "&c 金\n"
                       "a 日\n"
                       "a 曰\n"
                       "aa 昌\n"
                       "aaa 晶\n"
                       "abac 暝\n";

    std::stringstream ss(test);

    try {
        libime::TableBasedDictionary table;
        table.load(ss, libime::TableFormat::Text);
        table.save(std::cout, TableFormat::Text);
        FCITX_ASSERT(table.hasRule());
        std::string key;
        FCITX_ASSERT(table.generate("日日", key));
        FCITX_ASSERT(key == "aa") << key;
        FCITX_ASSERT(table.generate("日日日日", key));
        FCITX_ASSERT(key == "aaaa") << key;
        FCITX_ASSERT(table.generate("昌日", key));
        FCITX_ASSERT(key == "aaa") << key;
        FCITX_ASSERT(table.generate("日昌", key));
        FCITX_ASSERT(key == "aaa") << key;
        FCITX_ASSERT(table.generate("昌暝", key));
        FCITX_ASSERT(key == "aaabc") << key;
        FCITX_ASSERT(table.generate("暝暝", key));
        FCITX_ASSERT(key == "acabc") << key;
        FCITX_ASSERT(table.hint("abac") == "日月日金");
    } catch (std::ios_base::failure &e) {
        std::cout << e.what() << std::endl;
    }
}

void testRule() {
    TableRule rule("e2=p11+p12+p21+p22+p00", 5);
    FCITX_ASSERT(rule.entries().size() == 5);
    bool ex = false;
    try {
        TableRule ruleBad("e2=p11+p12+p21+p22+p10", 5);
    } catch (...) {
        ex = true;
    }
    FCITX_ASSERT(ex);
    ex = false;
    try {
        TableRule ruleBad("e2=p11+p12+p21+p22+p01", 5);
    } catch (...) {
        ex = true;
    }
    FCITX_ASSERT(ex);
}

void testPhraseSection() {

    std::string test = "KeyCode=abcdefghijklmnopqrstuvwxy\n"
                       "Length=4\n"
                       "Pinyin=@\n"
                       "[Rule]\n"
                       "e2=p11+p12+p21+p22\n"
                       "e3=p11+p21+p31+p32\n"
                       "a4=p11+p21+p31+n11\n"
                       "[Data]\n"
                       "xycq 统\n"
                       "yfh 计\n"
                       "nnkd 局\n"
                       "[Phrase]\n"
                       "统计局";

    std::stringstream ss(test);

    try {
        libime::TableBasedDictionary table;
        table.load(ss, libime::TableFormat::Text);
        FCITX_ASSERT(table.hasRule());
        FCITX_ASSERT(table.hasPinyin());
        FCITX_ASSERT(table.wordExists("xynn", "统计局") == PhraseFlag::None);
    } catch (std::ios_base::failure &e) {
        std::cout << e.what() << std::endl;
    }
}
void testInvalidPhraseSection() {

    std::string test = "KeyCode=abcdefghijklmnopqrstuvwxy\n"
                       "Length=4\n"
                       "Pinyin=@\n"
                       "[Data]\n"
                       "xycq 统\n"
                       "yfh 计\n"
                       "nnkd 局\n"
                       "[Phrase]\n"
                       "统计局";

    std::stringstream ss(test);
    bool ex = false;
    try {
        libime::TableBasedDictionary table;
        table.load(ss, libime::TableFormat::Text);
    } catch (const std::invalid_argument &) {
        ex = true;
    }
    FCITX_ASSERT(ex);
}

void testEscape() {
    std::string test = "KeyCode=abcdefghijklmnopqrstuvwxy\n"
                       "Length=4\n"
                       "Pinyin=@\n"
                       "[Rule]\n"
                       "e2=p11+p12+p21+p22\n"
                       "e3=p11+p21+p31+p32\n"
                       "a4=p11+p21+p31+n11\n"
                       "[Data]\n"
                       "xycq 统\n"
                       "yfh 计\n"
                       "nnkd 局\n"
                       "aaaa \"工 \"\n"
                       "f \"\n"
                       "[Phrase]\n"
                       "\"统计局\"";

    std::string expect = "KeyCode=abcdefghijklmnopqrstuvwxy\n"
                         "Length=4\n"
                         "Pinyin=@\n"
                         "[Rule]\n"
                         "e2=p11+p12+p21+p22\n"
                         "e3=p11+p21+p31+p32\n"
                         "a4=p11+p21+p31+n11\n"
                         "[Data]\n"
                         "xycq 统\n"
                         "yfh 计\n"
                         "nnkd 局\n"
                         "aaaa \"工 \"\n"
                         "f \"\\\"\"\n"
                         "xynn 统计局\n";

    std::stringstream ss(test);
    libime::TableBasedDictionary table;
    table.load(ss, libime::TableFormat::Text);
    std::stringstream out;
    table.save(out, TableFormat::Text);
    FCITX_ASSERT(out.str() == expect) << out.str();
}

void testExtraDict() {
    libime::TableBasedDictionary table;
    {
        std::string test = "KeyCode=abcdefghijklmnopqrstuvwxy\n"
                           "Length=4\n"
                           "Pinyin=@\n"
                           "[Rule]\n"
                           "e2=p11+p12+p21+p22\n"
                           "e3=p11+p21+p31+p32\n"
                           "a4=p11+p21+p31+n11\n"
                           "[Data]\n"
                           "xycq 统\n"
                           "yfh 计\n"
                           "nnkd 局\n";
        std::stringstream ss(test);
        table.load(ss, libime::TableFormat::Text);
    }
    testMatch(table, "nnkh", {}, false);
    size_t extraIndex;
    {
        std::string test = "nnkh 快跑\n";
        std::stringstream ss(test);
        extraIndex = table.loadExtra(ss, TableFormat::Text);
    }
    FCITX_ASSERT(extraIndex == 0);
    table.saveExtra(extraIndex, std::cout, libime::TableFormat::Text);

    testMatch(table, "nnkh", {"快跑"}, false);
    testMatchIndex(table, "xycq", 0);
    testMatchIndex(table, "yfh", 1);
    testMatchIndex(table, "nnkd", 2);
    testMatchIndex(table, "nnkh", 3);
    {
        std::string test = "[Phrase]\n统计局\n";
        std::stringstream ss(test);
        extraIndex = table.loadExtra(ss, TableFormat::Text);
    }
    FCITX_ASSERT(extraIndex == 1);
    table.saveExtra(extraIndex, std::cout, libime::TableFormat::Text);
    testMatchIndex(table, "xynn", 4);

    // Test load [词组]
    table.removeAllExtra();
    {
        std::string test = "[词组]\n统计局\n";
        std::stringstream ss(test);
        extraIndex = table.loadExtra(ss, TableFormat::Text);
    }
    FCITX_ASSERT(extraIndex == 0);
    table.saveExtra(extraIndex, std::cout, libime::TableFormat::Text);
    testMatchIndex(table, "xynn", 3);
}

void testOneMatchingWord() {

    std::string test = "KeyCode=abcdefghijklmnopqrstuvwxy\n"
                       "Length=4\n"
                       "Pinyin=@\n"
                       "[Rule]\n"
                       "e2=p11+p12+p21+p22\n"
                       "e3=p11+p21+p31+p32\n"
                       "a4=p11+p21+p31+n11\n"
                       "[Data]\n"
                       "xycq 统\n"
                       "yfh 计\n"
                       "nnkd 局\n"
                       "nnkh 快跑\n";
    std::stringstream ss(test);
    libime::TableBasedDictionary table;
    table.load(ss, libime::TableFormat::Text);
    FCITX_ASSERT(table.hasOneMatchingWord("x"));
    FCITX_ASSERT(table.hasOneMatchingWord("xy"));
    FCITX_ASSERT(table.hasOneMatchingWord("xyc"));
    FCITX_ASSERT(table.hasOneMatchingWord("xycq"));
    FCITX_ASSERT(!table.hasOneMatchingWord("xycy"));
    FCITX_ASSERT(!table.hasOneMatchingWord("n"));
    FCITX_ASSERT(!table.hasOneMatchingWord("nn"));
    FCITX_ASSERT(!table.hasOneMatchingWord("nnk"));
    FCITX_ASSERT(table.hasOneMatchingWord("nnkh"));
    FCITX_ASSERT(table.hasOneMatchingWord("nnkd"));
    table.insert("nnkd", "局", libime::PhraseFlag::User);
    FCITX_ASSERT(table.hasOneMatchingWord("nnkd"));
    table.insert("nnkd", "局2", libime::PhraseFlag::User);
    FCITX_ASSERT(!table.hasOneMatchingWord("nnkd"));
}

int main() {
    testRule();
    testWubi();
    testCangjie();
    testCangjiePhrase();
    testPhraseSection();
    testInvalidPhraseSection();
    testEscape();
    testOneMatchingWord();
    testExtraDict();

    return 0;
}