summaryrefslogtreecommitdiffstats
path: root/src/PYPLibPinyinCandidates.cc
blob: 91865be8f55cc1ff3008cd25a6fd715aa691c531 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* vim:set et ts=4 sts=4:
 *
 * ibus-libpinyin - Intelligent Pinyin engine based on libpinyin for IBus
 *
 * Copyright (c) 2018 Peng Wu <alexepico@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "PYPLibPinyinCandidates.h"
#include <assert.h>
#include <pinyin.h>
#include "PYPPhoneticEditor.h"

using namespace PY;

gboolean
LibPinyinCandidates::processCandidates (std::vector<EnhancedCandidate> & candidates)
{
    pinyin_instance_t *instance = m_editor->m_instance;

    guint len = 0;
    pinyin_get_n_candidate (instance, &len);

    for (guint i = 0; i < len; i++) {
        lookup_candidate_t * candidate = NULL;
        pinyin_get_candidate (instance, i, &candidate);

        lookup_candidate_type_t type;
        pinyin_get_candidate_type (instance, candidate, &type);

        const gchar * phrase_string = NULL;
        pinyin_get_candidate_string (instance, candidate, &phrase_string);

        EnhancedCandidate enhanced;

        switch (type) {
        case NBEST_MATCH_CANDIDATE:
            enhanced.m_candidate_type = CANDIDATE_NBEST_MATCH;
            break;

        case NORMAL_CANDIDATE:
        case ADDON_CANDIDATE:
            enhanced.m_candidate_type = CANDIDATE_NORMAL;

            if (pinyin_is_user_candidate (instance, candidate))
                enhanced.m_candidate_type = CANDIDATE_USER;

            break;

        default:
            assert (FALSE);
        }

        enhanced.m_candidate_id = i;
        enhanced.m_display_string = phrase_string;

        candidates.push_back (enhanced);
    }

    return TRUE;
}

SelectCandidateAction
LibPinyinCandidates::selectCandidate (EnhancedCandidate & enhanced)
{
    pinyin_instance_t * instance = m_editor->m_instance;
    assert (CANDIDATE_NBEST_MATCH == enhanced.m_candidate_type ||
            CANDIDATE_NORMAL == enhanced.m_candidate_type ||
            CANDIDATE_USER == enhanced.m_candidate_type);

    guint len = 0;
    pinyin_get_n_candidate (instance, &len);

    if (G_UNLIKELY (enhanced.m_candidate_id >= len))
        return SELECT_CANDIDATE_ALREADY_HANDLED;

    guint lookup_cursor = m_editor->getLookupCursor ();

    lookup_candidate_t * candidate = NULL;
    pinyin_get_candidate (instance, enhanced.m_candidate_id, &candidate);

    if (CANDIDATE_NBEST_MATCH == enhanced.m_candidate_type) {
        /* because nbest match candidate
           starts from the beginning of user input. */
        pinyin_choose_candidate (instance, 0, candidate);

        guint8 index = 0;
        pinyin_get_candidate_nbest_index(instance, candidate, &index);

        if (index != 0)
            pinyin_train (instance, index);

        return SELECT_CANDIDATE_COMMIT;
    }

    lookup_cursor = pinyin_choose_candidate
        (instance, lookup_cursor, candidate);

    pinyin_guess_sentence (instance);

    if (lookup_cursor == m_editor->m_text.length ()) {
        char *tmp = NULL;
        pinyin_get_sentence (instance, 0, &tmp);
        enhanced.m_display_string = tmp;
        pinyin_train (instance, 0);
        return SELECT_CANDIDATE_MODIFY_IN_PLACE_AND_COMMIT;
    }

    PinyinKeyPos *pos = NULL;
    pinyin_get_pinyin_key_rest (instance, lookup_cursor, &pos);

    guint16 begin = 0;
    pinyin_get_pinyin_key_rest_positions (instance, pos, &begin, NULL);
    m_editor->m_cursor = begin;

    return SELECT_CANDIDATE_UPDATE_ALL;
}