예제 #1
0
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);
    }
}
예제 #2
0
/**
 * @brief get the candidate word by index
 *
 * @param iIndex index of candidate word
 * @return the string of canidate word
 **/
__EXPORT_API
INPUT_RETURN_VALUE FcitxLibpinyinGetCandWord (void* arg, FcitxCandidateWord* candWord)
{
    FcitxLibpinyin* libpinyin = (FcitxLibpinyin* )arg;
    FcitxLibpinyinCandWord* pyCand = (FcitxLibpinyinCandWord*) candWord->priv;
    FcitxInstance* instance = libpinyin->owner->owner;
    FcitxInputState* input = FcitxInstanceGetInputState(instance);

    if (pyCand->ispunc) {
        strcpy(FcitxInputStateGetOutputString(input), candWord->strWord);
        return IRV_COMMIT_STRING;
    } else {
        if (!libpinyin->candidate || libpinyin->candidate->len <= pyCand->idx)
            return IRV_TO_PROCESS;
        lookup_candidate_t* cand = &g_array_index(libpinyin->candidate, lookup_candidate_t, pyCand->idx);
        pinyin_choose_candidate(libpinyin->inst, LibpinyinGetOffset(libpinyin), cand);

        FcitxLibpinyinFixed f;
        f.len = fcitx_utf8_strlen(cand->m_phrase_string);
        g_array_append_val(libpinyin->fixed_string, f);

        int offset = LibpinyinGetOffset(libpinyin);
        if (offset >= libpinyin->inst->m_pinyin_keys->len)
        {
            char* sentence = NULL;
            pinyin_guess_sentence(libpinyin->inst);
            sentence = LibpinyinGetSentence(libpinyin);
            if (sentence) {
                strcpy(FcitxInputStateGetOutputString(input), sentence);
                g_free(sentence);
                pinyin_train(libpinyin->inst);
            }
            else
                strcpy(FcitxInputStateGetOutputString(input), "");

            return IRV_COMMIT_STRING;
        }

        int pyoffset = LibpinyinGetPinyinOffset(libpinyin);
        if (pyoffset > libpinyin->cursor_pos)
            libpinyin->cursor_pos = pyoffset;

        return IRV_DISPLAY_CANDWORDS;
    }
    return IRV_TO_PROCESS;
}
예제 #3
0
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;
}