presage  0.9.2~beta
selector.cpp
Go to the documentation of this file.
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 
35  : contextTracker(ct),
36  config(configuration),
37  logger("Selector", std::cerr),
38  dispatcher(this)
39 {
40  // build notification dispatch map
45 
46  // set prefix
48 }
49 
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
68  logger << DEBUG << "Context change detected." << endl;
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
80  thresholdFilter( result );
81 
82  // if more suggestions than required, trim suggestions down to requested number
83  if( result.size() > suggestions ) {
84  result.erase (result.begin() + suggestions, result.end());
85  }
86 
87  // update suggested words set
88  updateSuggestedWords( result );
89 
90  return result;
91 }
92 
93 
98 {
99  // check whether user has not moved on to a new word
100  if (contextTracker->contextChange()) {
102  }
103 }
104 
105 
109 void Selector::updateSuggestedWords( const std::vector<std::string>& v )
110 {
111  std::vector<std::string>::const_iterator i = v.begin();
112  while( i != v.end() ) {
113  logger << DEBUG << "Adding token to suggested token set: " << *i << endl;
114  suggestedWords.insert( *i );
115  i++;
116  }
117 
118  logger << DEBUG << "Suggested words: ";
119  for (StringSet::const_iterator it = suggestedWords.begin();
120  it != suggestedWords.end();
121  it++) {
122  logger << *it << ' ';
123  }
124  logger << endl;
125 }
126 
127 
132 {
133  logger << DEBUG << "Clearing previously suggested tokens set." << endl;
134  suggestedWords.clear();
135 }
136 
145 void Selector::repetitionFilter( std::vector<std::string>& v )
146 {
147  std::vector< std::string > temp;
148 
149  for( std::vector<std::string>::iterator i = v.begin();
150  i != v.end();
151  i++ ) {
152  if( suggestedWords.find( *i ) == suggestedWords.end() ) {
153  temp.push_back( *i );
154  logger << DEBUG << "Token passed repetition filter: " << *i << endl;
155  } else {
156  logger << DEBUG << "Token failed repetition filter: " << *i << endl;
157  }
158  }
159 
160  v = temp;
161 }
162 
170 void Selector::thresholdFilter( std::vector<std::string>& v )
171 {
172  assert( greedy_suggestion_threshold >= 0 );
173 
174  // zero threshold indicates feature is disabled
175  if( greedy_suggestion_threshold != 0 ) {
176 
177  int length = contextTracker->getPrefix().size();
178  std::vector<std::string>::iterator i = v.begin();
179  while (i != v.end()) {
180  if( (i->size()-length) < greedy_suggestion_threshold) {
181  logger << INFO << "Removing token: " << *i << endl;
182  i = v.erase( i );
183  } else {
184  i++;
185  }
186  }
187  }
188 }
189 
190 
194 void Selector::set_logger (const std::string& value)
195 {
196  logger << setlevel (value);
197  logger << INFO << "LOGGER: " << value << endl;
198 }
199 
200 
204 void Selector::set_suggestions(const std::string& value)
205 {
206  logger << INFO << "SUGGESTIONS: " << value << endl;
207  int result = Utility::toInt(value);
208  if (result < 0) {
209  logger << ERROR << "Presage.Selector.SUGGESTIONS value out of range!/a" << endl;
210  // REVISIT: throw exception
211  abort();
212  }
213 
214  suggestions = result;
215 }
216 
217 
221 void Selector::set_repeat_suggestions(const std::string& value)
222 {
223  logger << INFO << "REPEAT_SUGGESTIONS: " << value << endl;
224  bool result = Utility::isYes(value);
225 
226  repeat_suggestions = result;
227 }
228 
229 
233 void Selector::set_greedy_suggestion_threshold(const std::string& value)
234 {
235  logger << INFO << "GREEDY_SUGGESTION_THRESHOLD: " << value << endl;
236  int result = Utility::toInt(value);
237  if( result < 0 ) {
238  logger << ERROR << "GREEDY_SUGGESTION_THRESHOLD value out of range." << value << endl;
239  // REVISIT: throw exception
240  abort();
241  }
242 
244 }
245 
247 {
248  return suggestions;
249 }
250 
252 {
253  return repeat_suggestions;
254 }
255 
257 {
259 }
260 
261 void Selector::update (const Observable* variable)
262 {
263  logger << DEBUG << "update(" << variable->get_name () << ") called" << endl;
264 
265  dispatcher.dispatch (variable);
266 }
bool get_repeat_suggestions() const
Definition: selector.cpp:251
std::string getWord() const
Definition: suggestion.cpp:64
static int toInt(const std::string)
Definition: utility.cpp:266
void set_greedy_suggestion_threshold(const std::string &value)
Definition: selector.cpp:233
void dispatch(const Observable *var)
Definition: dispatcher.h:73
size_t greedy_suggestion_threshold
Definition: selector.h:112
Dispatcher< Selector > dispatcher
Definition: selector.h:128
static const char * GREEDY_SUGGESTION_THRESHOLD
Definition: selector.h:101
Configuration * config
Definition: selector.h:125
Selector(Configuration *, ContextTracker *)
Definition: selector.cpp:34
void clearSuggestedWords()
Definition: selector.cpp:131
void thresholdFilter(std::vector< std::string > &)
Definition: selector.cpp:170
bool repeat_suggestions
Definition: selector.h:111
void set_logger(const std::string &value)
Definition: selector.cpp:194
_SetLevel setlevel(std::string __l)
Manipulator for level.
Definition: logger.h:46
StringSet suggestedWords
Definition: selector.h:120
Variable * find(const std::string &variable) const
std::string config
Definition: presageDemo.cpp:70
size_t get_greedy_suggestion_threshold() const
Definition: selector.cpp:256
Suggestion getSuggestion(int=0) const
Definition: prediction.cpp:73
std::string previous_prefix
Definition: selector.h:122
void repetitionFilter(std::vector< std::string > &)
Definition: selector.cpp:145
static const char * REPEAT_SUGGESTIONS
Definition: selector.h:100
static const char * SUGGESTIONS
Definition: selector.h:99
virtual std::string get_name() const =0
size_t size() const
Definition: prediction.cpp:68
void updateSuggestedWords(const std::vector< std::string > &)
Definition: selector.cpp:109
ContextTracker * contextTracker
Definition: selector.h:124
void map(Observable *var, const mbr_func_ptr_t &ptr)
Definition: dispatcher.h:62
void set_suggestions(const std::string &value)
Definition: selector.cpp:204
static const char * LOGGER
Definition: selector.h:98
~Selector()
Definition: selector.cpp:50
size_t suggestions
Definition: selector.h:110
static bool isYes(const char *)
Definition: utility.cpp:185
std::vector< std::string > select(Prediction)
Definition: selector.cpp:55
Tracks user interaction and context.
Logger< char > logger
Definition: selector.h:126
size_t get_suggestions() const
Definition: selector.cpp:246
void set_repeat_suggestions(const std::string &value)
Definition: selector.cpp:221
std::string getPrefix() const
void update()
Definition: selector.cpp:97
const Logger< _charT, _Traits > & endl(const Logger< _charT, _Traits > &lgr)
Definition: logger.h:278