void PinyinAdapter::wordCandidateSelected(const QString& word) { Q_UNUSED(word) lookup_candidate_t * candidate = NULL; if (pinyin_get_candidate(m_instance, 1, &candidate)) { pinyin_choose_candidate(m_instance, 0, candidate); } }
void PinyinAdapter::parse(const QString& string) { pinyin_parse_more_full_pinyins(m_instance, string.toLatin1().data()); #ifdef PINYIN_DEBUG for (int i = 0; i < m_instance->m_pinyin_keys->len; i ++) { PinyinKey* pykey = &g_array_index(m_instance->m_pinyin_keys, PinyinKey, i); gchar* py = pykey->get_pinyin_string(); std::cout << py << " "; g_free(py); } std::cout << std::endl; #endif pinyin_guess_candidates(m_instance, 0); candidates.clear(); guint len = 0; pinyin_get_n_candidate(m_instance, &len); len = len > MAX_SUGGESTIONS ? MAX_SUGGESTIONS : len; for (unsigned int i = 0 ; i < len; i ++ ) { lookup_candidate_t * candidate = NULL; if (pinyin_get_candidate(m_instance, i, &candidate)) { const char* word = NULL; pinyin_get_candidate_string(m_instance, candidate, &word); // Translate the token to utf-8 phrase. if (word) { candidates.append(QString(word)); } } } Q_EMIT newPredictionSuggestions(string, candidates); }
static PyObject * SimplePinyin_convert(SimplePinyin* self, PyObject *args, PyObject *kwds) { const char *pinyin = ""; const char *prefix = ""; static char *kwlist[] = {"pinyin", "prefix", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|s", kwlist, &pinyin, &prefix)) return NULL; // printf("DEBUG: pinyin=%s, prefix=%s.\n", pinyin, prefix); pinyin_parse_more_full_pinyins(self->instance, pinyin); pinyin_guess_sentence_with_prefix(self->instance, prefix); pinyin_guess_full_pinyin_candidates(self->instance, 0); guint num = 0; guint16 *arr = NULL; //FIXME: Use a name better than `arr` pinyin_get_n_pinyin(self->instance, &num); arr = PyMem_New(guint16, num); // printf("DEBUG: num=%i, arr=%p.\n", num, arr); for (size_t i = 0; i < num; ++i) { ChewingKeyRest *key_rest = NULL; pinyin_get_pinyin_key_rest(self->instance, i, &key_rest); pinyin_get_pinyin_key_rest_length(self->instance, key_rest, &arr[i]); if (i > 0) { arr[i] += arr[i-1]; } // printf("DEBUG: %i\n", arr[i]); } guint len = 0; pinyin_get_n_candidate(self->instance, &len); // printf("DEBUG: len=%i\n", len); PyObject *candidate_list = PyList_New(len); PyObject *match_len_list = PyList_New(len); for (size_t i = 0; i < len; ++i) { lookup_candidate_t * candidate = NULL; pinyin_get_candidate(self->instance, i, &candidate); const char * word = NULL; pinyin_get_candidate_string(self->instance, candidate, &word); PyObject *ob_word = NULL; ob_word = Py_BuildValue("s", word); PyList_SetItem(candidate_list, i, ob_word); lookup_candidate_type_t type; pinyin_get_candidate_type(self->instance, candidate, &type); // printf("DEBUG: type=%i\n", type); int cursor = pinyin_choose_candidate(self->instance, 0, candidate); int match_len = 0; int index = 0; switch (type) { case BEST_MATCH_CANDIDATE: match_len = strlen(pinyin); break; case DIVIDED_CANDIDATE: //FIXME: we assume that only one key get divided index = cursor-2; //FIXME: remove the below hack if possible if (index >= num) { index = num-1; } match_len = arr[index]; break; case RESPLIT_CANDIDATE: case NORMAL_CANDIDATE: index = cursor-1; match_len = arr[index]; default: break; } // printf("DEBUG: match_len=%i\n", match_len); PyObject *ob_match_len = NULL; ob_match_len = Py_BuildValue("i", match_len); PyList_SetItem(match_len_list, i, ob_match_len); pinyin_clear_constraint(self->instance, 0); // printf("DEBUG: %s %d\n", word, arr[cursor-1]); } PyMem_Del(arr); pinyin_reset(self->instance); PyObject *ob_pair = NULL; ob_pair = Py_BuildValue("(O,O)", candidate_list, match_len_list); return ob_pair; }