presage  0.9.1
abbreviationExpansionPredictor.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 
26 
27 #include <fstream>
28 
29 
31  : Predictor(config,
32  ct,
33  name,
34  "AbbreviationExpansionPredictor, maps abbreviations to the corresponding fully expanded token.",
35  "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."
36  ),
37  dispatcher (this)
38 {
39  LOGGER = PREDICTORS + name + ".LOGGER";
40  ABBREVIATIONS = PREDICTORS + name + ".ABBREVIATIONS";
41 
42  // build notification dispatch map
45 }
46 
48 {
49  // complete
50 }
51 
52 
53 void AbbreviationExpansionPredictor::set_abbreviations (const std::string& filename)
54 {
55  abbreviations = filename;
56  logger << INFO << "ABBREVIATIONS: " << abbreviations << endl;
57 
59 }
60 
61 
62 Prediction AbbreviationExpansionPredictor::predict(const size_t max_partial_predictions_size, const char** filter) const
63 {
64  Prediction result;
65 
66  std::string prefix = contextTracker->getPrefix();
67 
68  std::map< std::string, std::string >::const_iterator it = cache.find(prefix);
69 
70  if (it != cache.end()) {
71  // prepend expansion with enough backspaces to erase
72  // abbreviation
73  std::string expansion(prefix.size(), '\b');
74 
75  // concatenate actual expansion
76  expansion += it->second;
77 
78  result.addSuggestion(Suggestion(expansion, 1.0));
79 
80  } else {
81  logger << NOTICE << "Could not find expansion for abbreviation: " << prefix << endl;
82  }
83 
84  return result;
85 }
86 
87 void AbbreviationExpansionPredictor::learn (const std::vector<std::string>& change)
88 {
89  // intentionally empty
90 }
91 
93 {
94  cache.clear();
95 
96  std::ifstream abbr_file(abbreviations.c_str());
97  if (!abbr_file) {
98  logger << ERROR << "Could not open abbreviations file: " << abbreviations << endl;
99  // TODO: throw exception here
100  //
101 
102  } else {
103  logger << INFO << "Caching abbreviations/expansions from file: " << abbreviations << endl;
104 
105  std::string buffer;
106  std::string abbreviation;
107  std::string expansion;
108  std::string::size_type tab_pos;
109  while (getline(abbr_file, buffer)) {
110  tab_pos = buffer.find_first_of('\t');
111  if (tab_pos == std::string::npos) {
112  logger << ERROR << "Error reading abbreviations/expansions from file: " << abbreviations << endl;
113  } else {
114  abbreviation = buffer.substr(0, tab_pos);
115  expansion = buffer.substr(tab_pos + 1, std::string::npos);
116 
117  logger << INFO << "Caching abbreviation: " << abbreviation << " - expansion: " << expansion << endl;
118  cache[abbreviation] = expansion;
119  }
120  }
121 
122  abbr_file.close();
123  }
124 }
125 
127 {
128  logger << DEBUG << "About to invoke dispatcher: " << var->get_name () << " - " << var->get_value() << endl;
129  dispatcher.dispatch (var);
130 }
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