summaryrefslogtreecommitdiffstats
path: root/src/storage/pinyin_parser2.h
blob: 713a9dc1203b49a495fbbb5c9ba5e708e9220004 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/* 
 *  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 "novel_types.h"
#include "chewing_key.h"
#include "pinyin_custom2.h"

namespace pinyin{

typedef struct {
    const char * m_pinyin_str;
    const char * m_shengmu_str;
    const char * m_yunmu_str;
    const char * m_zhuyin_str;
    const char * m_luoma_pinyin_str;
    const char * m_secondary_zhuyin_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 {
    const char * m_orig_key;
    guint32      m_orig_freq;
    const char * m_new_keys[2];
    guint32      m_new_freq;
} divided_table_item_t;

typedef struct {
    const char * m_orig_keys[2];
    guint32      m_orig_freq;
    const char * m_new_keys[2];
    guint32      m_new_freq;
} resplit_table_item_t;

typedef struct {
    const char * m_shengmu;
} double_pinyin_scheme_shengmu_item_t;

typedef struct {
    const char * m_yunmus[2];
} double_pinyin_scheme_yunmu_item_t;

typedef struct {
    const char * m_input;
    const char * m_yunmu;
} double_pinyin_scheme_fallback_item_t;

typedef struct {
    const char m_input;
    const char * m_chewing;
} zhuyin_symbol_item_t;

typedef struct {
    const char m_input;
    const char m_tone;
} zhuyin_tone_item_t;

typedef GArray * ParseValueVector;


/**
 * PhoneticParser2:
 *
 * Parse the ascii string into an array of the struct ChewingKeys.
 *
 */
class PhoneticParser2
{
public:
    /**
     * PhoneticParser2::~PhoneticParser2:
     *
     * The destructor of the PhoneticParser2.
     *
     */
    virtual ~PhoneticParser2() {}

public:
    /**
     * PhoneticParser2::parse_one_key:
     * @options: the pinyin options from pinyin_custom2.h.
     * @key: the parsed result of struct ChewingKey.
     * @str: the input of the ascii string.
     * @len: the length of the str.
     * @returns: whether the entire string is parsed as one key.
     *
     * Parse only one struct ChewingKey from a string.
     *
     */
    virtual bool parse_one_key(pinyin_option_t options, ChewingKey & key, const char *str, int len) const = 0;

    /**
     * PhoneticParser2::parse:
     * @options: the pinyin options from pinyin_custom2.h.
     * @keys: the parsed result of struct ChewingKeys.
     * @str: the input of the ascii string.
     * @len: the length of the str.
     * @returns: the number of chars were actually used.
     *
     * Parse the ascii string into an array of struct ChewingKeys.
     *
     */
    virtual int parse(pinyin_option_t options, ChewingKeyVector & keys, ChewingKeyRestVector & key_rests, const char *str, int len) const = 0;

};


/**
 * FullPinyinParser2:
 *
 * Parses the full pinyin string into an array of struct ChewingKeys.
 *
 */
class FullPinyinParser2 : public PhoneticParser2
{
    /* Note: some internal pointers to full pinyin table. */
    const pinyin_index_item_t * m_pinyin_index;
    size_t m_pinyin_index_len;

protected:
    ParseValueVector m_parse_steps;

    int final_step(size_t step_len, ChewingKeyVector & keys,
                   ChewingKeyRestVector & key_rests) const;

    bool post_process2(pinyin_option_t options, ChewingKeyVector & keys,
                       ChewingKeyRestVector & key_rests,
                       const char * str, int len) const;

public:
    const divided_table_item_t * retrieve_divided_item
    (pinyin_option_t options, ChewingKey * key, ChewingKeyRest * rest,
     const char * str, int len) const;

    const resplit_table_item_t * retrieve_resplit_item_by_original_pinyins
    (pinyin_option_t options,
     ChewingKey * cur_key, ChewingKeyRest * cur_rest,
     ChewingKey * next_key, ChewingKeyRest * next_rest,
     const char * str, int len) const;
    const resplit_table_item_t * retrieve_resplit_item_by_resplit_pinyins
    (pinyin_option_t options,
     ChewingKey * cur_key, ChewingKeyRest * cur_rest,
     ChewingKey * next_key, ChewingKeyRest * next_rest,
     const char * str, int len) const;

public:
    FullPinyinParser2();
    virtual ~FullPinyinParser2() {
        g_array_free(m_parse_steps, TRUE);
    }

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

    /* Note:
     *   the parse method will use dynamic programming to drive parse_one_key.
     */
    virtual int parse(pinyin_option_t options, ChewingKeyVector & keys, ChewingKeyRestVector & key_rests, const char *str, int len) const;

public:
    bool set_scheme(FullPinyinScheme scheme);
};


