Debian Codegraph @

modules/pinyinhelper/pinyinlookup.cpp

raw
1 /*
2 * SPDX-FileCopyrightText: 2012-2012 Yichao Yu <[email protected]>
3 * SPDX-FileCopyrightText: 2017-2017 CSSlayer <[email protected]>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-or-later
6 *
7 */
8 #include "pinyinlookup.h"
9
10 #include <cstdint>
11 #include <fcitx-utils/cutf8.h>
12 #include <fcitx-utils/fs.h>
13 #include <fcitx-utils/log.h>
14 #include <fcitx-utils/macros.h>
15 #include <fcitx-utils/standardpaths.h>
16 #include <fcitx-utils/stringutils.h>
17 #include <fcitx-utils/utf8.h>
18 #include <fcntl.h>
19 #include <string>
20 #include <string_view>
21 #include <tuple>
22 #include <unistd.h>
23 #include <vector>
24
25 namespace fcitx {
26
27 namespace {
28
29 std::string_view py_enhance_get_vokal(int index, int tone) {
30 static const std::string_view vokals_table[][5] = {
31 {"", "", "", "", ""},
32 {"a", "ā", "á", "ǎ", "à"},
33 {"ai", "āi", "ái", "ǎi", "ài"},
34 {"an", "ān", "án", "ǎn", "àn"},
35 {"ang", "āng", "áng", "ǎng", "àng"},
36 {"ao", "āo", "áo", "ǎo", "ào"},
37 {"e", "ē", "é", "ě", "è"},
38 {"ei", "ēi", "éi", "ěi", "èi"},
39 {"en", "ēn", "én", "ěn", "èn"},
40 {"eng", "ēng", "éng", "ěng", "èng"},
41 {"er", "ēr", "ér", "ěr", "èr"},
42 {"i", "ī", "í", "ǐ", "ì"},
43 {"ia", "iā", "iá", "iǎ", "ià"},
44 {"ian", "iān", "ián", "iǎn", "iàn"},
45 {"iang", "iāng", "iáng", "iǎng", "iàng"},
46 {"iao", "iāo", "iáo", "iǎo", "iào"},
47 {"ie", "iē", "ié", "iě", "iè"},
48 {"in", "īn", "ín", "ǐn", "ìn"},
49 {"ing", "īng", "íng", "ǐng", "ìng"},
50 {"iong", "iōng", "ióng", "iǒng", "iòng"},
51 {"iu", "iū", "iú", "iǔ", "iù"},
52 {"m", "m", "m", "m", "m"},
53 {"n", "n", "ń", "ň", "ǹ"},
54 {"ng", "ng", "ńg", "ňg", "ǹg"},
55 {"o", "ō", "ó", "ǒ", "ò"},
56 {"ong", "ōng", "óng", "ǒng", "òng"},
57 {"ou", "ōu", "óu", "ǒu", "òu"},
58 {"u", "ū", "ú", "ǔ", "ù"},
59 {"ua", "uā", "uá", "uǎ", "uà"},
60 {"uai", "uāi", "uái", "uǎi", "uài"},
61 {"uan", "uān", "uán", "uǎn", "uàn"},
62 {"uang", "uāng", "uáng", "uǎng", "uàng"},
63 {"ue", "uē", "ué", "uě", "uè"},
64 {"ueng", "uēng", "uéng", "uěng", "uèng"},
65 {"ui", "uī", "uí", "uǐ", "uì"},
66 {"un", "ūn", "ún", "ǔn", "ùn"},
67 {"uo", "uō", "uó", "uǒ", "uò"},
68 {"ü", "ǖ", "ǘ", "ǚ", "ǜ"},
69 {"üan", "üān", "üán", "üǎn", "üàn"},
70 {"üe", "üē", "üé", "üě", "üè"},
71 {"ün", "ǖn", "ǘn", "ǚn", "ǜn"}};
72 static const int8_t vokals_count = FCITX_ARRAY_SIZE(vokals_table);
73 if (index < 0 || index >= vokals_count) {
74 return "";
75 }
76 if (tone < 0 || tone > 4) {
77 tone = 0;
78 }
79 return vokals_table[index][tone];
80 }
81
82 std::string_view py_enhance_get_konsonant(int index) {
83 static const std::string_view konsonants_table[] = {
84 "", "b", "c", "ch", "d", "f", "g", "h", "j", "k", "l", "m", "n",
85 "ng", "p", "q", "r", "s", "sh", "t", "w", "x", "y", "z", "zh"};
86 static const int8_t konsonants_count = FCITX_ARRAY_SIZE(konsonants_table);
87 if (index < 0 || index >= konsonants_count) {
88 return "";
89 }
90 return konsonants_table[index];
91 }
92 } // namespace
93
94 std::vector<std::string> PinyinLookup::lookup(uint32_t hz) {
95 auto iter = data_.find(hz);
96 if (iter == data_.end()) {
97 return {};
98 }
99 std::vector<std::string> result;
100 for (const auto &data : iter->second) {
101 auto c = py_enhance_get_konsonant(data.consonant);
102 auto v = py_enhance_get_vokal(data.vocal, data.tone);
103 if (c.empty() && v.empty()) {
104 continue;
105 }
106 result.emplace_back(stringutils::concat(c, v));
107 }
108 return result;
109 }
110
111 std::vector<std::tuple<std::string, std::string, int>>
112 PinyinLookup::fullLookup(uint32_t hz) {
113 auto iter = data_.find(hz);
114 if (iter == data_.end()) {
115 return {};
116 }
117 std::vector<std::tuple<std::string, std::string, int>> result;
118 for (const auto &data : iter->second) {
119 auto c = py_enhance_get_konsonant(data.consonant);
120 auto v = py_enhance_get_vokal(data.vocal, data.tone);
121 if (c.empty() && v.empty()) {
122 continue;
123 }
124 auto noToneV = py_enhance_get_vokal(data.vocal, 0);
125 result.emplace_back(stringutils::concat(c, v),
126 stringutils::concat(c, noToneV), data.tone);
127 }
128 return result;
129 }
130
131 bool PinyinLookup::load() {
132 if (loaded_) {
133 return loadResult_;
134 }
135 loaded_ = true;
136
137 auto file = StandardPaths::global().open(StandardPathsType::PkgData,
138 "pinyinhelper/py_table.mb");
139 if (!file.isValid()) {
140 return false;
141 }
142 /**
143 * Format:
144 * uint8_t word_l;
145 * char word[word_l];
146 * uint8_t count;
147 * int8_t py[count][3];
148 **/
149 while (true) {
150 char word[FCITX_UTF8_MAX_LENGTH + 1];
151 uint8_t wordLen;
152 auto res = fs::safeRead(file.fd(), &wordLen, 1);
153 if (res == 0) {
154 break;
155 }
156 if (res < 0 || wordLen > FCITX_UTF8_MAX_LENGTH) {
157 return false;
158 }
159 if (read(file.fd(), word, wordLen) != wordLen) {
160 return false;
161 }
162 word[wordLen] = '\0';
163 std::string_view view(word);
164 if (utf8::lengthValidated(view) != 1) {
165 return false;
166 }
167 uint32_t chr = utf8::getChar(view);
168 uint8_t count;
169 if (read(file.fd(), &count, 1) != 1) {
170 return false;
171 }
172 if (count == 0) {
173 continue;
174 }
175 auto &data = data_[chr];
176 while (count--) {
177 uint8_t buf[3];
178 if (read(file.fd(), buf, 3) != 3) {
179 return false;
180 }
181 data.push_back({buf[0], buf[1], buf[2]});
182 }
183 }
184 loadResult_ = true;
185 return true;
186 }
187 } // namespace fcitx