update m_table_index name
[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 static bool compare_less_than(const pinyin_index_item_t & lhs,
92                               const pinyin_index_item_t & rhs){
93     return 0 > strcmp(lhs.m_pinyin_input, rhs.m_pinyin_input);
94 }
95
96 int FullPinyinParser2::parse_one_key (guint32 options, ChewingKey & key,
97                                       ChewingKeyRest & key_rest,
98                                       const char * pinyin, int len) const {
99     /* "'" are not accepted in parse_one_key. */
100     assert(NULL == strchr(pinyin, '\''));
101     gchar * input = g_strndup(pinyin, len);
102
103     guint16 tone = CHEWING_ZERO_TONE; guint16 tone_pos = 0;
104     guint16 parsed_len = len;
105     key = ChewingKey(); key_rest = ChewingKeyRest();
106
107     /* find the tone in the last character. */
108     char chr = input[parsed_len - 1];
109     if ( '0' < chr && chr <= '5' ) {
110         tone = chr - '0';
111         parsed_len --;
112         tone_pos = parsed_len;
113     }
114
115     /* parse pinyin core staff here. */
116     pinyin_index_item_t item;
117     memset(&item, 0, sizeof(item));
118
119     for (; parsed_len > 0; --parsed_len) {
120         input[parsed_len] = '\0';
121         item.m_pinyin_input = input;
122         std_lite::pair<const pinyin_index_item_t *,
123                        const pinyin_index_item_t *> range;
124         range = std_lite::equal_range
125             (pinyin_index, pinyin_index + G_N_ELEMENTS(pinyin_index),
126              item, compare_less_than);
127
128         guint16 len = range.second - range.first;
129         assert (len <= 1);
130         if ( len == 1 ) {
131             const pinyin_index_item_t * index = range.first;
132
133             if (!check_pinyin_options(options, index))
134                 continue;
135
136             key_rest.m_table_index = index->m_table_index;
137             key = content_table[key_rest.m_table_index].m_chewing_key;
138             break;
139         }
140     }
141
142     /* post processing tone. */
143     if ( parsed_len == tone_pos ) {
144         if (tone != CHEWING_ZERO_TONE) {
145             key.m_tone = tone;
146             parsed_len ++;
147         }
148     }
149
150     key_rest.m_raw_begin = 0; key_rest.m_raw_end = parsed_len;
151     g_free(input);
152     return parsed_len;
153 }
154
155
156 int FullPinyinParser2::parse (guint32 options, ChewingKeyVector & keys,
157                               ChewingKeyRestVector & key_rests,
158                               const char *str, int len) const {
159     assert(FALSE);
160 }