recencyPredictor.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 "recencyPredictor.h"
#include <math.h> // for exp()
RecencyPredictor::RecencyPredictor(Configuration* config, ContextTracker* ct, const char* name)
: Predictor(config,
ct,
name,
"RecencyPredictor, a statistical recency promotion predictor",
"RecencyPredictor, based on a recency promotion principle, generates predictions by assigning exponentially decaying probability values to previously encountered tokens. Tokens are assigned a probability value that decays exponentially with their distance from the current token, thereby promoting context recency." ),
dispatcher (this)
{
// RecencyPredictor config variables
LOGGER = PREDICTORS + name + ".LOGGER";
LAMBDA = PREDICTORS + name + ".LAMBDA";
N_0 = PREDICTORS + name + ".N_0";
CUTOFF_THRESHOLD = PREDICTORS + name + ".CUTOFF_THRESHOLD";
// init default values
lambda = 1;
n_0 = 1;
cutoff_threshold = 20;
dispatcher.map(config->find (LOGGER), &RecencyPredictor::set_logger);
dispatcher.map(config->find (LAMBDA), &RecencyPredictor::set_lambda);
dispatcher.map(config->find (N_0), &RecencyPredictor::set_n_0);
dispatcher.map(config->find (CUTOFF_THRESHOLD), &RecencyPredictor::set_cutoff_threshold);
}
RecencyPredictor::~RecencyPredictor()
{
// complete
}
void RecencyPredictor::set_lambda (const std::string& value)
{
lambda = Utility::toDouble(value);
logger << INFO << "LAMBDA: " << value << endl;
}
void RecencyPredictor::set_n_0 (const std::string& value)
{
n_0 = Utility::toDouble (value);
logger << INFO << "N_0: " << value << endl;
}
void RecencyPredictor::set_cutoff_threshold (const std::string& value)
{
cutoff_threshold = Utility::toInt (value);
logger << INFO << "CUTOFF_THRESHOLD: " << value << endl;
}
Prediction RecencyPredictor::predict (const size_t max, const char** filter) const
{
Prediction result;
std::string prefix = contextTracker->getPrefix();
logger << INFO << "prefix: " << prefix << endl;
if (!prefix.empty()) {
// Only build recency prediction if prefix is not empty: when
// prefix is empty, all previosly seen tokens are candidates
// for prediction. This is not desirable, because it means
// that recency prediction reduces to repetion of max previous
// tokens (i.e. the prediction would contain the most recent
// tokens in reverse order).
//
Suggestion suggestion;
size_t index = 1;
std::string token = contextTracker->getToken(index);
double prob = 0;
while (!token.empty() // context history exhausted
&& result.size() < max // need only max suggestions
&& index <= cutoff_threshold // look back only as far as cutoff
) {
logger << INFO << "token: " << token << endl;
if (token.find(prefix) == 0) { // if token starts with prefix
if (token_satisfies_filter (token, prefix, filter)) {
// compute probability according to exponential decay
// formula
//
prob = n_0 * exp(-(lambda * (index - 1)));
logger << INFO << "probability: " << prob << endl;
suggestion.setWord(token);
suggestion.setProbability(prob);
result.addSuggestion(suggestion);
}
}
index++;
token = contextTracker->getToken(index);
}
}
return result;
}
void RecencyPredictor::learn(const std::vector<std::string>& change)
{}
void RecencyPredictor::update (const Observable* var)
{
logger << DEBUG << "About to invoke dispatcher: " << var->get_name () << " - " << var->get_value() << endl;
dispatcher.dispatch (var);
}
Configuration
Definition configuration.h:36
ContextTracker
Tracks user interaction and context.
Definition contextTracker.h:155
Observable
Definition observable.h:37
Observable::get_name
virtual std::string get_name() const =0
Observable::get_value
virtual std::string get_value() const =0
Prediction
Definition prediction.h:47
Prediction::size
size_t size() const
Definition prediction.cpp:68
Prediction::addSuggestion
void addSuggestion(Suggestion)
Definition prediction.cpp:90
Predictor::contextTracker
ContextTracker * contextTracker
Definition predictor.h:83
Predictor::PREDICTORS
const std::string PREDICTORS
Definition predictor.h:81
Predictor::set_logger
virtual void set_logger(const std::string &level)
Definition predictor.cpp:88
Predictor::Predictor
Predictor(Configuration *configuration, ContextTracker *contextTracker, const char *predictorName="Predictor", const char *shortDescription="", const char *longDescription="")
Definition predictor.cpp:39
Predictor::logger
Logger< char > logger
Definition predictor.h:87
Predictor::name
const std::string name
Definition predictor.h:77
Predictor::token_satisfies_filter
virtual bool token_satisfies_filter(const std::string &token, const std::string &prefix, const char **filter) const
Definition predictor.cpp:95
RecencyPredictor::learn
virtual void learn(const std::vector< std::string > &change)
Definition recencyPredictor.cpp:126
RecencyPredictor::n_0
double n_0
Definition recencyPredictor.h:86
RecencyPredictor::N_0
std::string N_0
Definition recencyPredictor.h:82
RecencyPredictor::CUTOFF_THRESHOLD
std::string CUTOFF_THRESHOLD
Definition recencyPredictor.h:83
RecencyPredictor::dispatcher
Dispatcher< RecencyPredictor > dispatcher
Definition recencyPredictor.h:89
RecencyPredictor::LOGGER
std::string LOGGER
Definition recencyPredictor.h:80
RecencyPredictor::predict
virtual Prediction predict(const size_t size, const char **filter) const
Generate prediction.
Definition recencyPredictor.cpp:79
RecencyPredictor::~RecencyPredictor
~RecencyPredictor()
Definition recencyPredictor.cpp:54
RecencyPredictor::set_n_0
void set_n_0(const std::string &value)
Definition recencyPredictor.cpp:65
RecencyPredictor::cutoff_threshold
size_t cutoff_threshold
Definition recencyPredictor.h:87
RecencyPredictor::LAMBDA
std::string LAMBDA
Definition recencyPredictor.h:81
RecencyPredictor::update
virtual void update(const Observable *variable)
Definition recencyPredictor.cpp:129
RecencyPredictor::set_lambda
void set_lambda(const std::string &value)
Definition recencyPredictor.cpp:59
RecencyPredictor::lambda
double lambda
Definition recencyPredictor.h:85
RecencyPredictor::RecencyPredictor
RecencyPredictor(Configuration *, ContextTracker *, const char *)
Definition recencyPredictor.cpp:29
RecencyPredictor::set_cutoff_threshold
void set_cutoff_threshold(const std::string &value)
Definition recencyPredictor.cpp:72
Suggestion
Definition suggestion.h:50
Suggestion::setWord
void setWord(std::string)
Definition suggestion.cpp:74
Suggestion::setProbability
void setProbability(double)
Definition suggestion.cpp:79
Utility::toDouble
static double toDouble(const std::string)
Definition utility.cpp:258
Utility::toInt
static int toInt(const std::string)
Definition utility.cpp:266
endl
const Logger< _charT, _Traits > & endl(const Logger< _charT, _Traits > &lgr)
Definition logger.h:278
config
std::string config
Definition presageDemo.cpp:70
recencyPredictor.h