/**
 * DoublePinyinParser2:
 *
 * Parse the double pinyin string into an array of struct ChewingKeys.
 *
 */
/* The valid input chars of double pinyin is a-z and ';'
 */
class DoublePinyinParser2 : public PhoneticParser2
{
    /* Note: three internal pointers to double pinyin scheme table. */
protected:
    const double_pinyin_scheme_shengmu_item_t  * m_shengmu_table;
    const double_pinyin_scheme_yunmu_item_t    * m_yunmu_table;
    const double_pinyin_scheme_fallback_item_t * m_fallback_table;

public:
    DoublePinyinParser2() {
        m_shengmu_table = NULL;
        m_yunmu_table = NULL;
        m_fallback_table = NULL;

        set_scheme(DOUBLE_PINYIN_DEFAULT);
    }

    virtual ~DoublePinyinParser2() {}

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

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

public:
    bool set_scheme(DoublePinyinScheme scheme);
};

/**
 * ChewingParser2:
 *
 * Parse the chewing input string into an array of struct ChewingKeys.
 *
 */
class ChewingParser2 : public PhoneticParser2
{
public:
    virtual ~ChewingParser2() {}

public:
    /**
     * ChewingParser2::in_chewing_scheme:
     * @options: the pinyin options.
     * @key: the user input ascii character.
     * @symbol: the corresponding chewing symbol.
     * @returns: whether the character is in the chewing scheme.
     *
     * Check whether the input character is in the chewing keyboard mapping.
     *
     */
    virtual bool in_chewing_scheme(pinyin_option_t options, const char key, gchar ** & symbols) const = 0;
};


 /**
 * ChewingSimpleParser2:
 *
 * Parse the chewing string into an array of struct ChewingKeys.
 *
 * Several keyboard scheme are supported:
 * * ZHUYIN_STANDARD  Standard ZhuYin keyboard, which maps 1 to Bo(ㄅ), q to Po(ㄆ) etc.
 * * ZHUYIN_IBM       IBM ZhuYin keyboard, which maps 1 to Bo(ㄅ), 2 to Po(ㄆ) etc.
 * * ZHUYIN_GINYIEH   Gin-Yieh ZhuYin keyboard.
 * * ZHUYIN_ETEN      Eten (倚天) ZhuYin keyboard.
 * * ZHUYIN_STANDARD_DVORAK      Standard Dvorak ZhuYin keyboard
 *
 */

class ChewingSimpleParser2 : public ChewingParser2
{
    /* internal options for chewing parsing. */
    pinyin_option_t m_options;

    /* Note: some internal pointers to chewing scheme table. */
protected:
    const zhuyin_symbol_item_t * m_symbol_table;
    const zhuyin_tone_item_t   * m_tone_table;

public:
    ChewingSimpleParser2() {
        m_symbol_table = NULL; m_tone_table = NULL;
        set_scheme(ZHUYIN_DEFAULT);
    }

    virtual ~ChewingSimpleParser2() {}

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

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

public:
    bool set_scheme(ZhuyinScheme scheme);
    virtual bool in_chewing_scheme(pinyin_option_t options, const char key, gchar ** & symbols) const;
};


/**
 * ChewingDiscreteParser2:
 *
 * Parse the chewing string into an array of struct ChewingKeys.
 *
 * Initially will support HSU, HSU Dvorak and ETEN26.
 *
 */

class ChewingDiscreteParser2 : public ChewingParser2
{
protected:
    /* internal options for chewing parsing. */
    pinyin_option_t m_options;

    /* some internal pointers to chewing scheme table. */
    const chewing_index_item_t * m_chewing_index;
    size_t m_chewing_index_len;
    const zhuyin_symbol_item_t * m_initial_table;
    const zhuyin_symbol_item_t * m_middle_table;
    const zhuyin_symbol_item_t * m_final_table;
    const zhuyin_tone_item_t   * m_tone_table;

public:
    ChewingDiscreteParser2() {
        m_options = 0;
        m_chewing_index = NULL; m_chewing_index_len = 0;
        m_initial_table = NULL; m_middle_table = NULL;
        m_final_table   = NULL; m_tone_table = NULL;
        set_scheme(ZHUYIN_HSU);
    }

    virtual ~ChewingDiscreteParser2() {}

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

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

public:
    bool set_scheme(ZhuyinScheme scheme);
    virtual bool in_chewing_scheme(pinyin_option_t options, const char key, gchar ** & symbols) const;
};


class ChewingDaChenCP26Parser2 : public ChewingParser2
{
    /* some internal pointers to chewing scheme table. */
    const chewing_index_item_t * m_chewing_index;
    size_t m_chewing_index_len;
    const zhuyin_symbol_item_t * m_initial_table;
    const zhuyin_symbol_item_t * m_middle_table;
    const zhuyin_symbol_item_t * m_final_table;
    const zhuyin_tone_item_t   * m_tone_table;

public:
    ChewingDaChenCP26Parser2();

