/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. #include <stdlib.h>
  17. #include "../include/pinyinime.h"
  18. #include "../include/dicttrie.h"
  19. #include "../include/matrixsearch.h"
  20. #include "../include/spellingtrie.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. using namespace ime_pinyin;
  25. // The maximum number of the prediction items.
  26. static const unsigned kMaxPredictNum = 500;
  27. // Used to search Pinyin string and give the best candidate.
  28. MatrixSearch* matrix_search = NULL;
  29. char16 predict_buf[kMaxPredictNum][kMaxPredictSize + 1];
  30. bool im_open_decoder(const char *fn_sys_dict, const char *fn_usr_dict) {
  31. if (NULL != matrix_search)
  32. delete matrix_search;
  33. matrix_search = new MatrixSearch();
  34. if (NULL == matrix_search) {
  35. return false;
  36. }
  37. return matrix_search->init(fn_sys_dict, fn_usr_dict);
  38. }
  39. bool im_open_decoder_fd(int sys_fd, long start_offset, long length,
  40. const char *fn_usr_dict) {
  41. if (NULL != matrix_search)
  42. delete matrix_search;
  43. matrix_search = new MatrixSearch();
  44. if (NULL == matrix_search)
  45. return false;
  46. return matrix_search->init_fd(sys_fd, start_offset, length, fn_usr_dict);
  47. }
  48. void im_close_decoder() {
  49. if (NULL != matrix_search) {
  50. matrix_search->close();
  51. delete matrix_search;
  52. }
  53. matrix_search = NULL;
  54. }
  55. void im_set_max_lens(unsigned max_sps_len, unsigned max_hzs_len) {
  56. if (NULL != matrix_search) {
  57. matrix_search->set_max_lens(max_sps_len, max_hzs_len);
  58. }
  59. }
  60. void im_flush_cache() {
  61. if (NULL != matrix_search)
  62. matrix_search->flush_cache();
  63. }
  64. // To be updated.
  65. unsigned im_search(const char* pybuf, unsigned pylen) {
  66. if (NULL == matrix_search)
  67. return 0;
  68. matrix_search->search(pybuf, pylen);
  69. return matrix_search->get_candidate_num();
  70. }
  71. unsigned im_delsearch(unsigned pos, bool is_pos_in_splid,
  72. bool clear_fixed_this_step) {
  73. if (NULL == matrix_search)
  74. return 0;
  75. matrix_search->delsearch(pos, is_pos_in_splid, clear_fixed_this_step);
  76. return matrix_search->get_candidate_num();
  77. }
  78. void im_reset_search() {
  79. if (NULL == matrix_search)
  80. return;
  81. matrix_search->reset_search();
  82. }
  83. // To be removed
  84. unsigned im_add_letter(char ch) {
  85. return 0;
  86. }
  87. const char* im_get_sps_str(unsigned *decoded_len) {
  88. if (NULL == matrix_search)
  89. return NULL;
  90. return matrix_search->get_pystr(decoded_len);
  91. }
  92. char16* im_get_candidate(unsigned cand_id, char16* cand_str,
  93. unsigned max_len) {
  94. if (NULL == matrix_search)
  95. return NULL;
  96. return matrix_search->get_candidate(cand_id, cand_str, max_len);
  97. }
  98. unsigned im_get_spl_start_pos(const uint16 *&spl_start) {
  99. if (NULL == matrix_search)
  100. return 0;
  101. return matrix_search->get_spl_start(spl_start);
  102. }
  103. unsigned im_choose(unsigned choice_id) {
  104. if (NULL == matrix_search)
  105. return 0;
  106. return matrix_search->choose(choice_id);
  107. }
  108. unsigned im_cancel_last_choice() {
  109. if (NULL == matrix_search)
  110. return 0;
  111. return matrix_search->cancel_last_choice();
  112. }
  113. unsigned im_get_fixed_len() {
  114. if (NULL == matrix_search)
  115. return 0;
  116. return matrix_search->get_fixedlen();
  117. }
  118. // To be removed
  119. bool im_cancel_input() {
  120. return true;
  121. }
  122. unsigned im_get_predicts(const char16 *his_buf,
  123. char16 (*&pre_buf)[kMaxPredictSize + 1]) {
  124. if (NULL == his_buf)
  125. return 0;
  126. unsigned fixed_len = utf16_strlen(his_buf);
  127. const char16 *fixed_ptr = his_buf;
  128. if (fixed_len > kMaxPredictSize) {
  129. fixed_ptr += fixed_len - kMaxPredictSize;
  130. fixed_len = kMaxPredictSize;
  131. }
  132. pre_buf = predict_buf;
  133. return matrix_search->get_predicts(his_buf, pre_buf, kMaxPredictNum);
  134. }
  135. void im_enable_shm_as_szm(bool enable) {
  136. SpellingTrie &spl_trie = SpellingTrie::get_instance();
  137. spl_trie.szm_enable_shm(enable);
  138. }
  139. void im_enable_ym_as_szm(bool enable) {
  140. SpellingTrie &spl_trie = SpellingTrie::get_instance();
  141. spl_trie.szm_enable_ym(enable);
  142. }
  143. #ifdef __cplusplus
  144. }
  145. #endif