summaryrefslogtreecommitdiffstats
path: root/src/storage/pinyin_custom.h
blob: e80d78e9c75119b7fbe239a50a15288b000dedec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* 
 *  libpinyin
 *  Library to deal with pinyin.
 *  
 *  Copyright (C) 2011 Peng Wu <alexepico@gmail.com>
 *  
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */


#ifndef PINYIN_CUSTOM_H
#define PINYIN_CUSTOM_H


namespace pinyin{


/**
 * @brief enums of pinyin ambiguities.
 *
 * Some pinyin element maybe confused by somebody,
 * We allow these ambiguities.
 */
enum PinyinAmbiguity
{
    PINYIN_AmbAny= 0,
    PINYIN_AmbCiChi,
    PINYIN_AmbChiCi,
    PINYIN_AmbZiZhi,
    PINYIN_AmbZhiZi,
    PINYIN_AmbSiShi,
    PINYIN_AmbShiSi,
    PINYIN_AmbLeNe,
    PINYIN_AmbNeLe,
    PINYIN_AmbFoHe,
    PINYIN_AmbHeFo,
    PINYIN_AmbLeRi,
    PINYIN_AmbRiLe,
    PINYIN_AmbKeGe,
    PINYIN_AmbGeKe,
    PINYIN_AmbAnAng,
    PINYIN_AmbAngAn,
    PINYIN_AmbEnEng,
    PINYIN_AmbEngEn,
    PINYIN_AmbInIng,
    PINYIN_AmbIngIn,
    PINYIN_AmbLast = PINYIN_AmbIngIn
};

/**
 * @brief Structure to hold pinyin custom settings.
 *
 * user can custom the behavor of libpinyin by these settings.
 */
struct PinyinCustomSettings
{
    bool use_incomplete;
        /**< allow incomplete pinyin key which only has inital. */

    bool use_ambiguities [PINYIN_AmbLast + 1];
        /**< allow ambiguous pinyin elements or not. */

    PinyinCustomSettings ()
        :use_incomplete (true)
    {
        for (size_t i=0; i<=PINYIN_AmbLast; ++i)
            use_ambiguities [i] = false;
    }

    void set_use_incomplete (bool use) { use_incomplete = use; }
    void set_use_ambiguities (PinyinAmbiguity amb, bool use)
    {
        if (amb == PINYIN_AmbAny)
            for (size_t i=0; i<=PINYIN_AmbLast; ++i) use_ambiguities [i] = use;
        else {
            use_ambiguities [0] = false;
		    use_ambiguities [static_cast<size_t>(amb)] = use;
            for (size_t i=1; i<=PINYIN_AmbLast; ++i)
                if (use_ambiguities [i]) {
                    use_ambiguities [0] = true;
                    break;
                }
        }
    }

    bool operator == (const PinyinCustomSettings &rhs) const
    {
        if (use_incomplete != rhs.use_incomplete)
            return false;

        for (size_t i=0; i <= PINYIN_AmbLast; ++i)
            if (use_ambiguities [i] != rhs.use_ambiguities [i])
                return false;

        return true;
    }

    bool operator != (const PinyinCustomSettings &rhs) const
    {
        return !(*this == rhs);
    }

    guint32 to_value () const
    {
        guint32 val = 0;

        if (use_incomplete) val |= 1;

        for (size_t i=0; i <= PINYIN_AmbLast; ++i)
            if (use_ambiguities [i])
                val |= (1 << (i+1));

        return val;
    }

    void from_value (guint32 val)
    {
        use_incomplete = ((val & 1) != 0);

        for (size_t i=0; i <= PINYIN_AmbLast; ++i)
            use_ambiguities [i] = ((val & (1 << (i+1))) != 0);
    }
};

};

#endif