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 }
virtual void update(const Observable *variable)
std::map< std::string, std::string > cache
AbbreviationExpansionPredictor(Configuration *, ContextTracker *, const char *)
void set_abbreviations(const std::string &filename)
Dispatcher< AbbreviationExpansionPredictor > dispatcher
virtual Prediction predict(const size_t size, const char **filter) const
Generate prediction.
virtual void learn(const std::vector< std::string > &change)
Tracks user interaction and context.
std::string getPrefix() const
void dispatch(const Observable *var)
Definition: dispatcher.h:73
void map(Observable *var, const mbr_func_ptr_t &ptr)
Definition: dispatcher.h:62
virtual std::string get_name() const =0
virtual std::string get_value() const =0
void addSuggestion(Suggestion)
Definition: prediction.cpp:90
ContextTracker * contextTracker
Definition: predictor.h:83
const std::string PREDICTORS
Definition: predictor.h:81
virtual void set_logger(const std::string &level)
Definition: predictor.cpp:88
Logger< char > logger
Definition: predictor.h:87
const std::string name
Definition: predictor.h:77
const Logger< _charT, _Traits > & endl(const Logger< _charT, _Traits > &lgr)
Definition: logger.h:278
std::string config
Definition: presageDemo.cpp:70
std::stringstream buffer
Definition: presageDemo.cpp:71