add final step method
[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_custom2.h"
28 #include "chewing_key.h"
29 #include "pinyin_parser2.h"
30 #include "pinyin_parser_table.h"
31
32
33 using namespace pinyin;
34
35 static bool check_pinyin_options(guint32 options, const pinyin_index_item_t * item) {
36     guint32 flags = item->m_flags;
37     assert (flags & IS_PINYIN);
38
39     /* handle incomplete pinyin. */
40     if (flags & PINYIN_INCOMPLETE) {
41         if (!(options & PINYIN_INCOMPLETE))
42             return false;
43     }
44
45     /* handle correct pinyin, currently only one flag per item. */
46     flags &= PINYIN_CORRECT_ALL;
47     options &= PINYIN_CORRECT_ALL;
48
49     if (flags) {
50         if ((flags & options) != flags)
51             return false;
52     }
53
54     return true;
55 }
56
57 static bool check_chewing_options(guint32 options, const chewing_index_item_t * item) {
58     guint32 flags = item->m_flags;
59     assert (flags & IS_CHEWING);
60
61     /* handle incomplete chewing. */
62     if (flags & CHEWING_INCOMPLETE) {
63         if (!(options & CHEWING_INCOMPLETE))
64             return false;
65     }
66
67     return true;
68 }
69
70
71 /* methods for Chewing Keys to access pinyin parser table. */
72 const char * ChewingKeyRest::get_pinyin_string(){
73     if (m_table_index == 0)
74         return NULL;
75
76     /* check end boundary. */
77     assert(m_table_index < G_N_ELEMENTS(content_table));
78     return content_table[m_table_index].m_pinyin_str;
79 }
80
81 const char * ChewingKeyRest::get_chewing_string(){
82     if (m_table_index == 0)
83         return NULL;
84
85     /* check end boundary. */
86     assert(m_table_index < G_N_ELEMENTS(content_table));
87     return content_table[m_table_index].m_chewing_str;
88 }
89
90
91 /* Pinyin Parsers */
92
93 /* internal information for pinyin parsers. */
94 struct parse_value_t{
95     ChewingKey m_key;
96     ChewingKeyRest m_key_rest;
97     gint16 m_num_keys;
98     gint16 m_parsed_len;
99     gint16 m_last_step;
100
101     /* constructor */
102 public:
103     parse_value_t(){
104         m_num_keys = 0;
105         m_parsed_len = 0;
106         m_last_step = -1;
107     }
108 };
109
110 /* Full Pinyin Parser */
111 FullPinyinParser2::FullPinyinParser2 (){
112     m_parse_steps = g_array_new(TRUE, FALSE, sizeof(parse_value_t));
113 }
114
115 const guint16 max_full_pinyin_length = 7;  /* include tone. */
116
117 static bool compare_less_than(const pinyin_index_item_t & lhs,
118                               const pinyin_index_item_t & rhs){
119     return 0 > strcmp(lhs.m_pinyin_input, rhs.m_pinyin_input);
120 }
121
122 bool FullPinyinParser2::parse_one_key (guint32 options, ChewingKey & key,
123                                        ChewingKeyRest & key_rest,
124                                        const char * pinyin, int len) const {
125     /* "'" are not accepted in parse_one_key. */
126     assert(NULL == strchr(pinyin, '\''));
127     gchar * input = g_strndup(pinyin, len);
128
129     guint16 tone = CHEWING_ZERO_TONE; guint16 tone_pos = 0;
130     guint16 parsed_len = len;
131     key = ChewingKey(); key_rest = ChewingKeyRest();
132
133     if (options & USE_TONE) {
134         /* find the tone in the last character. */
135         char chr = input[parsed_len - 1];
136         if ( '0' < chr && chr <= '5' ) {
137             tone = chr - '0';
138             parsed_len --;
139             tone_pos = parsed_len;
140         }
141     }
142
143     /* parse pinyin core staff here. */
144     pinyin_index_item_t item;
145     memset(&item, 0, sizeof(item));
146
147     /* Note: optimize here? */
148     for (; parsed_len >= len - 1; --parsed_len) {
149         input[parsed_len] = '\0';
150         item.m_pinyin_input = input;
151         std_lite::pair<const pinyin_index_item_t *,
152                        const pinyin_index_item_t *> range;
153         range = std_lite::equal_range
154             (pinyin_index, pinyin_index + G_N_ELEMENTS(pinyin_index),
155              item, compare_less_than);
156
157         guint16 range_len = range.second - range.first;
158         assert (range_len <= 1);
159         if ( range_len == 1 ) {
160             const pinyin_index_item_t * index = range.first;
161
162             if (!check_pinyin_options(options, index))
163                 continue;
164
165             key_rest.m_table_index = index->m_table_index;
166             key = content_table[key_rest.m_table_index].m_chewing_key;
167             break;
168         }
169     }
170
171     if (options & USE_TONE) {
172         /* post processing tone. */
173         if ( parsed_len == tone_pos ) {
174             if (tone != CHEWING_ZERO_TONE) {
175                 key.m_tone = tone;
176                 parsed_len ++;
177             }
178         }
179     }
180
181     key_rest.m_raw_begin = 0; key_rest.m_raw_end = parsed_len;
182     g_free(input);
183     return parsed_len == len;
184 }
185
186
187 int FullPinyinParser2::parse (guint32 options, ChewingKeyVector & keys,
188                               ChewingKeyRestVector & key_rests,
189                               const char *str, int len) const {
190     size_t i;
191     /* clear arrays. */
192     g_array_set_size(keys, 0);
193     g_array_set_size(key_rests, 0);
194
195     /* init m_parse_steps, and prepare dynamic programming. */
196     size_t step_len = len + 1;
197     g_array_set_size(m_parse_steps, 0);
198     parse_value_t value;
199     for (i = 0; i < step_len; ++i) {
200         g_array_append_val(m_parse_steps, value);
201     }
202
203     size_t str_len = len; size_t next_sep = 0;
204     gchar * input = g_strndup(str, len);
205     parse_value_t * curstep = NULL, * nextstep = NULL;
206
207     for (i = 0; i < len; ) {
208         if (input[i] == '\'') {
209             curstep = &g_array_index(m_parse_steps, parse_value_t, i);
210             nextstep = &g_array_index(m_parse_steps, parse_value_t, i + 1);
211
212             /* propagate current step into next step. */
213             nextstep->m_key = ChewingKey();
214             nextstep->m_key_rest = ChewingKeyRest();
215             nextstep->m_num_keys = curstep->m_num_keys;
216             nextstep->m_parsed_len = curstep->m_parsed_len + 1;
217             nextstep->m_last_step = i;
218             next_sep = 0;
219             continue;
220         }
221
222         /* forward to next "'" */
223         if ( 0 == next_sep ) {
224             size_t k;
225             for (k = i;  k < len; ++k) {
226                 if (input[k] == '\'')
227                     break;
228             }
229             next_sep = k;
230             i = next_sep;
231         }
232
233         /* dynamic programming here. */
234         for (size_t m = i; m < next_sep; ++m) {
235             curstep = &g_array_index(m_parse_steps, parse_value_t, m);
236             size_t try_len = std_lite::min
237                 (m + max_full_pinyin_length, next_sep);
238             for (size_t n = m + 1; n < try_len + 1; ++n) {
239                 nextstep = &g_array_index(m_parse_steps, parse_value_t, n);
240
241                 /* gen next step */
242                 const char * onepinyin = input + m;
243                 gint16 onepinyinlen = n - m;
244                 value = parse_value_t();
245
246                 ChewingKey key; ChewingKeyRest rest;
247                 bool parsed = parse_one_key
248                     (options, key, rest, onepinyin, onepinyinlen);
249                 rest.m_raw_begin = m; rest.m_raw_end = n;
250                 if (!parsed)
251                     continue;
252                 value.m_key = key; value.m_key_rest = rest;
253                 value.m_num_keys = curstep->m_num_keys + 1;
254                 value.m_parsed_len = curstep->m_parsed_len + onepinyinlen;
255                 value.m_last_step = m;
256
257                 /* save next step */
258                 if (-1 == nextstep->m_last_step)
259                     *nextstep = value;
260                 if (value.m_parsed_len > nextstep->m_parsed_len)
261                     *nextstep = value;
262                 if (value.m_parsed_len == nextstep->m_parsed_len &&
263                     value.m_num_keys < nextstep->m_num_keys)
264                     *nextstep = value;
265             }
266         }
267     }
268
269     /* final step for back tracing. */
270     gint16 parsed_len = final_step(step_len, keys, key_rests);
271
272     /* post processing for re-split table. */
273     if (options & USE_RESPLIT_TABLE) {
274         
275     }
276
277     g_free(input);
278     return parsed_len;
279 }
280
281 int FullPinyinParser2::final_step(size_t step_len, ChewingKeyVector & keys,
282                                   ChewingKeyRestVector & key_rests) const{
283     size_t i;
284     gint16 parsed_len = 0;
285     parse_value_t * curstep = NULL;
286
287     /* find longest match, which starts from the beginning of input. */
288     for ( i = step_len - 1; i >= 0; --i) {
289         curstep = &g_array_index(m_parse_steps, parse_value_t, i);
290         if (i == curstep->m_parsed_len)
291             break;
292     }
293     /* prepare saving. */
294     parsed_len = curstep->m_parsed_len;
295     gint16 num_keys = curstep->m_num_keys;
296     g_array_set_size(keys, num_keys);
297     g_array_set_size(key_rests, num_keys);
298
299     /* save the match. */
300     while (curstep->m_last_step != -1) {
301         gint16 pos = curstep->m_num_keys - 1;
302
303         /* skip "'" */
304         if (0 != curstep->m_key_rest.m_table_index) {
305             ChewingKey * key = &g_array_index(keys, ChewingKey, pos);
306             ChewingKeyRest * rest = &g_array_index
307                 (key_rests, ChewingKeyRest, pos);
308             *key = curstep->m_key; *rest = curstep->m_key_rest;
309         }
310
311         /* back ward */
312         curstep = &g_array_index(m_parse_steps, parse_value_t,
313                                  curstep->m_last_step);
314     }
315     return parsed_len;
316 }