Tizen 2.1 base
[platform/core/uifw/ise-engine-sunpinyin.git] / src / pinyin / syllable.h
1 // -*- mode: c++ -*-
2 /*
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4  *
5  * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
6  *
7  * The contents of this file are subject to the terms of either the GNU Lesser
8  * General Public License Version 2.1 only ("LGPL") or the Common Development and
9  * Distribution License ("CDDL")(collectively, the "License"). You may not use this
10  * file except in compliance with the License. You can obtain a copy of the CDDL at
11  * http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
12  * http://www.opensource.org/licenses/lgpl-license.php. See the License for the
13  * specific language governing permissions and limitations under the License. When
14  * distributing the software, include this License Header Notice in each file and
15  * include the full text of the License in the License file as well as the
16  * following notice:
17  *
18  * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
19  * (CDDL)
20  * For Covered Software in this distribution, this License shall be governed by the
21  * laws of the State of California (excluding conflict-of-law provisions).
22  * Any litigation relating to this License shall be subject to the jurisdiction of
23  * the Federal Courts of the Northern District of California and the state courts
24  * of the State of California, with venue lying in Santa Clara County, California.
25  *
26  * Contributor(s):
27  *
28  * If you wish your version of this file to be governed by only the CDDL or only
29  * the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
30  * include this software in this distribution under the [CDDL or LGPL Version 2.1]
31  * license." If you don't indicate a single choice of license, a recipient has the
32  * option to distribute your version of this file under either the CDDL or the LGPL
33  * Version 2.1, or to extend the choice of license to its licensees as provided
34  * above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
35  * Version 2 license, then the option applies only if the new code is made subject
36  * to such option by the copyright holder.
37  */
38
39 #ifndef __SUNPINYIN_SYLLABLE_H__
40 #define __SUNPINYIN_SYLLABLE_H__
41
42 #include <vector>
43 #include <string>
44 #include <map>
45 #include <cstdio>
46
47 #include "ime-core/utils.h"
48
49 #ifdef HAVE_CONFIG_H
50 #include <config.h>
51 #endif
52
53 struct TSyllable {
54 #ifdef WORDS_BIGENDIAN
55     unsigned other    : 12;
56     unsigned initial  : 8;
57     unsigned final    : 8;
58     unsigned tone     : 4;
59 #else
60     unsigned tone     : 4;
61     unsigned final    : 8;
62     unsigned initial  : 8;
63     unsigned other    : 12;
64 #endif
65     TSyllable (unsigned int s = 0)
66     { *((unsigned *) this) = s; }
67
68     TSyllable (int i, int f, int t)
69     : tone(t), final(f), initial(i), other(0) { }
70
71     operator unsigned int() const
72     { return *((unsigned *) this); }
73
74     bool isFullSyllable() const
75     { return final != 0; }
76
77     bool operator ==(const TSyllable & syl) const {
78         return (unsigned int ) *this == (unsigned int) (syl);
79     }
80
81     bool operator !=(const TSyllable & syl) const {
82         return !(*this == syl);
83     }
84
85     bool operator ==(const unsigned s) const {
86         return (unsigned int) *this == s;
87     }
88 };
89
90 typedef struct _TPyTabEntry {
91     const char *pystr;
92     unsigned id;
93 } TPyTabEntry;
94
95 typedef std::vector<TSyllable> CSyllables;
96
97 template <class PinyinDataPolicy>
98 class CGetFuzzySyllablesOp : private CNonCopyable
99 {
100 public:
101     typedef std::multimap<const std::string, std::string> CFuzzyMap;
102
103     CGetFuzzySyllablesOp () : m_bEnableFuzzies(false) {}
104
105     void setEnableFuzzies(bool value = true) { m_bEnableFuzzies = value; }
106     void setEnableSimplerInitials(bool value =
107                                       true) { m_bEnableSimplerInitials = value; }
108     bool isEnabled() { return m_bEnableFuzzies || m_bEnableSimplerInitials; }
109
110     void clearFuzzyMap()
111     { m_fuzzyMap.clear(); }
112
113     void initFuzzyMap(const string_pairs& fuzzyPairs, bool duplex = true){
114         string_pairs::const_iterator it = fuzzyPairs.begin();
115         string_pairs::const_iterator ite = fuzzyPairs.end();
116
117         for (; it != ite; ++it) {
118             const std::string i = it->first;
119             const std::string j = it->second;
120
121             if (m_fuzzyMap.find(i) == m_fuzzyMap.end())
122                 m_fuzzyMap.insert(std::pair<const std::string, std::string> (i,
123                                                                              j));
124
125             if (duplex && m_fuzzyMap.find(j) == m_fuzzyMap.end())
126                 m_fuzzyMap.insert(std::pair<const std::string, std::string> (j,
127                                                                              i));
128         }
129     }
130
131     CSyllables operator ()(TSyllable s){
132         CSyllables ret;
133         static char buf[128];
134
135         const char *i, *f;
136         PinyinDataPolicy::decodeSyllable(s, &i, &f);
137
138         if (m_bEnableSimplerInitials && !m_bEnableFuzzies && *f != '\0')
139             return ret;
140
141         std::vector<const char *> iset;
142         std::vector<const char *> fset;
143
144         iset.push_back(i);
145         fset.push_back(f);
146
147         CFuzzyMap::const_iterator it;
148         for (it = m_fuzzyMap.lower_bound(i);
149              it != m_fuzzyMap.upper_bound(i);
150              ++it)
151             iset.push_back((it->second).c_str());
152
153         for (it = m_fuzzyMap.lower_bound(f);
154              it != m_fuzzyMap.upper_bound(f);
155              ++it)
156             fset.push_back((it->second).c_str());
157
158         std::vector<const char *>::const_iterator iset_it = iset.begin();
159         for (; iset_it != iset.end(); ++iset_it) {
160             std::vector<const char *>::const_iterator fset_it = fset.begin();
161             for (; fset_it != fset.end(); ++fset_it) {
162                 snprintf(buf, sizeof(buf), "%s%s", *iset_it, *fset_it);
163                 TSyllable ts = PinyinDataPolicy::encodeSyllable(buf);
164                 if (ts && ts != s)
165                     ret.push_back(ts);
166             }
167         }
168
169         return ret;
170     }
171
172 private:
173     CFuzzyMap m_fuzzyMap;
174     bool m_bEnableFuzzies;
175     bool m_bEnableSimplerInitials;
176 };
177
178 #endif