Debian Codegraph @

tools/ch2py/main.cpp

raw
1 // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include <QCoreApplication>
6 #include <QCommandLineParser>
7 #include <QElapsedTimer>
8 #include <iostream>
9
10 #include "dpinyin.h"
11
12 DCORE_USE_NAMESPACE
13
14 int main(int argc, char **argv)
15 {
16 QCoreApplication a(argc, argv);
17 a.setOrganizationName("deepin");
18 a.setApplicationName("ch2py");
19 a.setApplicationVersion("0.0.1");
20
21 QCommandLineParser cp;
22 cp.setApplicationDescription("ch2py tool is a command tool that convert Chinese words to Pinyin.\n"
23 "The commands of DCI tools can be expressed as follows:\n"
24 "\t ch2py [chinese words]\n"
25 "\t ch2py --tonestyle notones [chinese words]\n"
26 "\t ch2py --letters [chinese words]\n"
27 );
28
29
30 QCommandLineOption tonestyle = QCommandLineOption(QStringList() << "s" << "tonestyle",
31 "tone style value can be \"notones\",\"tones\",\"numtones\"",
32 "tonestyle",
33 "tones");
34 QCommandLineOption letters = QCommandLineOption(QStringList() << "l" << "letters",
35 "convert Chinese words to Pinyin first letters");
36 cp.addOption(tonestyle);
37 cp.addOption(letters);
38 cp.addPositionalArgument("words", "words to be converted to pinyin");
39 cp.addHelpOption();
40
41 cp.process(a);
42
43 QString words = cp.positionalArguments().join(" ");
44
45 if (words.isEmpty()) {
46 cp.showHelp();
47 }
48
49 QString tones = cp.value("tonestyle");
50
51 ToneStyle ts = TS_Tone;
52 if (!tones.compare("notones"))
53 ts = TS_NoneTone;
54 else if (!tones.compare("numtones"))
55 ts = TS_ToneNum;
56
57 QElapsedTimer timer;
58 timer.start();
59
60 qint64 size = -1;
61 if (cp.isSet(letters)) {
62 const auto &ls = firstLetters(words);
63 printf("%s\n", qPrintable(ls.join("\n")));
64 size = ls.size();
65 } else {
66 const auto &py = pinyin(words, ts);
67 printf("%s\n", qPrintable(py.join("\n")));
68 size = py.size();
69 }
70
71 std:: cout << "Total size: " << size << ", time:" << timer.elapsed() << " ms" << std::endl;
72 return 0;
73 }