abbreviationExpansionPredictor.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 "abbreviationExpansionPredictor.h"
#include <fstream>
AbbreviationExpansionPredictor::AbbreviationExpansionPredictor(Configuration* config, ContextTracker* ct, const char* name)
: Predictor(config,
ct,
name,
"AbbreviationExpansionPredictor, maps abbreviations to the corresponding fully expanded token.",
"AbbreviationExpansionPredictor maps abbreviations to the corresponding fully expanded token (i.e. word or phrase).\n\nThe mapping between abbreviations and expansions is stored in the file specified by the predictor configuration section.\n\nThe format for the abbreviation-expansion database is a simple tab separated text file format, with each abbreviation-expansion pair per line."
),
dispatcher (this)
{
LOGGER = PREDICTORS + name + ".LOGGER";
ABBREVIATIONS = PREDICTORS + name + ".ABBREVIATIONS";
// build notification dispatch map
dispatcher.map (config->find (LOGGER), & AbbreviationExpansionPredictor::set_logger);
dispatcher.map (config->find (ABBREVIATIONS), & AbbreviationExpansionPredictor::set_abbreviations);
}
AbbreviationExpansionPredictor::~AbbreviationExpansionPredictor()
{
// complete
}
void AbbreviationExpansionPredictor::set_abbreviations (const std::string& filename)
{
abbreviations = filename;
logger << INFO << "ABBREVIATIONS: " << abbreviations << endl;
cacheAbbreviationsExpansions();
}
Prediction AbbreviationExpansionPredictor::predict(const size_t max_partial_predictions_size, const char** filter) const
{
Prediction result;
std::string prefix = contextTracker->getPrefix();
std::map< std::string, std::string >::const_iterator it = cache.find(prefix);
if (it != cache.end()) {
// prepend expansion with enough backspaces to erase
// abbreviation
std::string expansion(prefix.size(), '\b');
// concatenate actual expansion
expansion += it->second;
result.addSuggestion(Suggestion(expansion, 1.0));
} else {
logger << NOTICE << "Could not find expansion for abbreviation: " << prefix << endl;
}
return result;
}
void AbbreviationExpansionPredictor::learn (const std::vector<std::string>& change)
{
// intentionally empty
}
void AbbreviationExpansionPredictor::cacheAbbreviationsExpansions()
{
cache.clear();
std::ifstream abbr_file(abbreviations.c_str());
if (!abbr_file) {
logger << ERROR << "Could not open abbreviations file: " << abbreviations << endl;
// TODO: throw exception here
//
} else {
logger << INFO << "Caching abbreviations/expansions from file: " << abbreviations << endl;
std::string buffer;
std::string abbreviation;
std::string expansion;
std::string::size_type tab_pos;
while (getline(abbr_file, buffer)) {
tab_pos = buffer.find_first_of('\t');
if (tab_pos == std::string::npos) {
logger << ERROR << "Error reading abbreviations/expansions from file: " << abbreviations << endl;
} else {
abbreviation = buffer.substr(0, tab_pos);
expansion = buffer.substr(tab_pos + 1, std::string::npos);
logger << INFO << "Caching abbreviation: " << abbreviation << " - expansion: " << expansion << endl;
cache[abbreviation] = expansion;
}
}
abbr_file.close();
}
}
void AbbreviationExpansionPredictor::update (const Observable* var)
{
logger << DEBUG << "About to invoke dispatcher: " << var->get_name () << " - " << var->get_value() << endl;
dispatcher.dispatch (var);
}
Suggestion
Definition: suggestion.h:49
AbbreviationExpansionPredictor::update
virtual void update(const Observable *variable)
Definition: abbreviationExpansionPredictor.cpp:126
AbbreviationExpansionPredictor::cacheAbbreviationsExpansions
void cacheAbbreviationsExpansions()
Definition: abbreviationExpansionPredictor.cpp:92
buffer
std::stringstream buffer
Definition: presageDemo.cpp:70
abbreviationExpansionPredictor.h
Observable::get_name
virtual std::string get_name() const =0
Prediction
Definition: prediction.h:46
Dispatcher::map
void map(Observable *var, const mbr_func_ptr_t &ptr)
Definition: dispatcher.h:80
Predictor::set_logger
virtual void set_logger(const std::string &level)
Definition: predictor.cpp:87
ContextTracker::getPrefix
std::string getPrefix() const
Definition: contextTracker.cpp:186
Predictor::name
const std::string name
Definition: predictor.h:95
Predictor
Definition: predictor.h:45
AbbreviationExpansionPredictor::set_abbreviations
void set_abbreviations(const std::string &filename)
Definition: abbreviationExpansionPredictor.cpp:53
Predictor::contextTracker
ContextTracker * contextTracker
Definition: predictor.h:101
AbbreviationExpansionPredictor::LOGGER
std::string LOGGER
Definition: abbreviationExpansionPredictor.h:75
AbbreviationExpansionPredictor::predict
virtual Prediction predict(const size_t size, const char **filter) const
Generate prediction.
Definition: abbreviationExpansionPredictor.cpp:62
AbbreviationExpansionPredictor::learn
virtual void learn(const std::vector< std::string > &change)
Definition: abbreviationExpansionPredictor.cpp:87
ContextTracker
Tracks user interaction and context.
Definition: contextTracker.h:154
config
std::string config
Definition: presageDemo.cpp:69
Observable::get_value
virtual std::string get_value() const =0
Prediction::addSuggestion
void addSuggestion(Suggestion)
Definition: prediction.cpp:89
AbbreviationExpansionPredictor::dispatcher
Dispatcher< AbbreviationExpansionPredictor > dispatcher
Definition: abbreviationExpansionPredictor.h:84
AbbreviationExpansionPredictor::AbbreviationExpansionPredictor
AbbreviationExpansionPredictor(Configuration *, ContextTracker *, const char *)
Definition: abbreviationExpansionPredictor.cpp:30
Dispatcher::dispatch
void dispatch(const Observable *var)
Definition: dispatcher.h:91
AbbreviationExpansionPredictor::cache
std::map< std::string, std::string > cache
Definition: abbreviationExpansionPredictor.h:82
endl
const Logger< _charT, _Traits > & endl(const Logger< _charT, _Traits > &lgr)
Definition: logger.h:277
Predictor::PREDICTORS
const std::string PREDICTORS
Definition: predictor.h:99
Configuration
Definition: configuration.h:35
AbbreviationExpansionPredictor::abbreviations
std::string abbreviations
Definition: abbreviationExpansionPredictor.h:81
Observable
Definition: observable.h:36
Predictor::logger
Logger< char > logger
Definition: predictor.h:105
AbbreviationExpansionPredictor::~AbbreviationExpansionPredictor
~AbbreviationExpansionPredictor()
Definition: abbreviationExpansionPredictor.cpp:47
AbbreviationExpansionPredictor::ABBREVIATIONS
std::string ABBREVIATIONS
Definition: abbreviationExpansionPredictor.h:76