selector.cpp
Go to the documentation of this file.
/******************************************************
* Presage, an extensible predictive text entry system
* ---------------------------------------------------
*
* Copyright (C) 2008 Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
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 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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
**********(*)*/
#include "selector.h"
#include "utility.h"
const char* Selector::SUGGESTIONS = "Presage.Selector.SUGGESTIONS";
const char* Selector::REPEAT_SUGGESTIONS = "Presage.Selector.REPEAT_SUGGESTIONS";
const char* Selector::GREEDY_SUGGESTION_THRESHOLD = "Presage.Selector.GREEDY_SUGGESTION_THRESHOLD";
const char* Selector::LOGGER = "Presage.Selector.LOGGER";
Selector::Selector(Configuration* configuration, ContextTracker* ct)
: contextTracker(ct),
config(configuration),
logger("Selector", std::cerr),
dispatcher(this)
{
// build notification dispatch map
dispatcher.map (config->find (LOGGER), & Selector::set_logger);
dispatcher.map (config->find (SUGGESTIONS), & Selector::set_suggestions);
dispatcher.map (config->find (REPEAT_SUGGESTIONS), & Selector::set_repeat_suggestions);
dispatcher.map (config->find (GREEDY_SUGGESTION_THRESHOLD), & Selector::set_greedy_suggestion_threshold);
// set prefix
previous_prefix = contextTracker->getPrefix();
}
Selector::~Selector()
{
// nothing to do here, move along
}
std::vector<std::string> Selector::select( Prediction p )
{
// copy words from Prediction.Suggestion.word in result vector
std::vector<std::string> result;
std::string token;
for (size_t i=0 ; i<p.size() ; i++) {
token = p.getSuggestion(i).getWord();
result.push_back(token);
logger << DEBUG << "Added token to selector consideration set: " << token << endl;
}
// check whether user has not moved on to a new word
if (contextTracker->contextChange()) {
logger << DEBUG << "Context change detected." << endl;
clearSuggestedWords();
} else {
logger << DEBUG << "No context change detected." << endl;
}
// filter out suggestions that do not satisfy repetition constraint
if( !repeat_suggestions )
repetitionFilter( result );
// filter out suggestions that do not satisfy threshold constraint
if( greedy_suggestion_threshold > 0 )
thresholdFilter( result );
// if more suggestions than required, trim suggestions down to requested number
if( result.size() > suggestions ) {
result.erase (result.begin() + suggestions, result.end());
}
// update suggested words set
updateSuggestedWords( result );
return result;
}
void Selector::update()
{
// check whether user has not moved on to a new word
if (contextTracker->contextChange()) {
clearSuggestedWords();
}
}
void Selector::updateSuggestedWords( const std::vector<std::string>& v )
{
std::vector<std::string>::const_iterator i = v.begin();
while( i != v.end() ) {
logger << DEBUG << "Adding token to suggested token set: " << *i << endl;
suggestedWords.insert( *i );
i++;
}
logger << DEBUG << "Suggested words: ";
for (StringSet::const_iterator it = suggestedWords.begin();
it != suggestedWords.end();
it++) {
logger << *it << ' ';
}
logger << endl;
}
void Selector::clearSuggestedWords()
{
logger << DEBUG << "Clearing previously suggested tokens set." << endl;
suggestedWords.clear();
}
void Selector::repetitionFilter( std::vector<std::string>& v )
{
std::vector< std::string > temp;
for( std::vector<std::string>::iterator i = v.begin();
i != v.end();
i++ ) {
if( suggestedWords.find( *i ) == suggestedWords.end() ) {
temp.push_back( *i );
logger << DEBUG << "Token passed repetition filter: " << *i << endl;
} else {
logger << DEBUG << "Token failed repetition filter: " << *i << endl;
}
}
v = temp;
}
void Selector::thresholdFilter( std::vector<std::string>& v )
{
assert( greedy_suggestion_threshold >= 0 );
// zero threshold indicates feature is disabled
if( greedy_suggestion_threshold != 0 ) {
int length = contextTracker->getPrefix().size();
std::vector<std::string>::iterator i = v.begin();
while (i != v.end()) {
if( (i->size()-length) < greedy_suggestion_threshold) {
logger << INFO << "Removing token: " << *i << endl;
i = v.erase( i );
} else {
i++;
}
}
}
}
void Selector::set_logger (const std::string& value)
{
logger << setlevel (value);
logger << INFO << "LOGGER: " << value << endl;
}
void Selector::set_suggestions(const std::string& value)
{
logger << INFO << "SUGGESTIONS: " << value << endl;
int result = Utility::toInt(value);
if (result < 0) {
logger << ERROR << "Presage.Selector.SUGGESTIONS value out of range!/a" << endl;
// REVISIT: throw exception
abort();
}
suggestions = result;
}
void Selector::set_repeat_suggestions(const std::string& value)
{
logger << INFO << "REPEAT_SUGGESTIONS: " << value << endl;
bool result = Utility::isYes(value);
repeat_suggestions = result;
}
void Selector::set_greedy_suggestion_threshold(const std::string& value)
{
logger << INFO << "GREEDY_SUGGESTION_THRESHOLD: " << value << endl;
int result = Utility::toInt(value);
if( result < 0 ) {
logger << ERROR << "GREEDY_SUGGESTION_THRESHOLD value out of range." << value << endl;
// REVISIT: throw exception
abort();
}
greedy_suggestion_threshold = result;
}
size_t Selector::get_suggestions () const
{
return suggestions;
}
bool Selector::get_repeat_suggestions () const
{
return repeat_suggestions;
}
size_t Selector::get_greedy_suggestion_threshold () const
{
return greedy_suggestion_threshold;
}
void Selector::update (const Observable* variable)
{
logger << DEBUG << "update(" << variable->get_name () << ") called" << endl;
dispatcher.dispatch (variable);
}
Selector::logger
Logger< char > logger
Definition: selector.h:125
Selector::get_suggestions
size_t get_suggestions() const
Definition: selector.cpp:246
Selector::set_greedy_suggestion_threshold
void set_greedy_suggestion_threshold(const std::string &value)
Definition: selector.cpp:233
Selector::thresholdFilter
void thresholdFilter(std::vector< std::string > &)
Definition: selector.cpp:170
Selector::config
Configuration * config
Definition: selector.h:124
Prediction::getSuggestion
Suggestion getSuggestion(int=0) const
Definition: prediction.cpp:72
Selector::clearSuggestedWords
void clearSuggestedWords()
Definition: selector.cpp:131
Observable::get_name
virtual std::string get_name() const =0
Selector::repeat_suggestions
bool repeat_suggestions
Definition: selector.h:110
Prediction
Definition: prediction.h:46
Dispatcher::map
void map(Observable *var, const mbr_func_ptr_t &ptr)
Definition: dispatcher.h:80
Selector::dispatcher
Dispatcher< Selector > dispatcher
Definition: selector.h:127
utility.h
ContextTracker::getPrefix
std::string getPrefix() const
Definition: contextTracker.cpp:186
ContextTracker::contextChange
bool contextChange()
Definition: contextTracker.cpp:121
Utility::isYes
static bool isYes(const char *)
Definition: utility.cpp:184
Selector::GREEDY_SUGGESTION_THRESHOLD
static const char * GREEDY_SUGGESTION_THRESHOLD
Definition: selector.h:100
Prediction::size
size_t size() const
Definition: prediction.cpp:67
Selector::get_repeat_suggestions
bool get_repeat_suggestions() const
Definition: selector.cpp:251
Selector::Selector
Selector(Configuration *, ContextTracker *)
Definition: selector.cpp:34
ContextTracker
Tracks user interaction and context.
Definition: contextTracker.h:154
config
std::string config
Definition: presageDemo.cpp:69
Selector::REPEAT_SUGGESTIONS
static const char * REPEAT_SUGGESTIONS
Definition: selector.h:99
Selector::get_greedy_suggestion_threshold
size_t get_greedy_suggestion_threshold() const
Definition: selector.cpp:256
Selector::updateSuggestedWords
void updateSuggestedWords(const std::vector< std::string > &)
Definition: selector.cpp:109
Selector::contextTracker
ContextTracker * contextTracker
Definition: selector.h:123
Dispatcher::dispatch
void dispatch(const Observable *var)
Definition: dispatcher.h:91
Utility::toInt
static int toInt(const std::string)
Definition: utility.cpp:265
Selector::set_suggestions
void set_suggestions(const std::string &value)
Definition: selector.cpp:204
Selector::set_logger
void set_logger(const std::string &value)
Definition: selector.cpp:194
Suggestion::getWord
std::string getWord() const
Definition: suggestion.cpp:63
Selector::suggestedWords
StringSet suggestedWords
Definition: selector.h:119
endl
const Logger< _charT, _Traits > & endl(const Logger< _charT, _Traits > &lgr)
Definition: logger.h:277
Selector::LOGGER
static const char * LOGGER
Definition: selector.h:97
Selector::repetitionFilter
void repetitionFilter(std::vector< std::string > &)
Definition: selector.cpp:145
Configuration
Definition: configuration.h:35
Selector::update
void update()
Definition: selector.cpp:97
selector.h
Selector::set_repeat_suggestions
void set_repeat_suggestions(const std::string &value)
Definition: selector.cpp:221
Selector::~Selector
~Selector()
Definition: selector.cpp:50
Observable
Definition: observable.h:36
Selector::select
std::vector< std::string > select(Prediction)
Definition: selector.cpp:55
Configuration::find
Variable * find(const std::string &variable) const
Definition: configuration.cpp:45
setlevel
_SetLevel setlevel(std::string __l)
Manipulator for level.
Definition: logger.h:45
Selector::previous_prefix
std::string previous_prefix
Definition: selector.h:121
Selector::suggestions
size_t suggestions
Definition: selector.h:109
Selector::SUGGESTIONS
static const char * SUGGESTIONS
Definition: selector.h:98
Selector::greedy_suggestion_threshold
size_t greedy_suggestion_threshold
Definition: selector.h:111