summaryrefslogtreecommitdiffstats
path: root/src/storage/pinyin_parser2.h
blob: 4292387c79e9e296451f0beec463b3e82f7a4983 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* 
 *  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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#ifndef PINYIN_PARSER2_H
#define PINYIN_PARSER2_H

#include <glib.h>
#include "chewing_key.h"

namespace pinyin{

typedef struct {
    const char * m_pinyin_str;
    const char * m_chewing_str;
    ChewingKey   m_chewing_key;
} content_table_item_t;

typedef struct {
    const char * m_pinyin_input;
    guint32      m_flags;
    guint16      m_table_index;
} pinyin_index_item_t;

typedef struct {
    const char * m_chewing_input;
    guint32      m_flags;
    guint16      m_table_index;
} chewing_index_item_t;

typedef struct {
    ChewingKey   m_orig_key;
    guint32      m_orig_freq;
    ChewingKey   m_first_key;
    ChewingKey   m_second_key;
    guint32      m_new_freq;
} divided_table_item_t;

typedef struct {
    ChewingKey   m_orig_first_key;
    ChewingKey   m_orig_second_key;
    guint32      m_orig_freq;
    ChewingKey   m_new_first_key;
    ChewingKey   m_new_second_key;
    guint32      m_new_freq;
} resplit_table_item_t;


typedef GArray * ChewingKeyVector;
typedef GArray * ChewingKeyRestVector;
typedef GArray * ParseValueVector;


/**
 * @brief Class to translate string into ChewingKey.
 */
class PinyinParser2
{
    /* constructor/destructor */
public:
    virtual ~PinyinParser2 () {}

    /* public method */
public:
    /**
     * @brief Translate only one ChewingKey from a string.
     *
     * @param options pinyin options from pinyin_custom2.h.
     * @param key stores result ChewingKey.
     * @param str snput string in UTF-8 encoding, in most case this string is just a plain ASCII string.
     * @param len the length of str, in number of chars rather than bytes.
     *
     * @return whether the entire string is parsed as one key.
     */
    virtual bool parse_one_key (guint32 options, ChewingKey & key, ChewingKeyRest & key_rest, const char *str, int len) const = 0;

    /**
     * @brief Translate the source string into a set of ChewingKeys.
     *
     * @param options pinyin options from pinyin_custom2.h.
     * @param keys stores result ChewingKeys.
     * @param str input string in UTF-8 encoding, in most case this string is just a plain ASCII string.
     * @param len the length of str, in number of chars rather than bytes.
     *
     * @return the number of chars were actually used.
     */
    /* Note:
     *   the parse method will use dynamic programming to drive parse_one_key.
     */
    virtual int parse (guint32 options, ChewingKeyVector & keys, ChewingKeyRestVector & key_rests, const char *str, int len) const = 0;

};


/**
 * The Full Pinyin Parser which parses full pinyin string into ChewingKeys.
 */
class FullPinyinParser2 : public PinyinParser2
{
    /* Note: some internal pointers to full pinyin table. */

protected:
    ParseValueVector m_parse_steps;

    int final_step(size_t step_len, ChewingKeyVector & keys,
                   ChewingKeyRestVector & key_rests) const;
public:
    FullPinyinParser2 ();
    virtual ~FullPinyinParser2 () {
        g_array_free(m_parse_steps, TRUE);
    }

    virtual bool parse_one_key (guint32 options, ChewingKey & key, ChewingKeyRest & key_rest, const char *str, int len) const;

    virtual int parse (guint32 options, ChewingKeyVector & keys, ChewingKeyRestVector & key_rests, const char *str, int len) const;
};


/* The valid input chars of ShuangPin is a-z and ';'
 */
class DoublePinyinParser2 : public PinyinParser2
{
    /* Note: two internal pointers to double pinyin scheme table. */

public:
    virtual ~DoublePinyinParser2 () {}

    virtual bool parse_one_key (guint32 options, ChewingKey & key, ChewingKeyRest & key_rest, const char *str, int len) const;

    virtual int parse (guint32 options, ChewingKeyVector & keys, ChewingKeyRestVector & key_rests, const char *str, int len) const;

public:
    bool set_scheme (DoublePinyinScheme scheme);
};


/**
 * @brief Class to parse Chewing input string
 *
 * Several keyboard scheme are supported:
 * * ZHUYIN_ZHUYIN    Parse original ZhuYin string, such as ㄅㄧㄢ
 * * Chewing_STANDARD  Standard ZhuYin keyboard, which maps 1 to Bo(ㄅ), q to Po(ㄆ) etc.
 * * Chewing_HSU       Hsu ZhuYin keyboard, which uses a-z (except q) chars.
 * * Chewing_IBM       IBM ZhuYin keyboard, which maps 1 to Bo(ㄅ), 2 to Po(ㄆ) etc.
 * * Chewing_GIN_YIEH  Gin-Yieh ZhuYin keyboard.
 * * Chewing_ET        Eten (倚天) ZhuYin keyboard.
 * * Chewing_ET26      Eten (倚天) ZhuYin keyboard, which only uses a-z chars.
 * UTF-8 string is used in ZhuYin Parser, because the requirement of supporting original ZhuYin strings.
 * So that the length of inputted string is calculated in number of utf8 chars instead of bytes.
 */
class ChewingParser2 : public PinyinParser2
{
    /* Note: one internal pointer to chewing scheme table. */

public:
    virtual ~ChewingParser2 () {}

    virtual bool parse_one_key (guint32 options, ChewingKey & key, ChewingKeyRest & key_rest, const char *str, int len) const;

    virtual int parse (guint32 options, ChewingKeyVector & keys, ChewingKeyRestVector & key_rests, const char *str, int len) const;

public:
    bool set_scheme (ChewingScheme scheme);
};


};

#endif