Tizen 2.1 base
[platform/core/uifw/ise-engine-sunpinyin.git] / src / pinyin / shuangpin_data.cpp
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3  *
4  * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * The contents of this file are subject to the terms of either the GNU Lesser
7  * General Public License Version 2.1 only ("LGPL") or the Common Development and
8  * Distribution License ("CDDL")(collectively, the "License"). You may not use this
9  * file except in compliance with the License. You can obtain a copy of the CDDL at
10  * http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
11  * http://www.opensource.org/licenses/lgpl-license.php. See the License for the
12  * specific language governing permissions and limitations under the License. When
13  * distributing the software, include this License Header Notice in each file and
14  * include the full text of the License in the License file as well as the
15  * following notice:
16  *
17  * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
18  * (CDDL)
19  * For Covered Software in this distribution, this License shall be governed by the
20  * laws of the State of California (excluding conflict-of-law provisions).
21  * Any litigation relating to this License shall be subject to the jurisdiction of
22  * the Federal Courts of the Northern District of California and the state courts
23  * of the State of California, with venue lying in Santa Clara County, California.
24  *
25  * Contributor(s):
26  *
27  * If you wish your version of this file to be governed by only the CDDL or only
28  * the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
29  * include this software in this distribution under the [CDDL or LGPL Version 2.1]
30  * license." If you don't indicate a single choice of license, a recipient has the
31  * option to distribute your version of this file under either the CDDL or the LGPL
32  * Version 2.1, or to extend the choice of license to its licensees as provided
33  * above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
34  * Version 2 license, then the option applies only if the new code is made subject
35  * to such option by the copyright holder.
36  */
37
38 #include <stdio.h>
39 #include <stdint.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <algorithm>
43 #include <cassert>
44 #include "syllable.h"
45 #include "pinyin_data.h"
46 #include "shuangpin_data.h"
47
48 #define INITIAL_NUM 26
49 #define FINAL_NUM   34
50 #define ZEROINITIAL_NUM 12
51
52 static const char *initials[INITIAL_NUM] =
53 { "", "", "", "b", "p", "m", "f", "d", "t", "n", "l",
54   "g", "k", "h", "j", "q", "x", "zh",
55   "ch", "sh", "r", "z", "c", "s", "y", "w", };
56
57 static const char *finals[FINAL_NUM] = { "", "a", "o", "e", "ai", "ei", "ao",
58                                          "ou", "an", "en", "ang", "eng", "er",
59                                          "i", "ia", "ie", "iao", "iu", "ian",
60                                          "in", "iang", "ing", "u",
61                                          "ua", "uo", "uai", "ui", "uan", "un",
62                                          "uang", "ong", "v", "ue", "iong", };
63
64
65 //MS2003 shuangpin plan map table
66 static const char ms2003_mapinitials[INITIAL_NUM] =
67 { '\0', '\0', 'o', 'b', 'p', 'm', 'f', 'd',
68   't', 'n', 'l', 'g', 'k',
69   'h', 'j', 'q', 'x', 'v', 'i', 'u', 'r', 'z', 'c', 's',
70   'y', 'w', };
71
72 static const char ms2003_mapfinals[FINAL_NUM] =
73 { '\0', 'a', 'o', 'e', 'l', 'z', 'k',
74   'b', 'j', 'f', 'h', 'g', 'r',
75   'i', 'w', 'x', 'c', 'q', 'm', 'n', 'd', ';', 'u',
76   'w', 'o', 'y', 'v', 'r', 'p',
77   'd', 's', 'v', 't', 's', };
78
79 static TZeroInitial ms2003_zeroinitials[] = {
80     { "a", "oa" },
81     { "ai", "ol" },
82     { "an", "oj" },
83     { "ang", "oh" },
84     { "ao", "ok" },
85     { "e", "oe" },
86     { "ei", "oz" },
87     { "en", "of" },
88     { "eng", "og" },
89     { "er", "or" },
90     { "o", "oo" },
91     { "ou", "ob" },
92 };
93
94
95 //ZhiNengABC shuangpin plan map table
96 static const char abc_mapinitials[INITIAL_NUM] =
97 { '\0', '\0', 'o', 'b', 'p', 'm', 'f', 'd',
98   't', 'n', 'l', 'g', 'k', 'h',
99   'j', 'q', 'x', 'a', 'e', 'v', 'r', 'z', 'c', 's',
100   'y', 'w', };
101
102 static const char abc_mapfinals[FINAL_NUM] =
103 { '\0', 'a', 'o', 'e', 'l', 'q', 'k',
104   'b', 'j', 'f', 'h', 'g', 'r',
105   'i', 'd', 'x', 'z', 'r', 'w', 'c', 't', 'y', 'u',
106   'd', 'o', 'c', 'm', 'p', 'n',
107   't', 's', 'v', 'm', 's', };
108
109 static TZeroInitial abc_zeroinitials[] = {
110     { "a", "oa" },
111     { "ai", "ol" },
112     { "an", "oj" },
113     { "ang", "oh" },
114     { "ao", "ok" },
115     { "e", "oe" },
116     { "ei", "oq" },
117     { "en", "of" },
118     { "eng", "og" },
119     { "er", "or" },
120     { "o", "oo" },
121     { "ou", "ob" },
122 };
123
124 //ZiGuang shuangpin plan map table
125 static const char ziguang_mapinitials[INITIAL_NUM] =
126 { '\0', '\0', 'o', 'b', 'p', 'm', 'f', 'd',
127   't', 'n', 'l', 'g', 'k',
128   'h', 'j', 'q', 'x', 'u', 'a', 'i', 'r', 'z', 'c', 's',
129   'y', 'w', };
130
131 static const char ziguang_mapfinals[FINAL_NUM] =
132 { '\0', 'a', 'o', 'e', 'p', 'k', 'q',
133   'z', 'r', 'w', 's', 't', 'j',
134   'i', 'x', 'd', 'b', 'j', 'f', 'y', 'g', ';', 'u',
135   'x', 'o', 'y', 'n', 'l', 'm',
136   'g', 'h', 'v', 'n', 'h', };
137
138 static TZeroInitial ziguang_zeroinitials[] = {
139     { "a", "oa" },
140     { "ai", "op" },
141     { "an", "or" },
142     { "ang", "os" },
143     { "ao", "oq" },
144     { "e", "oe" },
145     { "ei", "ok" },
146     { "en", "ow" },
147     { "eng", "ot" },
148     { "er", "oj" },
149     { "o", "oo" },
150     { "ou", "oz" },
151 };
152
153 //PinYinJiaJia shuangpin plan map table
154 static const char pyjiajia_mapinitials[INITIAL_NUM] =
155 { '\0', '\0', 'o', 'b', 'p', 'm', 'f', 'd',
156   't', 'n', 'l', 'g', 'k',
157   'h', 'j', 'q', 'x', 'v', 'u', 'i', 'r', 'z', 'c', 's',
158   'y', 'w', };
159
160 static const char pyjiajia_mapfinals[FINAL_NUM] =
161 { '\0', 'a', 'o', 'e', 's', 'w', 'd',
162   'p', 'f', 'r', 'g', 't',
163   'q', 'i', 'b', 'm', 'k', 'n', 'j', 'l', 'h', 'q', 'u',
164   'b', 'o', 'x', 'v', 'c',
165   'z', 'h', 'y', 'v', 'x', 'y', };
166
167 static TZeroInitial pyjiajia_zeroinitials[] = {
168     { "a", "oa" },
169     { "ai", "os" },
170     { "an", "of" },
171     { "ang", "og" },
172     { "ao", "od" },
173     { "e", "oe" },
174     { "ei", "ow" },
175     { "en", "or" },
176     { "eng", "ot" },
177     { "er", "oq" },
178     { "o", "oo" },
179     { "ou", "op" },
180 };
181
182 //Ziranma shuangpin plan map table
183 static const char ziranma_mapinitials[INITIAL_NUM] =
184 { 'a', 'e', 'o', 'b', 'p', 'm', 'f', 'd',
185   't', 'n', 'l', 'g', 'k',
186   'h', 'j', 'q', 'x', 'v', 'i', 'u', 'r', 'z', 'c', 's',
187   'y', 'w', };
188
189 static const char ziranma_mapfinals[FINAL_NUM] =
190 { '\0', 'a', 'o', 'e', 'l', 'z', 'k',
191   'b', 'j', 'f', 'h', 'g',
192   '\0', 'i', 'w', 'x', 'c', 'q', 'm', 'n', 'd', 'y', 'u',
193   'w', 'o', 'y', 'v', 'r', 'p',
194   'd', 's', 'v', 't', 's', };
195
196 static TZeroInitial ziranma_zeroinitials[] = {
197     { "a", "aa" },
198     { "ai", "ai" },
199     { "an", "an" },
200     { "ang", "ah" },
201     { "ao", "ao" },
202     { "e", "ee" },
203     { "ei", "ei" },
204     { "en", "en" },
205     { "eng", "eg" },
206     { "er", "er" },
207     { "o", "oo" },
208     { "ou", "ou" },
209 };
210
211 //Ziranma shuangpin plan map table
212 static const char xiaohe_mapinitials[INITIAL_NUM] =
213 { 'a', 'e', 'o', 'b', 'p', 'm', 'f', 'd',
214   't', 'n', 'l', 'g', 'k',
215   'h', 'j', 'q', 'x', 'v', 'i', 'u', 'r', 'z', 'c', 's',
216   'y', 'w', };
217
218 static const char xiaohe_mapfinals[FINAL_NUM] =
219 { '\0', 'a', 'o', 'e', 'd', 'w', 'c',
220   'z', 'j', 'f', 'h', 'g', '\0',
221   'i', 'x', 'p', 'n', 'q', 'm', 'b', 'l', 'k', 'u',
222   'x', 'o', 'k', 'v', 'r', 'y',
223   'l', 's', 'v', 't', 's', };
224
225 static TZeroInitial xiaohe_zeroinitials[] = {
226     { "a", "aa" },
227     { "ai", "ai" },
228     { "an", "an" },
229     { "ang", "ah" },
230     { "ao", "ao" },
231     { "e", "ee" },
232     { "ei", "ei" },
233     { "en", "en" },
234     { "eng", "eg" },
235     { "er", "er" },
236     { "o", "oo" },
237     { "ou", "ou" },
238 };
239
240 CShuangpinData::CShuangpinData (EShuangpinType shpPlan) : m_shuangpinPlan(NULL)
241 {
242     _genCodingMap();
243     _genKeyboardMap(shpPlan);
244 }
245
246 CShuangpinData::~CShuangpinData()
247 {
248     delete m_shuangpinPlan;
249 }
250
251 TSyllable
252 CShuangpinData::encodeSyllable(const char *pinyin)
253 {
254     std::map<std::string, TSyllable>::iterator iter;
255     iter = m_codingmap.find(pinyin);
256     if (iter != m_codingmap.end()) {
257         return iter->second;
258     }
259     return (TSyllable)0;
260 }
261
262 const char *
263 CShuangpinData::decodeSyllable(TSyllable s, const char **i, const char **f)
264 {
265     return NULL;
266 }
267
268 int
269 CShuangpinData::getMapString(const char *shpstr, CMappedYin &syls)
270 {
271     assert(shpstr != NULL);
272     assert(m_shuangpinPlan->mapinitials != NULL);
273
274     const char *ch = shpstr;
275     int len = strlen(shpstr);
276     char buf[32] = "\0";
277
278     syls.clear();
279     switch (len) {
280     case 1:
281         for (int i = 0; i < INITIAL_NUM; i++) {
282             if (*ch == m_shuangpinPlan->mapinitials[i]) {
283                 syls.push_back(std::string(initials[i]));
284                 return 1;
285             }
286         }
287         break;
288     case 2:
289         if (m_shuangpinPlan->zeroinitals != NULL) {
290             for (int i = 0; i < ZEROINITIAL_NUM; i++) {
291                 if (!strcmp(shpstr, m_shuangpinPlan->zeroinitals[i].mapshp)) {
292                     syls.push_back(std::string(m_shuangpinPlan->zeroinitals[i].
293                                                syl));
294                     return 1;
295                 }
296             }
297         }
298
299         for (int i = 0; i < INITIAL_NUM; i++) {
300             if (*ch == m_shuangpinPlan->mapinitials[i]) {
301                 for (int j = 0; j < FINAL_NUM; j++) {
302                     if (*(ch + 1) == m_shuangpinPlan->mapfinals[j]) {
303                         sprintf(buf, "%s%s", initials[i], finals[j]);
304                         std::map<std::string, TSyllable>::iterator iter;
305                         iter = m_codingmap.find(buf);
306                         if (iter != m_codingmap.end()) {
307                             syls.push_back(std::string(buf));
308                         }
309                     }
310                 }
311             }
312         }
313         break;
314     default:
315         //Invalid shuangpin input character number, return 0;
316         break;
317     }
318
319     return syls.size();
320 }
321
322 void
323 CShuangpinData::setShuangpinType(EShuangpinType shpType)
324 {
325     if (shpType == getShuangpinType()) {
326         return;
327     }
328     _genKeyboardMap(shpType);
329 }
330
331
332 void
333 CShuangpinData::_genCodingMap()
334 {
335     unsigned len;
336     const TPyTabEntry *pinyin_table = CPinyinData::getPinyinTable(len);
337
338     for (size_t i = 0; i < len; ++i) {
339         m_codingmap.insert(CEncodingMap::value_type(pinyin_table[i].pystr,
340                                                     pinyin_table[i].id));
341     }
342 }
343
344 void
345 CShuangpinData::_genKeyboardMap(EShuangpinType shyType)
346 {
347     if (m_shuangpinPlan == NULL) {
348         m_shuangpinPlan = new TShuangpinPlan;
349         memset(m_shuangpinPlan, 0, sizeof(TShuangpinPlan));
350     }
351     m_shuangpinPlan->type = shyType;
352     switch (shyType) {
353     case MS2003:
354         m_shuangpinPlan->mapinitials = (char*)ms2003_mapinitials;
355         m_shuangpinPlan->mapfinals = (char*)ms2003_mapfinals;
356         m_shuangpinPlan->zeroinitals = ms2003_zeroinitials;
357         break;
358     case ABC:
359         m_shuangpinPlan->mapinitials = (char*)abc_mapinitials;
360         m_shuangpinPlan->mapfinals = (char*)abc_mapfinals;
361         m_shuangpinPlan->zeroinitals = abc_zeroinitials;
362         break;
363     case ZIGUANG:
364         m_shuangpinPlan->mapinitials = (char*)ziguang_mapinitials;
365         m_shuangpinPlan->mapfinals = (char*)ziguang_mapfinals;
366         m_shuangpinPlan->zeroinitals = ziguang_zeroinitials;
367         break;
368     case PINYINJIAJIA:
369         m_shuangpinPlan->mapinitials = (char*)pyjiajia_mapinitials;
370         m_shuangpinPlan->mapfinals = (char*)pyjiajia_mapfinals;
371         m_shuangpinPlan->zeroinitals = pyjiajia_zeroinitials;
372         break;
373     case ZIRANMA:
374         m_shuangpinPlan->mapinitials = (char*)ziranma_mapinitials;
375         m_shuangpinPlan->mapfinals = (char*)ziranma_mapfinals;
376         m_shuangpinPlan->zeroinitals = ziranma_zeroinitials;
377         break;
378     case XIAOHE:
379         m_shuangpinPlan->mapinitials = (char*)xiaohe_mapinitials;
380         m_shuangpinPlan->mapfinals = (char*)xiaohe_mapfinals;
381         m_shuangpinPlan->zeroinitals = xiaohe_zeroinitials;
382         break;
383     default:
384         break;
385     }
386 }