~ubuntu-branches/ubuntu/quantal/sunpinyin/quantal

« back to all changes in this revision

Viewing changes to src/pinyin/syllable.h

  • Committer: Package Import Robot
  • Author(s): YunQiang Su
  • Date: 2012-03-30 15:31:55 UTC
  • mfrom: (1.1.3) (1.2.7 sid)
  • Revision ID: package-import@ubuntu.com-20120330153155-qgls77sogzgtg9zp
Tags: 2.0.3+git20120222-1
* Team upload: git snapshot 20120222.
   - fix breaks if LDFLAGS in environment contains
       multiple words (Closese #646001).
   - rm patches merged to upstream:
       append-os-environ-toenv.patch
       fix-ftbfs-on-sh.patch
       remove-10-candidate-words-limitation.patch
   - refresh disable-lm-dict-compile.patch.
* Bump stardard version to 3.9.3: no modify needed.
* add libsunpinyin3-dbg and python-sunpinyin packages.
* debian/compat to 9, multiarch it.
* rewrite debian/rules with dh 7 format.

Show diffs side-by-side

added added

removed removed

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