PageRenderTime 53ms CodeModel.GetById 14ms app.highlight 34ms RepoModel.GetById 1ms app.codeStats 0ms

/src/im/gpinyin/share/pinyinime.cpp

http://ftk.googlecode.com/
C++ | 186 lines | 125 code | 41 blank | 20 comment | 35 complexity | cb78f7597cfe2b38185e686405101251 MD5 | raw file
  1/*
  2 * Copyright (C) 2009 The Android Open Source Project
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 *      http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17#include <stdlib.h>
 18#include "../include/pinyinime.h"
 19#include "../include/dicttrie.h"
 20#include "../include/matrixsearch.h"
 21#include "../include/spellingtrie.h"
 22
 23#ifdef __cplusplus
 24extern "C" {
 25#endif
 26
 27  using namespace ime_pinyin;
 28
 29  // The maximum number of the prediction items.
 30  static const unsigned kMaxPredictNum = 500;
 31
 32  // Used to search Pinyin string and give the best candidate.
 33  MatrixSearch* matrix_search = NULL;
 34
 35  char16 predict_buf[kMaxPredictNum][kMaxPredictSize + 1];
 36
 37  bool im_open_decoder(const char *fn_sys_dict, const char *fn_usr_dict) {
 38    if (NULL != matrix_search)
 39      delete matrix_search;
 40
 41    matrix_search = new MatrixSearch();
 42    if (NULL == matrix_search) {
 43      return false;
 44    }
 45
 46    return matrix_search->init(fn_sys_dict, fn_usr_dict);
 47  }
 48
 49  bool im_open_decoder_fd(int sys_fd, long start_offset, long length,
 50                          const char *fn_usr_dict) {
 51    if (NULL != matrix_search)
 52      delete matrix_search;
 53
 54    matrix_search = new MatrixSearch();
 55    if (NULL == matrix_search)
 56      return false;
 57
 58    return matrix_search->init_fd(sys_fd, start_offset, length, fn_usr_dict);
 59  }
 60
 61  void im_close_decoder() {
 62    if (NULL != matrix_search) {
 63      matrix_search->close();
 64      delete matrix_search;
 65    }
 66    matrix_search = NULL;
 67  }
 68
 69  void im_set_max_lens(unsigned max_sps_len, unsigned max_hzs_len) {
 70    if (NULL != matrix_search) {
 71      matrix_search->set_max_lens(max_sps_len, max_hzs_len);
 72    }
 73  }
 74
 75  void im_flush_cache() {
 76    if (NULL != matrix_search)
 77      matrix_search->flush_cache();
 78  }
 79
 80  // To be updated.
 81  unsigned im_search(const char* pybuf, unsigned pylen) {
 82    if (NULL == matrix_search)
 83      return 0;
 84
 85    matrix_search->search(pybuf, pylen);
 86    return matrix_search->get_candidate_num();
 87  }
 88
 89  unsigned im_delsearch(unsigned pos, bool is_pos_in_splid,
 90                      bool clear_fixed_this_step) {
 91    if (NULL == matrix_search)
 92      return 0;
 93    matrix_search->delsearch(pos, is_pos_in_splid, clear_fixed_this_step);
 94    return matrix_search->get_candidate_num();
 95  }
 96
 97  void im_reset_search() {
 98    if (NULL == matrix_search)
 99      return;
100
101    matrix_search->reset_search();
102  }
103
104  // To be removed
105  unsigned im_add_letter(char ch) {
106    return 0;
107  }
108
109  const char* im_get_sps_str(unsigned *decoded_len) {
110    if (NULL == matrix_search)
111      return NULL;
112
113    return matrix_search->get_pystr(decoded_len);
114  }
115
116  char16* im_get_candidate(unsigned cand_id, char16* cand_str,
117                        unsigned max_len) {
118    if (NULL == matrix_search)
119      return NULL;
120
121    return matrix_search->get_candidate(cand_id, cand_str, max_len);
122  }
123
124  unsigned im_get_spl_start_pos(const uint16 *&spl_start) {
125    if (NULL == matrix_search)
126      return 0;
127
128    return matrix_search->get_spl_start(spl_start);
129  }
130
131  unsigned im_choose(unsigned choice_id) {
132    if (NULL == matrix_search)
133      return 0;
134
135    return matrix_search->choose(choice_id);
136  }
137
138  unsigned im_cancel_last_choice() {
139    if (NULL == matrix_search)
140      return 0;
141
142    return matrix_search->cancel_last_choice();
143  }
144
145  unsigned im_get_fixed_len() {
146    if (NULL == matrix_search)
147      return 0;
148
149    return matrix_search->get_fixedlen();
150  }
151
152  // To be removed
153  bool im_cancel_input() {
154    return true;
155  }
156
157
158  unsigned im_get_predicts(const char16 *his_buf,
159                         char16 (*&pre_buf)[kMaxPredictSize + 1]) {
160    if (NULL == his_buf)
161      return 0;
162
163    unsigned fixed_len = utf16_strlen(his_buf);
164    const char16 *fixed_ptr = his_buf;
165    if (fixed_len > kMaxPredictSize) {
166      fixed_ptr += fixed_len - kMaxPredictSize;
167      fixed_len = kMaxPredictSize;
168    }
169
170    pre_buf = predict_buf;
171    return matrix_search->get_predicts(his_buf, pre_buf, kMaxPredictNum);
172  }
173
174  void im_enable_shm_as_szm(bool enable) {
175    SpellingTrie &spl_trie = SpellingTrie::get_instance();
176    spl_trie.szm_enable_shm(enable);
177  }
178
179  void im_enable_ym_as_szm(bool enable) {
180    SpellingTrie &spl_trie = SpellingTrie::get_instance();
181    spl_trie.szm_enable_ym(enable);
182  }
183
184#ifdef __cplusplus
185}
186#endif