summaryrefslogtreecommitdiffstats
path: root/src/PYPEnglishCandidates.cc
blob: 0da60a993c922b9102de43189afb2f1bc32ff667 (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
/* vim:set et ts=4 sts=4:
 *
 * ibus-libpinyin - Intelligent Pinyin engine based on libpinyin for IBus
 *
 * Copyright (c) 2021 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
 */

#include "PYPEnglishCandidates.h"
#include <algorithm>
#include <assert.h>

using namespace PY;

static const int MINIMAL_ENGLISH_CHARACTERS = 4;
static const int MAXIMAL_ENGLISH_CANDIDATES = 2;

EnglishCandidates::EnglishCandidates (Editor *editor)
    : m_train_factor (0.1)
{
    m_editor = editor;

    m_english_database = & EnglishDatabase::instance ();
}

static bool compare_string_length (const std::string & lhs, const std::string &rhs)
{
    return lhs.length () < rhs.length ();
}

gboolean
EnglishCandidates::processCandidates (std::vector<EnhancedCandidate> & candidates)
{
    if (m_editor->m_text.length () < MINIMAL_ENGLISH_CHARACTERS)
        return FALSE;

    const char *prefix = m_editor->m_text.c_str ();
    std::vector<std::string> words;

    std::vector<EnhancedCandidate>::iterator pos;
    for (pos = candidates.begin (); pos != candidates.end (); ++pos) {
        if (CANDIDATE_NBEST_MATCH != pos->m_candidate_type &&
            CANDIDATE_LONGER != pos->m_candidate_type &&
            CANDIDATE_LONGER_USER != pos->m_candidate_type)
            break;
    }

    EnhancedCandidate enhanced;
    enhanced.m_candidate_type = CANDIDATE_ENGLISH;

    int count = 0;
    if (m_english_database->listWords (prefix, words)) {
        // sort the words by length and frequency
        std::stable_sort (words.begin (), words.end (), compare_string_length);

        // list the shortest words here
        for (auto iter = words.begin (); iter != words.end (); ++iter) {
            if (count >= MAXIMAL_ENGLISH_CANDIDATES)
                break;

            enhanced.m_candidate_id = count;
            enhanced.m_display_string = *iter;
            candidates.insert (pos + count, enhanced);

            ++count;
        }

        return TRUE;
    }

    return FALSE;
}

int
EnglishCandidates::selectCandidate (EnhancedCandidate & enhanced)
{
    assert (CANDIDATE_ENGLISH == enhanced.m_candidate_type);
    assert (enhanced.m_candidate_id < MAXIMAL_ENGLISH_CANDIDATES);

    m_english_database->train (enhanced.m_display_string.c_str (), m_train_factor);

    return SELECT_CANDIDATE_DIRECT_COMMIT;
}

gboolean
EnglishCandidates::removeCandidate (EnhancedCandidate & enhanced)
{
    assert (CANDIDATE_ENGLISH == enhanced.m_candidate_type);
    assert (enhanced.m_candidate_id < MAXIMAL_ENGLISH_CANDIDATES);

    return m_english_database->deleteUserWord (enhanced.m_display_string.c_str ());
}