summaryrefslogtreecommitdiffstats
path: root/src/storage/pinyin_parser2.cpp
blob: ad4f05e1a857cc4c657f0a4042b0632589d29c2e (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
/* 
 *  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.
 */


#include <ctype.h>
#include <assert.h>
#include <string.h>
#include "stl_lite.h"
#include "pinyin_parser2.h"
#include "pinyin_phrase2.h"
#include "pinyin_custom2.h"
#include "chewing_key.h"
#include "pinyin_parser_table.h"


using namespace pinyin;

static bool check_pinyin_options(guint32 options, const pinyin_index_item_t * item) {
    guint32 flags = item->m_flags;
    assert (flags & IS_PINYIN);

    /* handle incomplete pinyin. */
    if (flags & PINYIN_INCOMPLETE) {
        if (!(options & PINYIN_INCOMPLETE))
            return false;
    }

    /* handle correct pinyin, currently only one flag per item. */
    flags &= PINYIN_CORRECT_ALL;
    options &= PINYIN_CORRECT_ALL;

    if (flags) {
        if ((flags & options) != flags)
            return false;
    }

    return true;
}

static bool check_chewing_options(guint32 options, const chewing_index_item_t * item) {
    guint32 flags = item->m_flags;
    assert (flags & IS_CHEWING);

    /* handle incomplete chewing. */
    if (flags & CHEWING_INCOMPLETE) {
        if (!(options & CHEWING_INCOMPLETE))
            return false;
    }

    return true;
}


/* methods for Chewing Keys to access pinyin parser table. */
const char * ChewingKeyRest::get_pinyin_string(){
    if (m_table_index == 0)
        return NULL;

    /* check end boundary. */
    assert(m_table_index < G_N_ELEMENTS(content_table));
    return content_table[m_table_index].m_pinyin_str;
}

const char * ChewingKeyRest::get_chewing_string(){
    if (m_table_index == 0)
        return NULL;

    /* check end boundary. */
    assert(m_table_index < G_N_ELEMENTS(content_table));
    return content_table[m_table_index].m_chewing_str;
}


/* Pinyin Parsers */

/* internal information for pinyin parsers. */
struct parse_value_t{
    ChewingKey m_key;
    ChewingKeyRest m_key_rest;
    gint16 m_num_keys;
    gint16 m_parsed_len;
    gint16 m_last_step;

    /* constructor */
public:
    parse_value_t(){
        m_num_keys = 0;
        m_parsed_len = 0;
        m_last_step = -1;
    }
};

/* Full Pinyin Parser */
FullPinyinParser2::FullPinyinParser2 (){
    m_parse_steps = g_array_new(TRUE, FALSE, sizeof(parse_value_t));
}

const guint16 max_full_pinyin_length = 7;  /* include tone. */

static bool compare_less_than(const pinyin_index_item_t & lhs,
                              const pinyin_index_item_t & rhs){
    return 0 > strcmp(lhs.m_pinyin_input, rhs.m_pinyin_input);
}

bool FullPinyinParser2::parse_one_key (guint32 options, ChewingKey & key,
                                       ChewingKeyRest & key_rest,
                                       const char * pinyin, int len) const {
    /* "'" are not accepted in parse_one_key. */
    assert(NULL == strchr(pinyin, '\''));
    gchar * input = g_strndup(pinyin, len);

    guint16 tone = CHEWING_ZERO_TONE; guint16 tone_pos = 0;
    guint16 parsed_len = len;
    key = ChewingKey(); key_rest = ChewingKeyRest();

    if (options & USE_TONE) {
        /* find the tone in the last character. */
        char chr = input[parsed_len - 1];
        if ( '0' < chr && chr <= '5' ) {
            tone = chr - '0';
            parsed_len --;
            tone_pos = parsed_len;
        }
    }

    /* parse pinyin core staff here. */
    pinyin_index_item_t item;
    memset(&item, 0, sizeof(item));

    /* Note: optimize here? */
    for (; parsed_len >= len - 1; --parsed_len) {
        input[parsed_len] = '\0';
        item.m_pinyin_input = input;
        std_lite::pair<const pinyin_index_item_t *,
                       const pinyin_index_item_t *> range;
        range = std_lite::equal_range
            (pinyin_index, pinyin_index + G_N_ELEMENTS(pinyin_index),
             item, compare_less_than);

        guint16 range_len = range.second - range.first;
        assert (range_len <= 1);
        if ( range_len == 1 ) {
            const pinyin_index_item_t * index = range.first;

            if (!check_pinyin_options(options, index))
                continue;

            key_rest.m_table_index = index->m_table_index;
            key = content_table[key_rest.m_table_index].m_chewing_key;
            break;
        }
    }

    if (options & USE_TONE) {
        /* post processing tone. */
        if ( parsed_len == tone_pos ) {
            if (tone != CHEWING_ZERO_TONE) {
                key.m_tone = tone;
                parsed_len ++;
            }
        }
    }

    key_rest.m_raw_begin = 0; key_rest.m_raw_end = parsed_len;
    g_free(input);
    return parsed_len == len;
}


int FullPinyinParser2::parse (guint32 options, ChewingKeyVector & keys,
                              ChewingKeyRestVector & key_rests,
                              const char *str, int len) const {
    size_t i;
    /* clear arrays. */
    g_array_set_size(keys, 0);
    g_array_set_size(key_rests, 0);

    /* init m_parse_steps, and prepare dynamic programming. */
    size_t step_len = len + 1;
    g_array_set_size(m_parse_steps, 0);
    parse_value_t value;
    for (i = 0; i < step_len; ++i) {
        g_array_append_val(m_parse_steps, value);
    }

    size_t str_len = len; size_t next_sep = 0;
    gchar * input = g_strndup(str, len);
    parse_value_t * curstep = NULL, * nextstep = NULL;

    for (i = 0; i < len; ) {
        if (input[i] == '\'') {
            curstep = &g_array_index(m_parse_steps, parse_value_t, i);
            nextstep = &g_array_index(m_parse_steps, parse_value_t, i + 1);

            /* propagate current step into next step. */
            nextstep->m_key = ChewingKey();
            nextstep->m_key_rest = ChewingKeyRest();
            nextstep->m_num_keys = curstep->m_num_keys;
            nextstep->m_parsed_len = curstep->m_parsed_len + 1;
            nextstep->m_last_step = i;
            next_sep = 0;
            continue;
        }

        /* forward to next "'" */
        if ( 0 == next_sep ) {
            size_t k;
            for (k = i;  k < len; ++k) {
                if (input[k] == '\'')
                    break;
            }
            next_sep = k;
            i = next_sep;
        }

        /* dynamic programming here. */
        for (size_t m = i; m < next_sep; ++m) {
            curstep = &g_array_index(m_parse_steps, parse_value_t, m);
            size_t try_len = std_lite::min
                (m + max_full_pinyin_length, next_sep);
            for (size_t n = m + 1; n < try_len + 1; ++n) {
                nextstep = &g_array_index(m_parse_steps, parse_value_t, n);

                /* gen next step */
                const char * onepinyin = input + m;
                gint16 onepinyinlen = n - m;
                value = parse_value_t();

                ChewingKey key; ChewingKeyRest rest;
                bool parsed = parse_one_key
                    (options, key, rest, onepinyin, onepinyinlen);
                rest.m_raw_begin = m; rest.m_raw_end = n;
                if (!parsed)
                    continue;
                value.m_key = key; value.m_key_rest = rest;
                value.m_num_keys = curstep->m_num_keys + 1;
                value.m_parsed_len = curstep->m_parsed_len + onepinyinlen;
                value.m_last_step = m;

                /* save next step */
                if (-1 == nextstep->m_last_step)
                    *nextstep = value;
                if (value.m_parsed_len > nextstep->m_parsed_len)
                    *nextstep = value;
                if (value.m_parsed_len == nextstep->m_parsed_len &&
                    value.m_num_keys < nextstep->m_num_keys)
                    *nextstep = value;
            }
        }
    }

    /* final step for back tracing. */
    gint16 parsed_len = final_step(step_len, keys, key_rests);

    /* post processing for re-split table. */
    if (options & USE_RESPLIT_TABLE) {
        post_process(options, keys, key_rests);
    }

    g_free(input);
    return parsed_len;
}

int FullPinyinParser2::final_step(size_t step_len, ChewingKeyVector & keys,
                                  ChewingKeyRestVector & key_rests) const{
    size_t i;
    gint16 parsed_len = 0;
    parse_value_t * curstep = NULL;

    /* find longest match, which starts from the beginning of input. */
    for (i = step_len - 1; i >= 0; --i) {
        curstep = &g_array_index(m_parse_steps, parse_value_t, i);
        if (i == curstep->m_parsed_len)
            break;
    }
    /* prepare saving. */
    parsed_len = curstep->m_parsed_len;
    gint16 num_keys = curstep->m_num_keys;
    g_array_set_size(keys, num_keys);
    g_array_set_size(key_rests, num_keys);

    /* save the match. */
    while (curstep->m_last_step != -1) {
        gint16 pos = curstep->m_num_keys - 1;

        /* skip "'" */
        if (0 != curstep->m_key_rest.m_table_index) {
            ChewingKey * key = &g_array_index(keys, ChewingKey, pos);
            ChewingKeyRest * rest = &g_array_index
                (key_rests, ChewingKeyRest, pos);
            *key = curstep->m_key; *rest = curstep->m_key_rest;
        }

        /* back ward */
        curstep = &g_array_index(m_parse_steps, parse_value_t,
                                 curstep->m_last_step);
    }
    return parsed_len;
}


bool FullPinyinParser2::post_process(guint32 options,
                                     ChewingKeyVector & keys,
                                     ChewingKeyRestVector & key_rests) const {
    size_t i;
    assert(keys->len == key_rests->len);
    gint16 num_keys = keys->len;

    ChewingKey * cur_key = NULL, * next_key = NULL;
    ChewingKeyRest * cur_rest = NULL, * next_rest = NULL;
    guint16 cur_tone = CHEWING_ZERO_TONE, next_tone = CHEWING_ZERO_TONE;

    for (i = 0; i < num_keys - 1; ++i) {
        cur_rest = &g_array_index(key_rests, ChewingKeyRest, i);
        next_rest = &g_array_index(key_rests, ChewingKeyRest, i + 1);

        /* some "'" here */
        if (cur_rest->m_raw_end != next_rest->m_raw_begin)
            continue;

        cur_key = &g_array_index(keys, ChewingKey, i);
        next_key = &g_array_index(keys, ChewingKey, i + 1);

        if (options & USE_TONE) {
            cur_tone = cur_key->m_tone;
            next_tone = next_key->m_tone;
            cur_key->m_tone = next_key->m_tone = CHEWING_ZERO_TONE;
        }

        /* lookup re-split table */
        size_t k;
        const resplit_table_item_t * item = NULL;
        for (k = 0; k < G_N_ELEMENTS(resplit_table); ++k) {
            item = resplit_table + k;
            /* no ops */
            if (item->m_orig_freq >= item->m_new_freq)
                continue;

            /* use pinyin_exact_compare2 here. */
            if (0 == pinyin_exact_compare2(item->m_orig_keys,
                                           cur_key, 2))
                break;

        }

        /* find the match */
        if (k < G_N_ELEMENTS(resplit_table)) {
            /* do re-split */
            item = resplit_table + k;
            *cur_key = item->m_new_keys[0];
            *next_key = item->m_new_keys[1];
            /* assumes only moved one char in gen_all_resplit script. */
            cur_rest->m_raw_end --;
            next_rest->m_raw_begin --;
        }

        /* save back tones */
        if (options & USE_TONE) {
            cur_key->m_tone = cur_tone;
            next_key->m_tone = next_tone;
        }
    }

    return true;
}


bool DoublePinyinParser2::parse_one_key (guint32 options, ChewingKey & key,
                                         ChewingKeyRest & key_rest,
                                         const char *str, int len) const{
    if (1 == len) {
        if (!(options & PINYIN_INCOMPLETE))
            return false;
        assert(FALSE);
    }

    options &= ~PINYIN_CORRECT_ALL;

    if (2 == len || 3 == len) {
        /* parse shengmu and yunmu here. */
        assert(FALSE);
    }

    if (3 == len) {
        if (!(options & USE_TONE))
            return false;
        assert(FALSE);
    }

    return false;
}