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