3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5 * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
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
18 * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
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.
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.
39 #ifndef __SUNPINYIN_SYLLABLE_H__
40 #define __SUNPINYIN_SYLLABLE_H__
47 #include "ime-core/utils.h"
54 #ifdef WORDS_BIGENDIAN
65 TSyllable (unsigned int s = 0)
66 { *((unsigned *) this) = s; }
68 TSyllable (int i, int f, int t)
69 : tone(t), final(f), initial(i), other(0) { }
71 operator unsigned int() const
72 { return *((unsigned *) this); }
74 bool isFullSyllable() const
75 { return final != 0; }
77 bool operator ==(const TSyllable & syl) const {
78 return (unsigned int ) *this == (unsigned int) (syl);
81 bool operator !=(const TSyllable & syl) const {
82 return !(*this == syl);
85 bool operator ==(const unsigned s) const {
86 return (unsigned int) *this == s;
90 typedef struct _TPyTabEntry {
95 typedef std::vector<TSyllable> CSyllables;
97 template <class PinyinDataPolicy>
98 class CGetFuzzySyllablesOp : private CNonCopyable
101 typedef std::multimap<const std::string, std::string> CFuzzyMap;
103 CGetFuzzySyllablesOp () : m_bEnableFuzzies(false) {}
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; }
111 { m_fuzzyMap.clear(); }
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();
117 for (; it != ite; ++it) {
118 const std::string i = it->first;
119 const std::string j = it->second;
121 if (m_fuzzyMap.find(i) == m_fuzzyMap.end())
122 m_fuzzyMap.insert(std::pair<const std::string, std::string> (i,
125 if (duplex && m_fuzzyMap.find(j) == m_fuzzyMap.end())
126 m_fuzzyMap.insert(std::pair<const std::string, std::string> (j,
131 CSyllables operator ()(TSyllable s){
133 static char buf[128];
136 PinyinDataPolicy::decodeSyllable(s, &i, &f);
138 if (m_bEnableSimplerInitials && !m_bEnableFuzzies && *f != '\0')
141 std::vector<const char *> iset;
142 std::vector<const char *> fset;
147 CFuzzyMap::const_iterator it;
148 for (it = m_fuzzyMap.lower_bound(i);
149 it != m_fuzzyMap.upper_bound(i);
151 iset.push_back((it->second).c_str());
153 for (it = m_fuzzyMap.lower_bound(f);
154 it != m_fuzzyMap.upper_bound(f);
156 fset.push_back((it->second).c_str());
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);
173 CFuzzyMap m_fuzzyMap;
174 bool m_bEnableFuzzies;
175 bool m_bEnableSimplerInitials;