~ubuntu-branches/ubuntu/saucy/ibus-pinyin/saucy-proposed

« back to all changes in this revision

Viewing changes to src/PhraseEditor.cc

  • Committer: Bazaar Package Importer
  • Author(s): LI Daobing, Asias He
  • Date: 2010-09-08 21:38:54 UTC
  • mfrom: (1.2.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20100908213854-q4wlx8zlcyqxvelz
Tags: 1.3.11-1
[ Asias He ]
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* vim:set et ts=4 sts=4:
2
 
 *
3
 
 * ibus-pinyin - The Chinese PinYin engine for IBus
4
 
 *
5
 
 * Copyright (c) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
6
 
 *
7
 
 * This program is free software; you can redistribute it and/or modify
8
 
 * it under the terms of the GNU General Public License as published by
9
 
 * the Free Software Foundation; either version 2, or (at your option)
10
 
 * any later version.
11
 
 *
12
 
 * This program is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 * GNU General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU General Public License
18
 
 * along with this program; if not, write to the Free Software
19
 
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
 
 */
21
 
#include "PhraseEditor.h"
22
 
#include "Config.h"
23
 
#include "Database.h"
24
 
#include "PinyinProperties.h"
25
 
#include "SimpTradConverter.h"
26
 
 
27
 
namespace PY {
28
 
 
29
 
PhraseEditor::PhraseEditor (PinyinProperties & props, Config & config)
30
 
    : m_candidates (32),
31
 
      m_selected_phrases (8),
32
 
      m_selected_string (32),
33
 
      m_candidate_0_phrases (8),
34
 
      m_pinyin (16),
35
 
      m_cursor (0),
36
 
      m_props (props),
37
 
      m_config (config)
38
 
{
39
 
}
40
 
 
41
 
PhraseEditor::~PhraseEditor (void)
42
 
{
43
 
}
44
 
 
45
 
gboolean
46
 
PhraseEditor::update (const PinyinArray &pinyin)
47
 
{
48
 
    /* the size of pinyin must not bigger than MAX_PHRASE_LEN */
49
 
    g_assert (pinyin.size () <= MAX_PHRASE_LEN);
50
 
 
51
 
    m_pinyin = pinyin;
52
 
    m_cursor = 0;
53
 
 
54
 
    /* FIXME, should not remove all phrases1 */
55
 
    m_selected_phrases.clear ();
56
 
    m_selected_string.truncate (0);
57
 
    updateCandidates ();
58
 
    return TRUE;
59
 
}
60
 
 
61
 
gboolean
62
 
PhraseEditor::resetCandidate (guint i)
63
 
{
64
 
    Database::instance ().remove (m_candidates[i]);
65
 
 
66
 
    updateCandidates ();
67
 
    return TRUE;
68
 
}
69
 
 
70
 
void
71
 
PhraseEditor::commit (void)
72
 
{
73
 
    Database::instance ().commit (m_selected_phrases);
74
 
    reset ();
75
 
}
76
 
 
77
 
gboolean
78
 
PhraseEditor::selectCandidate (guint i)
79
 
{
80
 
    if (G_UNLIKELY (i >= m_candidates.size ()))
81
 
        return FALSE;
82
 
 
83
 
    if (G_LIKELY (i == 0)) {
84
 
        m_selected_phrases.insert (m_selected_phrases.end (),
85
 
                                   m_candidate_0_phrases.begin (),
86
 
                                   m_candidate_0_phrases.end ());
87
 
        if (G_LIKELY (m_props.modeSimp ()))
88
 
            m_selected_string << m_candidates[0].phrase;
89
 
        else
90
 
            SimpTradConverter::simpToTrad (m_candidates[0].phrase, m_selected_string);
91
 
        m_cursor = m_pinyin.size ();
92
 
    }
93
 
    else {
94
 
        m_selected_phrases.push_back (m_candidates[i]);
95
 
        if (G_LIKELY (m_props.modeSimp ()))
96
 
            m_selected_string << m_candidates[i].phrase;
97
 
        else
98
 
            SimpTradConverter::simpToTrad (m_candidates[i].phrase, m_selected_string);
99
 
        m_cursor += m_candidates[i].len;
100
 
    }
101
 
 
102
 
    updateCandidates ();
103
 
    return TRUE;
104
 
}
105
 
 
106
 
void
107
 
PhraseEditor::updateCandidates (void)
108
 
{
109
 
    m_candidates.clear ();
110
 
    m_query.reset ();
111
 
    updateTheFirstCandidate ();
112
 
 
113
 
    if (G_UNLIKELY (m_pinyin.size () == 0))
114
 
        return;
115
 
 
116
 
    if (G_LIKELY (m_candidate_0_phrases.size () > 1)) {
117
 
        Phrase phrase;
118
 
        phrase.reset ();
119
 
        for (guint i = 0; i < m_candidate_0_phrases.size (); i++)
120
 
            phrase += m_candidate_0_phrases[i];
121
 
        m_candidates.push_back (phrase);
122
 
    }
123
 
 
124
 
    m_query.reset (new Query (m_pinyin,
125
 
                              m_cursor,
126
 
                              m_pinyin.size () - m_cursor,
127
 
                              m_config.option ()));
128
 
    fillCandidates ();
129
 
}
130
 
 
131
 
void
132
 
PhraseEditor::updateTheFirstCandidate (void)
133
 
{
134
 
    guint begin;
135
 
    guint end;
136
 
 
137
 
    m_candidate_0_phrases.clear ();
138
 
 
139
 
    if (G_UNLIKELY (m_pinyin.size () == 0))
140
 
        return;
141
 
 
142
 
    begin = m_cursor;
143
 
    end = m_pinyin.size ();
144
 
 
145
 
    while (begin != end) {
146
 
        gint ret;
147
 
        Query query (m_pinyin,
148
 
                     begin,
149
 
                     end - begin,
150
 
                     m_config.option ());
151
 
        ret = query.fill (m_candidate_0_phrases, 1);
152
 
        g_assert (ret == 1);
153
 
        begin += m_candidate_0_phrases.back ().len;
154
 
    }
155
 
}
156
 
 
157
 
gboolean
158
 
PhraseEditor::fillCandidates (void)
159
 
{
160
 
    if (G_UNLIKELY (m_query.get () == NULL)) {
161
 
        return FALSE;
162
 
    }
163
 
 
164
 
    gint ret = m_query->fill (m_candidates, FILL_GRAN);
165
 
 
166
 
    if (G_UNLIKELY (ret < FILL_GRAN)) {
167
 
        /* got all candidates from query */
168
 
        m_query.reset ();
169
 
    }
170
 
 
171
 
    return ret > 0 ? TRUE : FALSE;
172
 
}
173
 
 
174
 
};
175
 
 
176