~ubuntu-branches/ubuntu/trusty/presage/trusty-proposed

« back to all changes in this revision

Viewing changes to src/lib/core/selector.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Matteo Vescovi
  • Date: 2011-08-06 09:26:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110806092615-0wvhajaht9974ncx
Tags: upstream-0.8.6
ImportĀ upstreamĀ versionĀ 0.8.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/******************************************************
 
3
 *  Presage, an extensible predictive text entry system
 
4
 *  ---------------------------------------------------
 
5
 *
 
6
 *  Copyright (C) 2008  Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
 
7
 
 
8
    This program is free software; you can redistribute it and/or modify
 
9
    it under the terms of the GNU General Public License as published by
 
10
    the Free Software Foundation; either version 2 of the License, or
 
11
    (at your option) any later version.
 
12
 
 
13
    This program is distributed in the hope that it will be useful,
 
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
    GNU General Public License for more details.
 
17
 
 
18
    You should have received a copy of the GNU General Public License along
 
19
    with this program; if not, write to the Free Software Foundation, Inc.,
 
20
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
21
                                                                             *
 
22
                                                                **********(*)*/
 
23
 
 
24
 
 
25
#include "selector.h"
 
26
#include "utility.h"
 
27
 
 
28
const char* Selector::SUGGESTIONS = "Presage.Selector.SUGGESTIONS";
 
29
const char* Selector::REPEAT_SUGGESTIONS = "Presage.Selector.REPEAT_SUGGESTIONS";
 
30
const char* Selector::GREEDY_SUGGESTION_THRESHOLD = "Presage.Selector.GREEDY_SUGGESTION_THRESHOLD";
 
31
 
 
32
const char* Selector::LOGGER = "Presage.Selector.LOGGER";
 
33
 
 
34
Selector::Selector(Configuration* configuration, ContextTracker* ct)
 
35
    : contextTracker(ct),
 
36
      config(configuration),
 
37
      logger("Selector", std::cerr),
 
38
      dispatcher(this)
 
39
{
 
40
    // build notification dispatch map
 
41
    dispatcher.map (config->find (LOGGER), & Selector::set_logger);
 
42
    dispatcher.map (config->find (SUGGESTIONS), & Selector::set_suggestions);
 
43
    dispatcher.map (config->find (REPEAT_SUGGESTIONS), & Selector::set_repeat_suggestions);
 
44
    dispatcher.map (config->find (GREEDY_SUGGESTION_THRESHOLD), & Selector::set_greedy_suggestion_threshold);
 
45
 
 
46
    // set prefix
 
47
    previous_prefix = contextTracker->getPrefix();
 
48
}
 
49
 
 
50
Selector::~Selector()
 
51
{
 
52
    // nothing to do here, move along
 
53
}
 
54
 
 
55
std::vector<std::string> Selector::select( Prediction p )
 
56
{
 
57
    // copy words from Prediction.Suggestion.word in result vector
 
58
    std::vector<std::string> result;
 
59
    std::string token;
 
60
    for (size_t i=0 ; i<p.size() ; i++) {
 
61
        token =  p.getSuggestion(i).getWord();
 
62
        result.push_back(token);
 
63
        logger << DEBUG << "Added token to selector consideration set: " << token << endl;
 
64
    }
 
65
        
 
66
    // check whether user has not moved on to a new word
 
67
    if (contextTracker->contextChange()) {
 
68
        logger << DEBUG << "Context change detected." << endl;
 
69
        clearSuggestedWords();
 
70
    } else {
 
71
        logger << DEBUG << "No context change detected." << endl;
 
72
    }
 
73
 
 
74
    // filter out suggestions that do not satisfy repetition constraint
 
75
    if( !repeat_suggestions )
 
76
        repetitionFilter( result );
 
77
 
 
78
    // filter out suggestions that do not satisfy threshold constraint
 
79
    if( greedy_suggestion_threshold > 0 )
 
80
        thresholdFilter( result );
 
81
 
 
82
    // build result
 
83
        
 
84
    // check that we have enough selected words
 
85
    if( result.size() < static_cast<unsigned int>(suggestions) ) {
 
86
        // Job's not done, got to get a bigger Prediction
 
87
        // we should invoke predict() to get more Suggestions
 
88
 
 
89
        // could throw an exception that would be caught by predictor
 
90
        // which would reissue the predict call to get more
 
91
        // suggestions
 
92
                        
 
93
        // TODO <============================================
 
94
                        
 
95
        // just abort for now
 
96
        //std::cerr << "Not enough Suggestions" << std::endl;
 
97
        //abort();
 
98
                
 
99
    } else {
 
100
        // erase the requested number of words
 
101
        result.erase( result.begin() + suggestions, result.end() );
 
102
    }
 
103
 
 
104
    // update suggested words set
 
105
    updateSuggestedWords( result );
 
106
 
 
107
    return result;
 
108
}
 
109
 
 
110
 
 
111
/** Trigger update of the suggested tokens cache
 
112
 *
 
113
 */
 
114
void Selector::update()
 
115
{
 
116
    // check whether user has not moved on to a new word
 
117
    if (contextTracker->contextChange()) {
 
118
        clearSuggestedWords();
 
119
    }
 
120
}
 
121
 
 
122
 
 
123
/** Adds suggestions to the set of previously selected suggestions.
 
124
 *
 
125
 */
 
126
void Selector::updateSuggestedWords( const std::vector<std::string>& v )
 
