Debian Codegraph @

src/util/dpinyin.cpp

raw
1 // SPDX-FileCopyrightText: 2019 - 2023 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "dpinyin.h"
6
7 #include <QFile>
8 #include <QSet>
9 #include <QTextStream>
10 #include <QMap>
11 #include <QDebug>
12
13 DCORE_BEGIN_NAMESPACE
14
15 static QHash<uint, QString> dict = {};
16 const char kDictFile[] = ":/dpinyin/resources/dpinyin.dict";
17
18 static void InitDict() {
19 if (!dict.isEmpty()) {
20 return;
21 }
22
23 dict.reserve(25333);
24
25 QFile file(kDictFile);
26
27 if (!file.open(QIODevice::ReadOnly))
28 return;
29
30 QByteArray content = file.readAll();
31
32 file.close();
33
34 QTextStream stream(&content, QIODevice::ReadOnly);
35
36 while (!stream.atEnd()) {
37 const QString line = stream.readLine();
38 // comment
39 if (line.startsWith("#"))
40 continue;
41 const QStringList items = line.left(line.indexOf("#")).split(QChar(':'));
42
43 if (items.size() == 2) {
44 dict.insert(items[0].toUInt(nullptr, 16), items[1].trimmed());
45 }
46 }
47 }
48
49 static void initToneTable(QMap<QChar, QString> &toneTable)
50 {
51 if (toneTable.size() > 0)
52 return;
53
54 const QString ts = "aāáǎà,oōóǒò,eēéěè,iīíǐì,uūúǔù,vǖǘǚǜ";
55
56 for (const QString &s : ts.split(",")) {
57 for (int i = 1; i < s.length(); ++i) {
58 toneTable.insert(s.at(i), QString("%1%2").arg(s.at(0)).arg(i));
59 }
60 }
61 }
62
63 static QString toned(const QString &str, ToneStyle ts)
64 {
65 // TS_Tone is default
66 if (ts == TS_Tone)
67 return str;
68
69 static QMap<QChar, QString> toneTable; // {ā, a1}
70 initToneTable(toneTable);
71
72 QString newStr = str;
73 QString cv;
74 for (QChar c : str) {
75 if (!toneTable.contains(c))
76 continue;
77
78 cv = toneTable.value(c);
79 switch (ts) {
80 case TS_NoneTone:
81 newStr.replace(c, cv.left(1));
82 break;
83 case TS_ToneNum:
84 newStr.replace(c, cv);
85 break;
86 default:
87 break;
88 }
89 }
90 return newStr;
91 }
92
93 static QStringList toned(const QStringList &words, ToneStyle ts)
94 {
95 QStringList tonedWords;
96 for (auto str : words)
97 tonedWords << toned(str, ts);
98
99 return tonedWords;
100 }
101
102 static QStringList permutations(const QStringList &list1, const QStringList &list2)
103 {
104 QStringList ret;
105 for (const QString &str1 : list1)
106 for (const QString &str2 : list2)
107 ret << str1 + str2;
108
109 return ret;
110 }
111
112 static QStringList permutations(const QList<QStringList> &pyList)
113 {
114 QStringList result;
115
116 if (pyList.size() <= 1)
117 return pyList.value(0, result);
118
119 result = permutations(pyList.value(0), pyList.value(1));
120
121 for (int i = 2; i < pyList.size(); ++i) {
122 result = permutations(result, pyList.value(i));
123
124 // 限制返回的大小,
125 if (result.size() > 0xFFFF) {
126 qWarning() << "Warning: Too many combinations have exceeded the limit\n";
127 break;
128 }
129 }
130
131 return result;
132 }
133
134 static QStringList deduplication(const QStringList &list)
135 {
136 QStringList result;
137 for (const QString &item : list)
138 if (!result.contains(item))
139 result.append(item);
140
141 return result;
142 }
143
144 /*!
145 \fn QString Dtk::Core::Chinese2Pinyin(const QString &words)
146 \brief Convert Chinese characters to Pinyin
147 \note this function not support polyphonic characters support.
148 \return pinyin of the words
149 \sa Dtk::Core::pinyin
150 */
151 QString Chinese2Pinyin(const QString &words)
152 {
153 QStringList res = pinyin(words, TS_ToneNum);
154
155 return res.value(0);
156 }
157
158 /*!
159 * @~english
160 \enum Dtk::Core::ToneStyle
161 pinyin tone style
162
163 \value TS_NoneTone pinyin without tone
164
165 \value TS_Tone pinyin with tone, default style in dictory file
166
167 \value TS_ToneNum pinyin tone number
168
169 */
170
171 /*!
172 \fn QStringList Dtk::Core::pinyin(const QString &words, ToneStyle ts, bool *ok)
173 \brief Convert Chinese characters to Pinyin with polyphonic characters support.
174
175 \return pinyin list of the words
176 */
177 QStringList pinyin(const QString &words, ToneStyle ts, bool *ok)
178 {
179 if (words.length() < 1)
180 return QStringList();
181
182 InitDict();
183
184 if (ok)
185 *ok = true;
186 QList<QStringList> pyList;
187 for (int i = 0; i < words.length(); ++i) {
188 const uint key = words.at(i).unicode();
189 auto find_result = dict.find(key);
190
191 if (find_result != dict.end()) {
192 const QString &ret = find_result.value();
193 pyList << toned(ret.split(","), ts);
194
195 } else {
196 pyList << QStringList(words.at(i));
197 // 部分字没有在词典中找到,使用字本身, ok 可以判断结果
198 if (ok)
199 *ok = false;
200 }
201 }
202
203 return deduplication(permutations(pyList));
204 }
205
206 /*!
207 \fn QStringList Dtk::Core::firstLetters(const QString &words)
208 \brief Convert Chinese characters to Pinyin firstLetters list
209 \brief with polyphonic characters support.
210
211 \return pinyin first letters list of the words
212 */
213 QStringList firstLetters(const QString &words)
214 {
215 QList<QStringList> result;
216 bool ok = false;
217 for (const QChar &w : words) {
218 QStringList pys = pinyin(w, TS_Tone, &ok);
219 if (!ok) {
220 result << QStringList(w);
221 continue;
222 }
223
224 for (QString &py : pys)
225 py = py.left(1);
226
227 result << pys;
228 }
229
230 return deduplication(permutations(result));
231 }
232
233 DCORE_END_NAMESPACE