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.
22 #ifndef PINYIN_PHRASE2_H
23 #define PINYIN_PHRASE2_H
25 #include "novel_types.h"
26 #include "chewing_key.h"
27 #include "pinyin_custom2.h"
28 #include "pinyin_parser2.h"
32 inline int pinyin_exact_compare2(const ChewingKey * key_lhs,
33 const ChewingKey * key_rhs,
39 for (i = 0; i < phrase_length; ++i) {
40 result = key_lhs[i].m_initial - key_rhs[i].m_initial;
45 /* compare middle and final */
46 for (i = 0; i < phrase_length; ++i) {
47 result = key_lhs[i].m_middle - key_rhs[i].m_middle;
50 result = key_lhs[i].m_final - key_rhs[i].m_final;
56 for (i = 0; i < phrase_length; ++i) {
57 result = key_lhs[i].m_tone - key_rhs[i].m_tone;
66 inline int pinyin_compare_with_ambiguities2(pinyin_option_t options,
67 const ChewingKey * key_lhs,
68 const ChewingKey * key_rhs,
74 for (i = 0; i < phrase_length; ++i) {
75 result = pinyin_compare_initial2
77 (ChewingInitial)key_lhs[i].m_initial,
78 (ChewingInitial)key_rhs[i].m_initial);
83 /* compare middle and final */
84 for (i = 0; i < phrase_length; ++i) {
85 result = pinyin_compare_middle_and_final2
87 (ChewingMiddle)key_lhs[i].m_middle,
88 (ChewingMiddle)key_rhs[i].m_middle,
89 (ChewingFinal) key_lhs[i].m_final,
90 (ChewingFinal) key_rhs[i].m_final);
96 for (i = 0; i < phrase_length; ++i) {
97 result = pinyin_compare_tone2
99 (ChewingTone)key_lhs[i].m_tone,
100 (ChewingTone)key_rhs[i].m_tone);
108 /* compute pinyin lower bound */
109 inline void compute_lower_value2(pinyin_option_t options,
110 ChewingKey * in_keys,
111 ChewingKey * out_keys,
115 for (int i = 0; i < phrase_length; ++i) {
119 /* compute lower initial */
120 sel = aKey.m_initial;
121 for (k = aKey.m_initial - 1; k >= CHEWING_ZERO_INITIAL; --k) {
122 if (0 != pinyin_compare_initial2
123 (options, (ChewingInitial)aKey.m_initial, (ChewingInitial)k))
128 aKey.m_initial = (ChewingInitial)sel;
130 /* compute lower middle, skipped as no fuzzy pinyin here.
131 * if needed in future, still use pinyin_compare_middle_and_final2
132 * to check lower bound.
135 /* compute lower final */
137 for (k = aKey.m_final - 1; k >= CHEWING_ZERO_FINAL; --k) {
138 if (0 != pinyin_compare_middle_and_final2
140 (ChewingMiddle)aKey.m_middle, (ChewingMiddle) aKey.m_middle,
141 (ChewingFinal)aKey.m_final, (ChewingFinal)k))
146 aKey.m_final = (ChewingFinal)sel;
148 /* compute lower tone */
150 for (k = aKey.m_tone - 1; k >= CHEWING_ZERO_TONE; --k) {
151 if (0 != pinyin_compare_tone2
152 (options, (ChewingTone)aKey.m_tone, (ChewingTone)k))
157 aKey.m_tone = (ChewingTone)sel;
159 /* save the result */
164 /* compute pinyin upper bound */
165 inline void compute_upper_value2(pinyin_option_t options,
166 ChewingKey * in_keys,
167 ChewingKey * out_keys,
171 for (int i = 0; i < phrase_length; ++i) {
175 /* compute upper initial */
176 sel = aKey.m_initial;
177 for (k = aKey.m_initial + 1; k <= CHEWING_LAST_INITIAL; ++k) {
178 if (0 != pinyin_compare_initial2
179 (options, (ChewingInitial)aKey.m_initial, (ChewingInitial)k))
184 aKey.m_initial = (ChewingInitial)sel;
186 /* compute upper middle, skipped as no fuzzy pinyin here.
187 * if needed in future, still use pinyin_compare_middle_and_final2
188 * to check upper bound.
191 /* compute upper final */
193 for (k = aKey.m_final + 1; k <= CHEWING_LAST_FINAL; ++k) {
194 if (0 != pinyin_compare_middle_and_final2
196 (ChewingMiddle)aKey.m_middle, (ChewingMiddle)aKey.m_middle,
197 (ChewingFinal)aKey.m_final, (ChewingFinal)k))
202 aKey.m_final = (ChewingFinal)sel;
204 /* compute upper tone */
206 for (k = aKey.m_tone + 1; k <= CHEWING_LAST_TONE; ++k) {
207 if (0 != pinyin_compare_tone2
208 (options, (ChewingTone)aKey.m_tone, (ChewingTone)k))
213 aKey.m_tone = (ChewingTone)sel;
215 /* save the result */
221 template<size_t phrase_length>
222 struct PinyinIndexItem2{
223 phrase_token_t m_token;
224 ChewingKey m_keys[phrase_length];
226 PinyinIndexItem2<phrase_length> (ChewingKey * keys, phrase_token_t token) {
227 memmove(m_keys, keys, sizeof(ChewingKey) * phrase_length);
233 /* for find the element in the phrase array */
234 template<size_t phrase_length>
235 inline int phrase_exact_compare2(const PinyinIndexItem2<phrase_length> &lhs,
236 const PinyinIndexItem2<phrase_length> &rhs)
238 ChewingKey * keys_lhs = (ChewingKey *) lhs.m_keys;
239 ChewingKey * keys_rhs = (ChewingKey *) rhs.m_keys;
240 return pinyin_exact_compare2(keys_lhs, keys_rhs, phrase_length);
243 template<size_t phrase_length>
244 inline bool phrase_exact_less_than2(const PinyinIndexItem2<phrase_length> &lhs,
245 const PinyinIndexItem2<phrase_length> &rhs)
247 return 0 > phrase_exact_compare2<phrase_length>(lhs, rhs);