3 * Library to deal with pinyin.
5 * Copyright (C) 2011 Peng Wu <alexepico@gmail.com>
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.
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.
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.
27 #include "pinyin_custom2.h"
28 #include "chewing_key.h"
29 #include "pinyin_parser2.h"
30 #include "pinyin_parser_table.h"
33 using namespace pinyin;
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);
39 /* handle incomplete pinyin. */
40 if (flags & PINYIN_INCOMPLETE) {
41 if (!(options & PINYIN_INCOMPLETE))
45 /* handle correct pinyin, currently only one flag per item. */
46 flags &= PINYIN_CORRECT_ALL;
47 options &= PINYIN_CORRECT_ALL;
50 if ((flags & options) != flags)
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);
61 /* handle incomplete chewing. */
62 if (flags & CHEWING_INCOMPLETE) {
63 if (!(options & CHEWING_INCOMPLETE))
71 /* methods for Chewing Keys to access pinyin parser table. */
72 const char * ChewingKeyRest::get_pinyin_string(){
73 if (m_table_index == 0)
76 /* check end boundary. */
77 assert(m_table_index < G_N_ELEMENTS(content_table));
78 return content_table[m_table_index].m_pinyin_str;
81 const char * ChewingKeyRest::get_chewing_string(){
82 if (m_table_index == 0)
85 /* check end boundary. */
86 assert(m_table_index < G_N_ELEMENTS(content_table));
87 return content_table[m_table_index].m_chewing_str;
93 /* internal information for pinyin parsers. */
96 ChewingKeyRest m_key_rest;
110 /* Full Pinyin Parser */
111 FullPinyinParser2::FullPinyinParser2 (){
112 m_parse_steps = g_array_new(TRUE, FALSE, sizeof(parse_value_t));
115 const guint16 max_full_pinyin_length = 7; /* include tone. */
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);
122 int 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);
129 guint16 tone = CHEWING_ZERO_TONE; guint16 tone_pos = 0;
130 guint16 parsed_len = len;
131 key = ChewingKey(); key_rest = ChewingKeyRest();
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' ) {
139 tone_pos = parsed_len;
143 /* parse pinyin core staff here. */
144 pinyin_index_item_t item;
145 memset(&item, 0, sizeof(item));
147 for (; parsed_len > 0; --parsed_len) {
148 input[parsed_len] = '\0';
149 item.m_pinyin_input = input;
150 std_lite::pair<const pinyin_index_item_t *,
151 const pinyin_index_item_t *> range;
152 range = std_lite::equal_range
153 (pinyin_index, pinyin_index + G_N_ELEMENTS(pinyin_index),
154 item, compare_less_than);
156 guint16 len = range.second - range.first;
159 const pinyin_index_item_t * index = range.first;
161 if (!check_pinyin_options(options, index))
164 key_rest.m_table_index = index->m_table_index;
165 key = content_table[key_rest.m_table_index].m_chewing_key;
170 if (options & USE_TONE) {
171 /* post processing tone. */
172 if ( parsed_len == tone_pos ) {
173 if (tone != CHEWING_ZERO_TONE) {
180 key_rest.m_raw_begin = 0; key_rest.m_raw_end = parsed_len;
186 int FullPinyinParser2::parse (guint32 options, ChewingKeyVector & keys,
187 ChewingKeyRestVector & key_rests,
188 const char *str, int len) const {
191 g_array_set_size(keys, 0);
192 g_array_set_size(key_rests, 0);
194 /* init m_parse_steps. */
195 int step_len = len + 1;
196 g_array_set_size(m_parse_steps, 0);
197 parse_value_t onestep;
198 for (i = 0; i < step_len; ++i) {
199 g_array_append_val(m_parse_steps, onestep);
202 /* split "'" here. */
203 gchar * input = g_strndup(str, len);
204 gchar ** inputs = g_strsplit(input, "'", -1);
206 /* parse each input */
207 for (i = 0; inputs[i]; ++i) {
209 /* dynamic programming here. */
210 size_t str_len = strlen(input);
211 for (size_t m = 0; m < str_len; ++m) {
212 size_t try_len = std_lite::min
213 (m + max_full_pinyin_length, str_len);
214 for (size_t n = m + 1; n < try_len + 1; ++n) {
221 /* post processing for re-split table. */
223 /* final step for back tracing. */