    virtual ~ChewingDaChenCP26Parser2() {}

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

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

public:
    virtual bool in_chewing_scheme(pinyin_option_t options, const char key, gchar ** & symbols) const;
};


/* Direct Parser for Chewing table load. */
class ChewingDirectParser2 : public PhoneticParser2
{
    const chewing_index_item_t * m_chewing_index;
    size_t m_chewing_index_len;

public:
    ChewingDirectParser2();

    virtual ~ChewingDirectParser2() {}

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

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


/* compare pinyins with chewing internal representations. */
inline int pinyin_compare_initial2(pinyin_option_t options,
                                   ChewingInitial lhs,
                                   ChewingInitial rhs) {
    if (lhs == rhs)
        return 0;

    if ((options & PINYIN_AMB_C_CH) &&
        ((lhs == CHEWING_C && rhs == CHEWING_CH) ||
         (lhs == CHEWING_CH && rhs == CHEWING_C)))
        return 0;

    if ((options & PINYIN_AMB_S_SH) &&
        ((lhs == CHEWING_S && rhs == CHEWING_SH) ||
         (lhs == CHEWING_SH && rhs == CHEWING_S)))
        return 0;

    if ((options & PINYIN_AMB_Z_ZH) &&
        ((lhs == CHEWING_Z && rhs == CHEWING_ZH) ||
         (lhs == CHEWING_ZH && rhs == CHEWING_Z)))
        return 0;

    if ((options & PINYIN_AMB_F_H) &&
        ((lhs == CHEWING_F && rhs == CHEWING_H) ||
         (lhs == CHEWING_H && rhs == CHEWING_F)))
        return 0;

    if ((options & PINYIN_AMB_L_N) &&
        ((lhs == CHEWING_L && rhs == CHEWING_N) ||
         (lhs == CHEWING_N && rhs == CHEWING_L)))
        return 0;

    if ((options & PINYIN_AMB_L_R) &&
        ((lhs == CHEWING_L && rhs == CHEWING_R) ||
         (lhs == CHEWING_R && rhs == CHEWING_L)))
        return 0;

    if ((options & PINYIN_AMB_G_K) &&
        ((lhs == CHEWING_G && rhs == CHEWING_K) ||
         (lhs == CHEWING_K && rhs == CHEWING_G)))
        return 0;

    return (lhs - rhs);
}


inline int pinyin_compare_middle_and_final2(pinyin_option_t options,
                                            ChewingMiddle middle_lhs,
                                            ChewingMiddle middle_rhs,
                                            ChewingFinal final_lhs,
                                            ChewingFinal final_rhs) {
    if (middle_lhs == middle_rhs && final_lhs == final_rhs)
        return 0;

    /* both pinyin and chewing incomplete options will enable this. */
    if (options & (PINYIN_INCOMPLETE | ZHUYIN_INCOMPLETE)) {
        if (middle_lhs == CHEWING_ZERO_MIDDLE &&
            final_lhs == CHEWING_ZERO_FINAL)
            return 0;
        if (middle_rhs == CHEWING_ZERO_MIDDLE &&
            final_rhs == CHEWING_ZERO_FINAL)
            return 0;
    }

    /* compare chewing middle first. */
    int middle_diff = middle_lhs - middle_rhs;
    if (middle_diff)
        return middle_diff;

    if ((options & PINYIN_AMB_AN_ANG) &&
        ((final_lhs == CHEWING_AN && final_rhs == CHEWING_ANG) ||
         (final_lhs == CHEWING_ANG && final_rhs == CHEWING_AN)))
        return 0;

    if ((options & PINYIN_AMB_EN_ENG) &&
        ((final_lhs == CHEWING_EN && final_rhs == CHEWING_ENG) ||
         (final_lhs == CHEWING_ENG && final_rhs == CHEWING_EN)))
        return 0;

    if ((options & PINYIN_AMB_IN_ING) &&
        ((final_lhs == PINYIN_IN && final_rhs == PINYIN_ING) ||
         (final_lhs == PINYIN_ING && final_rhs == PINYIN_IN)))
        return 0;

    return (final_lhs - final_rhs);
}


inline int pinyin_compare_tone2(pinyin_option_t options,
                                ChewingTone lhs,
                                ChewingTone rhs) {
    if (lhs == rhs)
        return 0;
    if (lhs == CHEWING_ZERO_TONE)
        return 0;
    if (rhs == CHEWING_ZERO_TONE)
        return 0;
    return (lhs - rhs);
}


};

#endif