add escape char to gen bopomofo header
[platform/upstream/libpinyin.git] / src / storage / pinyin_parser2.cpp
1 /* 
2  *  libpinyin
3  *  Library to deal with pinyin.
4  *  
5  *  Copyright (C) 2011 Peng Wu <alexepico@gmail.com>
6  *  
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  * 
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  *  GNU General Public License for more details.
16  *  
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  */
21
22
23 #include <ctype.h>
24 #include <assert.h>
25 #include <string.h>
26 #include "stl_lite.h"
27 #include "pinyin_parser2.h"
28 #include "pinyin_phrase2.h"
29 #include "pinyin_custom2.h"
30 #include "chewing_key.h"
31 #include "pinyin_parser_table.h"
32 #include "double_pinyin_table.h"
33 #include "chewing_table.h"
34
35
36 using namespace pinyin;
37
38 static bool check_pinyin_options(guint32 options, const pinyin_index_item_t * item) {
39     guint32 flags = item->m_flags;
40     assert (flags & IS_PINYIN);
41
42     /* handle incomplete pinyin. */
43     if (flags & PINYIN_INCOMPLETE) {
44         if (!(options & PINYIN_INCOMPLETE))
45             return false;
46     }
47
48     /* handle correct pinyin, currently only one flag per item. */
49     flags &= PINYIN_CORRECT_ALL;
50     options &= PINYIN_CORRECT_ALL;
51
52     if (flags) {
53         if ((flags & options) != flags)
54             return false;
55     }
56
57     return true;
58 }
59
60 static bool check_chewing_options(guint32 options, const chewing_index_item_t * item) {
61     guint32 flags = item->m_flags;
62     assert (flags & IS_CHEWING);
63
64     /* handle incomplete chewing. */
65     if (flags & CHEWING_INCOMPLETE) {
66         if (!(options & CHEWING_INCOMPLETE))
67             return false;
68     }
69
70     return true;
71 }
72
73
74 /* methods for Chewing Keys to access pinyin parser table. */
75 const char * ChewingKeyRest::get_pinyin_string(){
76     if (m_table_index == 0)
77         return NULL;
78
79     /* check end boundary. */
80     assert(m_table_index < G_N_ELEMENTS(content_table));
81     return content_table[m_table_index].m_pinyin_str;
82 }
83
84 const char * ChewingKeyRest::get_chewing_string(){
85     if (m_table_index == 0)
86         return NULL;
87
88     /* check end boundary. */
89     assert(m_table_index < G_N_ELEMENTS(content_table));
90     return content_table[m_table_index].m_chewing_str;
91 }
92
93
94 /* Pinyin Parsers */
95
96 /* internal information for pinyin parsers. */
97 struct parse_value_t{
98     ChewingKey m_key;
99     ChewingKeyRest m_key_rest;
100     gint16 m_num_keys;
101     gint16 m_parsed_len;
102     gint16 m_last_step;
103
104     /* constructor */
105 public:
106     parse_value_t(){
107         m_num_keys = 0;
108         m_parsed_len = 0;
109         m_last_step = -1;
110     }
111 };
112
113 /* Full Pinyin Parser */
114 FullPinyinParser2::FullPinyinParser2 (){
115     m_parse_steps = g_array_new(TRUE, FALSE, sizeof(parse_value_t));
116 }
117
118 const guint16 max_full_pinyin_length = 7;  /* include tone. */
119
120 static bool compare_less_than(const pinyin_index_item_t & lhs,
121                               const pinyin_index_item_t & rhs){
122     return 0 > strcmp(lhs.m_pinyin_input, rhs.m_pinyin_input);
123 }
124
125 bool FullPinyinParser2::parse_one_key (guint32 options, ChewingKey & key,
126                                        ChewingKeyRest & key_rest,
127                                        const char * pinyin, int len) const {
128     /* "'" are not accepted in parse_one_key. */
129     assert(NULL == strchr(pinyin, '\''));
130     gchar * input = g_strndup(pinyin, len);
131
132     guint16 tone = CHEWING_ZERO_TONE; guint16 tone_pos = 0;
133     guint16 parsed_len = len;
134     key = ChewingKey(); key_rest = ChewingKeyRest();
135
136     if (options & USE_TONE) {
137         /* find the tone in the last character. */
138         char chr = input[parsed_len - 1];
139         if ( '0' < chr && chr <= '5' ) {
140             tone = chr - '0';
141             parsed_len --;
142             tone_pos = parsed_len;
143         }
144     }
145
146     /* parse pinyin core staff here. */
147     pinyin_index_item_t item;
148     memset(&item, 0, sizeof(item));
149
150     /* Note: optimize here? */
151     for (; parsed_len >= len - 1; --parsed_len) {
152         input[parsed_len] = '\0';
153         item.m_pinyin_input = input;
154         std_lite::pair<const pinyin_index_item_t *,
155                        const pinyin_index_item_t *> range;
156         range = std_lite::equal_range
157             (pinyin_index, pinyin_index + G_N_ELEMENTS(pinyin_index),
158              item, compare_less_than);
159
160         guint16 range_len = range.second - range.first;
161         assert (range_len <= 1);
162         if ( range_len == 1 ) {
163             const pinyin_index_item_t * index = range.first;
164
165             if (!check_pinyin_options(options, index))
166                 continue;
167
168             key_rest.m_table_index = index->m_table_index;
169             key = content_table[key_rest.m_table_index].m_chewing_key;
170             break;
171         }
172     }
173
174     if (options & USE_TONE) {
175         /* post processing tone. */
176         if ( parsed_len == tone_pos ) {
177             if (tone != CHEWING_ZERO_TONE) {
178                 key.m_tone = tone;
179                 parsed_len ++;
180             }
181         }
182     }
183
184     key_rest.m_raw_begin = 0; key_rest.m_raw_end = parsed_len;
185     g_free(input);
186     return parsed_len == len;
187 }
188
189
190 int FullPinyinParser2::parse (guint32 options, ChewingKeyVector & keys,
191                               ChewingKeyRestVector & key_rests,
192                               const char *str, int len) const {
193     size_t i;
194     /* clear arrays. */
195     g_array_set_size(keys, 0);
196     g_array_set_size(key_rests, 0);
197
198     /* init m_parse_steps, and prepare dynamic programming. */
199     size_t step_len = len + 1;
200     g_array_set_size(m_parse_steps, 0);
201     parse_value_t value;
202     for (i = 0; i < step_len; ++i) {
203         g_array_append_val(m_parse_steps, value);
204     }
205
206     size_t str_len = len; size_t next_sep = 0;
207     gchar * input = g_strndup(str, len);
208     parse_value_t * curstep = NULL, * nextstep = NULL;
209
210     for (i = 0; i < len; ) {
211         if (input[i] == '\'') {
212             curstep = &g_array_index(m_parse_steps, parse_value_t, i);
213             nextstep = &g_array_index(m_parse_steps, parse_value_t, i + 1);
214
215             /* propagate current step into next step. */
216             nextstep->m_key = ChewingKey();
217             nextstep->m_key_rest = ChewingKeyRest();
218             nextstep->m_num_keys = curstep->m_num_keys;
219             nextstep->m_parsed_len = curstep->m_parsed_len + 1;
220             nextstep->m_last_step = i;
221             next_sep = 0;
222             continue;
223         }
224
225         /* forward to next "'" */
226         if ( 0 == next_sep ) {
227             size_t k;
228             for (k = i;  k < len; ++k) {
229                 if (input[k] == '\'')
230                     break;
231             }
232             next_sep = k;
233             i = next_sep;
234         }
235
236         /* dynamic programming here. */
237         for (size_t m = i; m < next_sep; ++m) {
238             curstep = &g_array_index(m_parse_steps, parse_value_t, m);
239             size_t try_len = std_lite::min
240                 (m + max_full_pinyin_length, next_sep);
241             for (size_t n = m + 1; n < try_len + 1; ++n) {
242                 nextstep = &g_array_index(m_parse_steps, parse_value_t, n);
243
244                 /* gen next step */
245                 const char * onepinyin = input + m;
246                 gint16 onepinyinlen = n - m;
247                 value = parse_value_t();
248
249                 ChewingKey key; ChewingKeyRest rest;
250                 bool parsed = parse_one_key
251                     (options, key, rest, onepinyin, onepinyinlen);
252                 rest.m_raw_begin = m; rest.m_raw_end = n;
253                 if (!parsed)
254                     continue;
255                 value.m_key = key; value.m_key_rest = rest;
256                 value.m_num_keys = curstep->m_num_keys + 1;
257                 value.m_parsed_len = curstep->m_parsed_len + onepinyinlen;
258                 value.m_last_step = m;
259
260                 /* save next step */
261                 if (-1 == nextstep->m_last_step)
262                     *nextstep = value;
263                 if (value.m_parsed_len > nextstep->m_parsed_len)
264                     *nextstep = value;
265                 if (value.m_parsed_len == nextstep->m_parsed_len &&
266                     value.m_num_keys < nextstep->m_num_keys)
267                     *nextstep = value;
268             }
269         }
270     }
271
272     /* final step for back tracing. */
273     gint16 parsed_len = final_step(step_len, keys, key_rests);
274
275     /* post processing for re-split table. */
276     if (options & USE_RESPLIT_TABLE) {
277         post_process(options, keys, key_rests);
278     }
279
280     g_free(input);
281     return parsed_len;
282 }
283
284 int FullPinyinParser2::final_step(size_t step_len, ChewingKeyVector & keys,
285                                   ChewingKeyRestVector & key_rests) const{
286     size_t i;
287     gint16 parsed_len = 0;
288     parse_value_t * curstep = NULL;
289
290     /* find longest match, which starts from the beginning of input. */
291     for (i = step_len - 1; i >= 0; --i) {
292         curstep = &g_array_index(m_parse_steps, parse_value_t, i);
293         if (i == curstep->m_parsed_len)
294             break;
295     }
296     /* prepare saving. */
297     parsed_len = curstep->m_parsed_len;
298     gint16 num_keys = curstep->m_num_keys;
299     g_array_set_size(keys, num_keys);
300     g_array_set_size(key_rests, num_keys);
301
302     /* save the match. */
303     while (curstep->m_last_step != -1) {
304         gint16 pos = curstep->m_num_keys - 1;
305
306         /* skip "'" */
307         if (0 != curstep->m_key_rest.m_table_index) {
308             ChewingKey * key = &g_array_index(keys, ChewingKey, pos);
309             ChewingKeyRest * rest = &g_array_index
310                 (key_rests, ChewingKeyRest, pos);
311             *key = curstep->m_key; *rest = curstep->m_key_rest;
312         }
313
314         /* back ward */
315         curstep = &g_array_index(m_parse_steps, parse_value_t,
316                                  curstep->m_last_step);
317     }
318     return parsed_len;
319 }
320
321
322 bool FullPinyinParser2::post_process(guint32 options,
323                                      ChewingKeyVector & keys,
324                                      ChewingKeyRestVector & key_rests) const {
325     size_t i;
326     assert(keys->len == key_rests->len);
327     gint16 num_keys = keys->len;
328
329     ChewingKey * cur_key = NULL, * next_key = NULL;
330     ChewingKeyRest * cur_rest = NULL, * next_rest = NULL;
331     guint16 cur_tone = CHEWING_ZERO_TONE, next_tone = CHEWING_ZERO_TONE;
332
333     for (i = 0; i < num_keys - 1; ++i) {
334         cur_rest = &g_array_index(key_rests, ChewingKeyRest, i);
335         next_rest = &g_array_index(key_rests, ChewingKeyRest, i + 1);
336
337         /* some "'" here */
338         if (cur_rest->m_raw_end != next_rest->m_raw_begin)
339             continue;
340
341         cur_key = &g_array_index(keys, ChewingKey, i);
342         next_key = &g_array_index(keys, ChewingKey, i + 1);
343
344         if (options & USE_TONE) {
345             cur_tone = cur_key->m_tone;
346             next_tone = next_key->m_tone;
347             cur_key->m_tone = next_key->m_tone = CHEWING_ZERO_TONE;
348         }
349
350         /* lookup re-split table */
351         size_t k;
352         const resplit_table_item_t * item = NULL;
353         for (k = 0; k < G_N_ELEMENTS(resplit_table); ++k) {
354             item = resplit_table + k;
355             /* no ops */
356             if (item->m_orig_freq >= item->m_new_freq)
357                 continue;
358
359             /* use pinyin_exact_compare2 here. */
360             if (0 == pinyin_exact_compare2(item->m_orig_keys,
361                                            cur_key, 2))
362                 break;
363
364         }
365
366         /* find the match */
367         if (k < G_N_ELEMENTS(resplit_table)) {
368             /* do re-split */
369             item = resplit_table + k;
370             *cur_key = item->m_new_keys[0];
371             *next_key = item->m_new_keys[1];
372             /* assumes only moved one char in gen_all_resplit script. */
373             cur_rest->m_raw_end --;
374             next_rest->m_raw_begin --;
375         }
376
377         /* save back tones */
378         if (options & USE_TONE) {
379             cur_key->m_tone = cur_tone;
380             next_key->m_tone = next_tone;
381         }
382     }
383
384     return true;
385 }
386
387
388 bool DoublePinyinParser2::parse_one_key (guint32 options, ChewingKey & key,
389                                          ChewingKeyRest & key_rest,
390                                          const char *str, int len) const{
391     if (1 == len) {
392         if (!(options & PINYIN_INCOMPLETE))
393             return false;
394         assert(FALSE);
395     }
396
397     options &= ~(PINYIN_CORRECT_ALL|PINYIN_AMB_ALL);
398
399     if (2 == len || 3 == len) {
400         /* parse shengmu and yunmu here. */
401         assert(FALSE);
402     }
403
404     if (3 == len) {
405         if (!(options & USE_TONE))
406             return false;
407         assert(FALSE);
408     }
409
410     return false;
411 }
412
413
414 int DoublePinyinParser2::parse (guint32 options, ChewingKeyVector & keys,
415                                 ChewingKeyRestVector & key_rests,
416                                 const char *str, int len) const{
417     assert(FALSE);
418 }