127
{
 
128
    std::vector<std::string>::const_iterator i = v.begin();
 
129
    while( i != v.end() ) {
 
130
        logger << DEBUG << "Adding token to suggested token set: " << *i << endl; 
 
131
        suggestedWords.insert( *i );
 
132
        i++;
 
133
    }
 
134
 
 
135
    logger << DEBUG << "Suggested words: ";
 
136
    for (StringSet::const_iterator it = suggestedWords.begin();
 
137
         it != suggestedWords.end();
 
138
         it++) {
 
139
        logger << *it << ' ';
 
140
    }
 
141
    logger << endl;
 
142
}
 
143
 
 
144
 
 
145
/** Clear the set of previously selected suggestions.
 
146
 *
 
147
 */
 
148
void Selector::clearSuggestedWords()
 
149
{
 
150
    logger << DEBUG << "Clearing previously suggested tokens set." << endl;
 
151
    suggestedWords.clear();
 
152
}
 
153
 
 
154
/** Filters out suggestions that have previously been selected in the current context.
 
155
 *
 
156
 * This filter removes the suggestions that have previously been
 
157
 * selected. The set of suggestions that were previously selected is
 
158
 * stored in suggestedWords.  This filters removes the words that are
 
159
 * contained in both @param v and suggestedWords.
 
160
 *
 
161
 */
 
162
void Selector::repetitionFilter( std::vector<std::string>& v )
 
163
{
 
164
    std::vector< std::string > temp;
 
165
 
 
166
    for( std::vector<std::string>::iterator i = v.begin();
 
167
         i != v.end();
 
168
         i++ ) {
 
169
        if( suggestedWords.find( *i ) == suggestedWords.end() ) {
 
170
            temp.push_back( *i );
 
171
            logger << DEBUG << "Token passed repetition filter: " << *i << endl;
 
172
        } else {
 
173
            logger << DEBUG << "Token failed repetition filter: " << *i << endl;
 
174
        }
 
175
    }
 
176
 
 
177
    v = temp;
 
178
}
 
179
 
 
180
/** Filters out suggestions that could save fewer than THRESHOLD keystrokes.
 
181
 *
 
182
 * Assuming prefix.size() == n, suggestion.size() == m, and THRESHOLD
 
183
 * == t, then this filter removes those suggestions for which the
 
184
 * following condition is true: (m - n) < t
 
185
 *
 
186
 */
 
187
void Selector::thresholdFilter( std::vector<std::string>& v )
 
188
{
 
189
    assert( greedy_suggestion_threshold >= 0 );
 
190
 
 
191
    // zero threshold indicates feature is disabled
 
192
    if( greedy_suggestion_threshold != 0 ) {
 
193
                
 
194
        int length = contextTracker->getPrefix().size();
 
195
        std::vector<std::string>::iterator i = v.begin();
 
196
        while (i != v.end()) {
 
197
            if( (i->size()-length) < greedy_suggestion_threshold) {
 
198
                logger << INFO << "Removing token: " << *i << endl;
 
199
                v.erase( i );
 
200
            } else {
 
201
                i++;
 
202
            }
 
203
        }
 
204
    }
 
205
}
 
206
 
 
207
 
 
208
/** Set LOGGER option.
 
209
 *
 
210
 */
 
211
void Selector::set_logger (const std::string& value)
 
212
{
 
213
    logger << setlevel (value);
 
214
    logger << INFO << "LOGGER: " << value << endl;
 
215
}
 
216
 
 
217
 
 
218
/** Set SUGGESTIONS option.
 
219
 *
 
220
 */
 
221
void Selector::set_suggestions(const std::string& value)
 
222
{
 
223
    logger << INFO << "SUGGESTIONS: " << value << endl;
 
224
    int result = Utility::toInt(value);
 
225
    if (result < 0) {
 
226
        logger << ERROR << "Presage.Selector.SUGGESTIONS value out of range!/a" << endl;
 
227
        // REVISIT: throw exception
 
228
        abort();
 
229
    }
 
230
 
 
231
    suggestions = result;
 
232
}
 
233
 
 
234
 
 
235
/** Set REPEAT_SUGGESTION option.
 
236
 *
 
237
 */
 
238
void Selector::set_repeat_suggestions(const std::string& value)
 
239
{
 
240
    logger << INFO << "REPEAT_SUGGESTIONS: " << value << endl;
 
241
    bool result = Utility::isYes(value);
 
242
 
 
243
    repeat_suggestions = result;
 
244
}
 
245
 
 
246
 
 
247
/** Set SUGGESTION_THRESHOLD option.
 
248
 *
 
249
 */
 
250
void Selector::set_greedy_suggestion_threshold(const std::string& value)
 
251
{
 
252
    logger << INFO << "GREEDY_SUGGESTION_THRESHOLD: " << value << endl;
 
253
    int result = Utility::toInt(value);
 
254
    if( result < 0 ) {
 
255
        logger << ERROR << "GREEDY_SUGGESTION_THRESHOLD value out of range." << value << endl;
 
256
        // REVISIT: throw exception
 
257
        abort();
 
258
    }
 
259
 
 
260
    greedy_suggestion_threshold = result;
 
261
}
 
262
 
 
263
size_t Selector::get_suggestions () const
 
264
{
 
265
    return suggestions;
 
266
}
 
267
 
 
268
bool Selector::get_repeat_suggestions () const
 
269
{
 
270
    return repeat_suggestions;
 
271
}
 
272
 
 
273
size_t Selector::get_greedy_suggestion_threshold () const
 
274
{
 
275
    return greedy_suggestion_threshold;
 
276
}
 
277
 
 
278
void Selector::update (const Observable* variable)
 
279
{
 
280
    logger << DEBUG << "update(" << variable->get_name () << ") called" << endl;
 
281
 
 
282
    dispatcher.dispatch (variable);
 
283
}