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

#include <unistd.h>
#include <chrono>
#include <fstream>
#include <iostream>
#include <string_view>
#include <fcitx-utils/log.h>
#include "libime/core/utils.h"
#include "libime/core/utils_p.h"
#include "libime/pinyin/pinyindictionary.h"

void usage(const char *argv0) {
    std::cout << "Usage: " << argv0 << " [-d] <source> <dest>\n"
              << "-d: Dump binary to text\n"
              << "-v: Show debug message\n"
              << "-h: Show this help\n";
}

int main(int argc, char *argv[]) {

    bool dump = false;
    int c;
    while ((c = getopt(argc, argv, "dhv")) != -1) {
        switch (c) {
        case 'd':
            dump = true;
            break;
        case 'v':
            fcitx::Log::setLogRule("libime=5");
            break;
        case 'h':
            usage(argv[0]);
            return 0;
        default:
            usage(argv[0]);
            return 1;
        }
    }

    if (optind + 2 != argc) {
        usage(argv[0]);
        return 1;
    }
    using namespace libime;
    PinyinDictionary dict;

    auto t0 = std::chrono::high_resolution_clock::now();
    dict.load(PinyinDictionary::SystemDict, argv[optind],
              dump ? PinyinDictFormat::Binary : PinyinDictFormat::Text);
    LIBIME_DEBUG() << "Load pinyin dict: " << millisecondsTill(t0);

    std::ofstream fout;
    std::ostream *out;
    if (std::string_view(argv[optind + 1]) == "-") {
        out = &std::cout;
    } else {
        fout.open(argv[optind + 1], std::ios::out | std::ios::binary);
        out = &fout;
    }
    dict.save(PinyinDictionary::SystemDict, *out,
              dump ? PinyinDictFormat::Text : PinyinDictFormat::Binary);
    return 0;
}