File: testtableime_unit.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 (108 lines) | stat: -rw-r--r-- 3,040 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
/*
 * SPDX-FileCopyrightText: 2017-2017 CSSlayer <wengxt@gmail.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include <string>
#include <string_view>
#include <fcitx-utils/log.h>
#include "libime/core/languagemodel.h"
#include "libime/core/userlanguagemodel.h"
#include "libime/table/tablebaseddictionary.h"
#include "libime/table/tablecontext.h"
#include "libime/table/tableoptions.h"
#include "testdir.h"

using namespace libime;

class TestLmResolver : public LanguageModelResolver {
public:
    TestLmResolver(std::string_view path) : path_(path) {}

protected:
    std::string
    languageModelFileNameForLanguage(const std::string &language) override {
        if (language == "zh_CN") {
            return path_;
        }
        return {};
    }

private:
    std::string path_;
};

int main() {
    fcitx::Log::setLogRule("*=5");
    TestLmResolver lmresolver(LIBIME_BINARY_DIR "/data/sc.lm");
    auto lm = lmresolver.languageModelFileForLanguage("zh_CN");
    TableBasedDictionary dict;
    UserLanguageModel model(lm);
    dict.load(LIBIME_BINARY_DIR "/data/wbx.main.dict");
    TableOptions options;
    options.setLanguageCode("zh_CN");
    options.setLearning(true);
    options.setAutoPhraseLength(-1);
    options.setAutoSelect(true);
    options.setAutoSelectLength(-1);
    options.setNoMatchAutoSelectLength(-1);
    options.setNoSortInputLength(2);
    options.setAutoRuleSet({});
    options.setMatchingKey('z');
    options.setOrderPolicy(OrderPolicy::Freq);
    dict.setTableOptions(options);
    TableContext c(dict, model);

    // This candidate does not exist in table, should not be selected.
    c.type("qfgo");
    FCITX_ASSERT(!c.selected());

    c.clear();
    c.type("qfgop");
    FCITX_ASSERT(!c.selected());
    c.clear();

    c.type("vb");

    for (const auto &candidate : c.candidates()) {
        FCITX_INFO() << candidate.toString() << candidate.score();
    }
    c.select(0);
    c.learn();
    c.clear();

    c.type("bbh");

    for (const auto &candidate : c.candidates()) {
        FCITX_INFO() << candidate.toString() << candidate.score();
    }
    c.select(0);
    c.learn();
    c.clear();
    c.learnAutoPhrase("好耶");
    FCITX_ASSERT(c.dict().wordExists("vbbb", "好耶") ==
                 libime::PhraseFlag::Auto);
    c.learnAutoPhrase("好耶", {"ky", "cy"});
    FCITX_ASSERT(c.dict().wordExists("kycy", "好耶") ==
                 libime::PhraseFlag::Auto);
    c.learnAutoPhrase("萌豚萌", {"mbsd", "tdk,", "mbsd"});
    FCITX_ASSERT(c.dict().wordExists("mtmb", "萌豚萌") ==
                 libime::PhraseFlag::Auto);
    FCITX_ASSERT(c.dict().wordExists("tdmb", "豚萌") ==
                 libime::PhraseFlag::Auto);

    for (int i = 0; i < 2; i++) {
        c.type("vbbb");

        for (const auto &candidate : c.candidates()) {
            FCITX_INFO() << candidate.toString() << candidate.score();
        }
        c.select(1);
        c.learn();
        c.clear();
        FCITX_INFO() << "========================";
    }

    return 0;
}