File: testdictionary.cpp

package info (click to toggle)
libime-jyutping 1.0.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,152 kB
  • sloc: cpp: 4,218; makefile: 4; sh: 1
file content (63 lines) | stat: -rw-r--r-- 2,409 bytes parent folder | download | duplicates (3)
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
/*
 * SPDX-FileCopyrightText: 2018~2018 CSSlayer <wengxt@gmail.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 */

/*
 * SPDX-FileCopyrightText: 2017~2017 CSSlayer <wengxt@gmail.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "libime/jyutping/jyutpingdictionary.h"
#include "libime/jyutping/jyutpingencoder.h"
#include "testdir.h"
#include <fcitx-utils/log.h>

int main() {
    using namespace libime;
    using namespace libime::jyutping;
    JyutpingDictionary dict;
    dict.load(JyutpingDictionary::SystemDict,
              LIBIME_BINARY_DIR "/data/jyutping.dict",
              JyutpingDictFormat::Binary);

    char c[] = {static_cast<char>(JyutpingInitial::J),
                static_cast<char>(JyutpingFinal::IN),
                static_cast<char>(JyutpingInitial::H),
                static_cast<char>(JyutpingFinal::AU)};
    dict.matchWords(c, 2,
                    [c](std::string_view encodedJyutping,
                        std::string_view hanzi, float cost) {
                        std::cout << JyutpingEncoder::decodeFullJyutping(
                                         encodedJyutping)
                                  << " " << hanzi << " " << cost << std::endl;
                        return true;
                    });
    dict.matchWords(c + 2, 2,
                    [c](std::string_view encodedJyutping,
                        std::string_view hanzi, float cost) {
                        std::cout << JyutpingEncoder::decodeFullJyutping(
                                         encodedJyutping)
                                  << " " << hanzi << " " << cost << std::endl;
                        return true;
                    });

    auto graph = JyutpingEncoder::parseUserJyutping("jinhau", true);
    dict.matchPrefix(graph,
                     [&graph](const SegmentGraphPath &path, WordNode &node,
                              float, std::unique_ptr<LatticeNodeData>) {
                         for (auto &step : path) {
                             std::cout << step->index() << " ";
                         }
                         std::cout << node.word() << std::endl;
                         return true;
                     });

    dict.save(0, LIBIME_BINARY_DIR "/test/testjyutpingdictionary.dict",
              JyutpingDictFormat::Binary);
    // dict.save(0, std::cout, JyutpingDictFormat::Text);
    return 0;
}