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); }
void FcitxLibpinyinUpdatePreedit(FcitxLibpinyin* libpinyin, char* sentence) { FcitxInstance* instance = libpinyin->owner->owner; FcitxInputState* input = FcitxInstanceGetInputState(instance); int offset = LibpinyinGetOffset(libpinyin); if (libpinyin->type == LPT_Pinyin) { int libpinyinLen = strlen(libpinyin->inst->m_raw_full_pinyin); int fcitxLen = strlen(libpinyin->buf); if (fcitxLen != libpinyinLen) { strcpy(libpinyin->buf, libpinyin->inst->m_raw_full_pinyin); libpinyin->cursor_pos += libpinyinLen - fcitxLen; } } int pyoffset = LibpinyinGetPinyinOffset(libpinyin); if (pyoffset > libpinyin->cursor_pos) libpinyin->cursor_pos = pyoffset; int hzlen = 0; if (fcitx_utf8_strlen(sentence) > offset) hzlen = fcitx_utf8_get_nth_char(sentence, offset) - sentence; else hzlen = strlen(sentence); if (hzlen > 0) { char* buf = (char*) fcitx_utils_malloc0((hzlen + 1) * sizeof(char)); strncpy(buf, sentence, hzlen); buf[hzlen] = 0; FcitxMessagesAddMessageAtLast(FcitxInputStateGetPreedit(input), MSG_INPUT, "%s", buf); free(buf); } int charcurpos = hzlen; int lastpos = pyoffset; int curoffset = pyoffset; for (int i = offset; i < libpinyin->inst->m_pinyin_keys->len; i ++) { PinyinKey* pykey = &g_array_index(libpinyin->inst->m_pinyin_keys, PinyinKey, i); PinyinKeyPos* pykeypos = &g_array_index(libpinyin->inst->m_pinyin_key_rests, PinyinKeyPos, i); if (lastpos > 0) { FcitxMessagesMessageConcatLast (FcitxInputStateGetPreedit(input), " "); if (curoffset < libpinyin->cursor_pos) charcurpos ++; for (int j = lastpos; j < pykeypos->m_raw_begin; j ++) { char temp[2] = {'\0', '\0'}; temp[0] = libpinyin->buf[j]; FcitxMessagesMessageConcatLast (FcitxInputStateGetPreedit(input), temp); if (curoffset < libpinyin->cursor_pos) { curoffset ++; charcurpos ++; } } } lastpos = pykeypos->m_raw_end; switch (libpinyin->type) { case LPT_Pinyin: { gchar* pystring = pykey->get_pinyin_string(); FcitxMessagesAddMessageAtLast(FcitxInputStateGetPreedit(input), MSG_CODE, "%s", pystring); size_t pylen = strlen(pystring); if (curoffset + pylen < libpinyin->cursor_pos) { curoffset += pylen; charcurpos += pylen; } else { charcurpos += libpinyin->cursor_pos - curoffset; curoffset = libpinyin->cursor_pos; } g_free(pystring); break; } case LPT_Shuangpin: { if (pykeypos->length() == 2) { const char* initial = 0; if (pykey->m_initial == CHEWING_ZERO_INITIAL) initial = "'"; else initial = get_initial_string(pykey); if (curoffset + 1 <= libpinyin->cursor_pos) { curoffset += 1; charcurpos += strlen(initial); } FcitxMessagesAddMessageAtLast(FcitxInputStateGetPreedit(input), MSG_CODE, "%s", initial); if (curoffset + 1 <= libpinyin->cursor_pos) { curoffset += 1; charcurpos += strlen(get_middle_string(pykey)) + strlen(get_final_string(pykey)); } FcitxMessagesAddMessageAtLast(FcitxInputStateGetPreedit(input), MSG_CODE, "%s%s", get_middle_string(pykey), get_final_string(pykey)); } else if (pykeypos->length() == 1) { gchar* pystring = pykey->get_pinyin_string(); if (curoffset + 1 <= libpinyin->cursor_pos) { curoffset += 1; charcurpos += strlen(pystring); } FcitxMessagesAddMessageAtLast(FcitxInputStateGetPreedit(input), MSG_CODE, "%s", pystring); g_free(pystring); } break; } case LPT_Zhuyin: { gchar* pystring = pykey->get_chewing_string(); FcitxMessagesAddMessageAtLast(FcitxInputStateGetPreedit(input), MSG_CODE, "%s", pystring); if (curoffset + pykeypos->length() <= libpinyin->cursor_pos) { curoffset += pykeypos->length(); charcurpos += strlen(pystring); } else { int diff = libpinyin->cursor_pos - curoffset; curoffset = libpinyin->cursor_pos; size_t len = fcitx_utf8_strlen(pystring); if (pykey->m_tone != CHEWING_ZERO_TONE) len --; if (diff > len) charcurpos += strlen(pystring); else { charcurpos += fcitx_utf8_get_nth_char(pystring, diff) - pystring; } } g_free(pystring); break; } } } int buflen = strlen(libpinyin->buf); if (lastpos < buflen) { FcitxMessagesMessageConcatLast (FcitxInputStateGetPreedit(input), " "); if (lastpos < libpinyin->cursor_pos) charcurpos ++; for (int i = lastpos; i < buflen; i ++) { char temp[2] = {'\0', '\0'}; temp[0] = libpinyin->buf[i]; FcitxMessagesMessageConcatLast (FcitxInputStateGetPreedit(input), temp); if (lastpos < libpinyin->cursor_pos) { charcurpos ++; lastpos++; } } } FcitxInputStateSetCursorPos(input, charcurpos); }
int main(int argc, char *argv[]) { pinyin_context_t* context = pinyin_init(LIBPINYIN_PKGDATADIR "/data", NULL); pinyin_instance_t* inst = pinyin_alloc_instance(context); pinyin_set_options(context, IS_PINYIN | USE_DIVIDED_TABLE | USE_RESPLIT_TABLE); string s; cin >> s ; pinyin_parse_more_double_pinyins(inst, s.c_str()); int cursor = 0; for (int i = 0; i < inst->m_pinyin_keys->len; i ++) { PinyinKey* pykey = &g_array_index(inst->m_pinyin_keys, PinyinKey, i); gchar* py = pykey->get_pinyin_string(); gchar* chewing = pykey->get_chewing_string(); cout << py << " " << chewing << endl; g_free(py); g_free(chewing); } while (true) { cout << get_lookup_cursor(inst, cursor) << endl; GArray* array = g_array_new(FALSE, FALSE, sizeof(lookup_candidate_t)); pinyin_get_candidates(inst, get_lookup_cursor(inst, cursor), array); cout << array->len << endl; pinyin_guess_sentence(inst); char* sentence = NULL; pinyin_get_sentence(inst, &sentence); if (sentence) cout << sentence << endl; else cout << "no sentence" << endl; g_free(sentence); for (int i = 0 ; i < array->len; i ++ ) { lookup_candidate_t token = g_array_index(array, lookup_candidate_t, i); char* word = NULL; pinyin_translate_token(inst, token.m_token, &word); if (word) cout << word << " "; g_free(word); } cout << "constraints " << inst->m_constraints->len << endl; int cand; cin >> cursor >> cand; if (cand >= 0) pinyin_choose_candidate(inst, 0, &g_array_index(array, lookup_candidate_t, cand)); else if (cand != -1) { pinyin_clear_constraints(inst); } else if (cand != -2) { break; } g_array_free(array, TRUE); } pinyin_free_instance(inst); pinyin_fini(context); }