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 "novel_types.h"
28 #include "pinyin_custom2.h"
29 #include "chewing_key.h"
30 #include "pinyin_parser2.h"
32 using namespace pinyin;
36 typedef struct _pinyin_context_t pinyin_context_t;
37 typedef struct _pinyin_instance_t pinyin_instance_t;
38 typedef struct _lookup_candidate_t lookup_candidate_t;
40 typedef struct _import_iterator_t import_iterator_t;
42 typedef GArray * CandidateVector; /* GArray of lookup_candidate_t */
44 enum lookup_candidate_type_t{
50 struct _lookup_candidate_t{
51 enum lookup_candidate_type_t m_candidate_type;
52 phrase_token_t m_token;
53 ChewingKeyRest m_orig_rest;
54 gchar * m_new_pinyins;
55 guint32 m_freq; /* the amplifed gfloat numerical value. */
57 _lookup_candidate_t() {
58 m_candidate_type = NORMAL_CANDIDATE;
65 struct _pinyin_instance_t{
66 pinyin_context_t * m_context;
67 gchar * m_raw_full_pinyin;
68 TokenVector m_prefixes;
69 ChewingKeyVector m_pinyin_keys;
70 ChewingKeyRestVector m_pinyin_key_rests;
71 CandidateConstraints m_constraints;
72 MatchResults m_match_results;
77 * @systemdir: the system wide language model data directory.
78 * @userdir: the user's language model data directory.
79 * @returns: the newly created pinyin context, NULL if failed.
81 * Create a new pinyin context.
84 pinyin_context_t * pinyin_init(const char * systemdir, const char * userdir);
87 * pinyin_load_phrase_library:
88 * @context: the pinyin context.
89 * @index: the phrase index to be loaded.
90 * @returns: whether the load succeeded.
92 * Load the sub phrase library of the index.
95 bool pinyin_load_phrase_library(pinyin_context_t * context,
99 * pinyin_unload_phrase_library:
100 * @context: the pinyin context.
101 * @index: the phrase index to be unloaded.
102 * @returns: whether the unload succeeded.
104 * Unload the sub phrase library of the index.
107 bool pinyin_unload_phrase_library(pinyin_context_t * context,
110 import_iterator_t * pinyin_begin_add_phrases(pinyin_context_t * context,
113 bool pinyin_add_phrase(import_iterator_t * iterator,
115 const char * pinyin);
117 void pinyin_end_add_phrases(import_iterator_t * iterator);
121 * @context: the pinyin context to be saved into user directory.
122 * @returns: whether the save succeeded.
124 * Save the user's self-learning information of the pinyin context.
127 bool pinyin_save(pinyin_context_t * context);
130 * pinyin_set_double_pinyin_scheme:
131 * @context: the pinyin context.
132 * @scheme: the double pinyin scheme.
133 * @returns: whether the set double pinyin scheme succeeded.
135 * Change the double pinyin scheme of the pinyin context.
138 bool pinyin_set_double_pinyin_scheme(pinyin_context_t * context,
139 DoublePinyinScheme scheme);
142 * pinyin_set_chewing_scheme:
143 * @context: the pinyin context.
144 * @scheme: the chewing scheme.
145 * @returns: whether the set chewing scheme succeeded.
147 * Change the chewing scheme of the pinyin context.
150 bool pinyin_set_chewing_scheme(pinyin_context_t * context,
151 ChewingScheme scheme);
155 * @context: the pinyin context.
157 * Finalize the pinyin context.
160 void pinyin_fini(pinyin_context_t * context);
164 * pinyin_set_options:
165 * @context: the pinyin context.
166 * @options: the pinyin options of the pinyin context.
167 * @returns: whether the set options scheme succeeded.
169 * Set the options of the pinyin context.
172 bool pinyin_set_options(pinyin_context_t * context,
173 pinyin_option_t options);
176 * pinyin_alloc_instance:
177 * @context: the pinyin context.
178 * @returns: the newly allocated pinyin instance, NULL if failed.
180 * Allocate a new pinyin instance from the context.
183 pinyin_instance_t * pinyin_alloc_instance(pinyin_context_t * context);
186 * pinyin_free_instance:
187 * @instance: the pinyin instance.
189 * Free the pinyin instance.
192 void pinyin_free_instance(pinyin_instance_t * instance);
196 * pinyin_guess_sentence:
197 * @instance: the pinyin instance.
198 * @returns: whether the sentence are guessed successfully.
200 * Guess a sentence from the saved pinyin keys in the instance.
203 bool pinyin_guess_sentence(pinyin_instance_t * instance);
206 * pinyin_guess_sentence_with_prefix:
207 * @instance: the pinyin instance.
208 * @prefix: the prefix before the sentence.
209 * @returns: whether the sentence are guessed successfully.
211 * Guess a sentence from the saved pinyin keys with a prefix.
214 bool pinyin_guess_sentence_with_prefix(pinyin_instance_t * instance,
215 const char * prefix);
218 * pinyin_phrase_segment:
219 * @instance: the pinyin instance.
220 * @sentence: the utf-8 sentence to be segmented.
221 * @returns: whether the sentence are segmented successfully.
223 * Segment a sentence and saved the result in the instance.
226 bool pinyin_phrase_segment(pinyin_instance_t * instance,
227 const char * sentence);
230 * pinyin_get_sentence:
231 * @instance: the pinyin instance.
232 * @sentence: the saved sentence in the instance.
233 * @returns: whether the sentence is already saved in the instance.
235 * Get the sentence from the instance.
237 * Note: the returned sentence should be freed by g_free().
240 bool pinyin_get_sentence(pinyin_instance_t * instance,
244 * pinyin_parse_full_pinyin:
245 * @instance: the pinyin instance.
246 * @onepinyin: a single full pinyin to be parsed.
247 * @onekey: the parsed key.
248 * @returns: whether the parse is successfully.
250 * Parse a single full pinyin.
253 bool pinyin_parse_full_pinyin(pinyin_instance_t * instance,
254 const char * onepinyin,
255 ChewingKey * onekey);
258 * pinyin_parse_more_full_pinyins:
259 * @instance: the pinyin instance.
260 * @pinyins: the full pinyins to be parsed.
261 * @returns: the parsed length of the full pinyins.
263 * Parse multiple full pinyins and save it in the instance.
266 size_t pinyin_parse_more_full_pinyins(pinyin_instance_t * instance,
267 const char * pinyins);
270 * pinyin_parse_double_pinyin:
271 * @instance: the pinyin instance.
272 * @onepinyin: the single double pinyin to be parsed.
273 * @onekey: the parsed key.
274 * @returns: whether the parse is successfully.
276 * Parse a single double pinyin.
279 bool pinyin_parse_double_pinyin(pinyin_instance_t * instance,
280 const char * onepinyin,
281 ChewingKey * onekey);
284 * pinyin_parse_more_double_pinyins:
285 * @instance: the pinyin instance.
286 * @pinyins: the double pinyins to be parsed.
287 * @returns: the parsed length of the double pinyins.
289 * Parse multiple double pinyins and save it in the instance.
292 size_t pinyin_parse_more_double_pinyins(pinyin_instance_t * instance,
293 const char * pinyins);
296 * pinyin_parse_chewing:
297 * @instance: the pinyin instance.
298 * @onechewing: the single chewing to be parsed.
299 * @onekey: the parsed key.
300 * @returns: whether the parse is successfully.
302 * Parse a single chewing.
305 bool pinyin_parse_chewing(pinyin_instance_t * instance,
306 const char * onechewing,
307 ChewingKey * onekey);
310 * pinyin_parse_more_chewings:
311 * @instance: the pinyin instance.
312 * @chewings: the chewings to be parsed.
313 * @returns: the parsed length of the chewings.
315 * Parse multiple chewings and save it in the instance.
318 size_t pinyin_parse_more_chewings(pinyin_instance_t * instance,
319 const char * chewings);
322 * pinyin_in_chewing_keyboard:
323 * @instance: the pinyin instance.
324 * @key: the input key.
325 * @symbol: the chewing symbol.
326 * @returns: whether the key is in current chewing scheme.
328 * Check whether the input key is in current chewing scheme.
331 bool pinyin_in_chewing_keyboard(pinyin_instance_t * instance,
332 const char key, const char ** symbol);
334 * pinyin_get_candidates:
335 * @instance: the pinyin instance.
336 * @offset: the offset in the pinyin keys.
337 * @candidates: The GArray of token candidates.
338 * @returns: whether a list of tokens are gotten.
340 * Get the candidates at the offset.
343 bool pinyin_get_candidates(pinyin_instance_t * instance,
345 TokenVector candidates);
348 * pinyin_get_full_pinyin_candidates:
349 * @instance: the pinyin instance.
350 * @offset: the offset in the pinyin keys.
351 * @candidates: the GArray of lookup_candidate_t candidates.
352 * @returns: whether a list of lookup_candidate_t candidates are gotten.
354 * Get the full pinyin candidates at the offset.
357 bool pinyin_get_full_pinyin_candidates(pinyin_instance_t * instance,
359 CandidateVector candidates);
362 * pinyin_choose_candidate:
363 * @instance: the pinyin instance.
364 * @offset: the offset in the pinyin keys.
365 * @token: the selected candidate.
366 * @returns: the cursor after the chosen candidate.
368 * Choose an candidate at the offset.
371 int pinyin_choose_candidate(pinyin_instance_t * instance,
373 phrase_token_t token);
376 * pinyin_choose_full_pinyin_candidate:
377 * @instance: the pinyin instance.
378 * @offset: the offset in the pinyin keys.
379 * @candidate: the selected lookup_candidate_t candidate.
380 * @returns: the cursor after the chosen candidate.
382 * Choose a full pinyin candidate at the offset.
385 int pinyin_choose_full_pinyin_candidate(pinyin_instance_t * instance,
387 lookup_candidate_t * candidate);
390 * pinyin_clear_constraint:
391 * @instance: the pinyin instance.
392 * @offset: the offset in the pinyin keys.
393 * @returns: whether the constraint is cleared.
395 * Clear the previous chosen candidate.
398 bool pinyin_clear_constraint(pinyin_instance_t * instance,
402 * pinyin_clear_constraints:
403 * @instance: the pinyin instance.
404 * @returns: whether the constraints are cleared.
406 * Clear all constraints.
409 bool pinyin_clear_constraints(pinyin_instance_t * instance);
412 * pinyin_translate_token:
413 * @instance: the pinyin instance.
414 * @token: the phrase token.
415 * @word: the phrase in utf-8.
416 * @returns: whether the token is valid.
418 * Translate the token to utf-8 phrase.
420 * Note: the returned word should be freed by g_free().
423 bool pinyin_translate_token(pinyin_instance_t * instance,
424 phrase_token_t token, char ** word);
428 * @instance: the pinyin instance.
429 * @returns: whether the sentence is trained.
431 * Train the current user input sentence.
434 bool pinyin_train(pinyin_instance_t * instance);
438 * @instance: the pinyin instance.
439 * @returns: whether the pinyin instance is resetted.
441 * Reset the pinyin instance.
444 bool pinyin_reset(pinyin_instance_t * instance);
448 typedef ChewingKey PinyinKey;
449 typedef ChewingKeyRest PinyinKeyPos;
450 typedef ChewingKeyVector PinyinKeyVector;
451 typedef ChewingKeyRestVector PinyinKeyPosVector;
454 #define LIBPINYIN_FORMAT_VERSION "0.6.91"