presage  0.9.2~beta
dictionaryPredictor.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 
25 #include "dictionaryPredictor.h"
26 
27 #include <assert.h>
28 
29 
31  : Predictor(config,
32  ht,
33  name,
34  "DictionaryPredictor, dictionary lookup",
35  "DictionaryPredictor, a dictionary based predictor that generates a prediction by extracting tokens that start with the current prefix from a given dictionary"
36  ),
37  dispatcher (this)
38 {
39  LOGGER = PREDICTORS + name + ".LOGGER";
40  DICTIONARY = PREDICTORS + name + ".DICTIONARY";
41  PROBABILITY = PREDICTORS + name + ".PROBABILITY";
42 
43  // build notification dispatch map
47 }
48 
50 {
51  // intentionally empty
52 }
53 
54 void DictionaryPredictor::set_dictionary (const std::string& value)
55 {
56  dictionary_path = value;
57  logger << INFO << "DICTIONARY: " << value << endl;
58 }
59 
60 
61 void DictionaryPredictor::set_probability (const std::string& value)
62 {
64  logger << INFO << "PROBABILITY: " << value << endl;
65 }
66 
67 Prediction DictionaryPredictor::predict(const size_t max_partial_predictions_size, const char** filter) const
68 {
69  Prediction result;
70 
71  std::string candidate;
72  std::string prefix = contextTracker->getPrefix();
73 
74  std::ifstream dictionary_file;
75  dictionary_file.open(dictionary_path.c_str());
76  if(!dictionary_file)
77  logger << ERROR << "Error opening dictionary: " << dictionary_path << endl;
78  assert(dictionary_file); // REVISIT: handle with exceptions
79 
80  // scan file entries until we get enough suggestions
81  unsigned int count = 0;
82  while(dictionary_file >> candidate && count < max_partial_predictions_size) {
83  if(candidate.find(prefix) == 0) { // candidate starts with prefix
84  logger << NOTICE << "Found valid token: " << candidate << endl;
85  if (token_satisfies_filter (candidate, prefix, filter)) {
86  logger << NOTICE << "Filter check satisfied by token: " << candidate << endl;
87  result.addSuggestion(Suggestion(candidate,probability));
88  count++;
89  } else {
90  logger << NOTICE << "Filter check failed, discarding token: " << candidate << endl;
91  }
92  } else {
93  logger << INFO << "Discarding invalid token: " << candidate << endl;
94  }
95  }
96 
97  dictionary_file.close();
98 
99  return result;
100 }
101 
102 void DictionaryPredictor::learn(const std::vector<std::string>& change)
103 {
104  std::cout << "DictionaryPredictor::learn() method called" << std::endl;
105  std::cout << "DictionaryPredictor::learn() method exited" << std::endl;
106 }
107 
109 {
110  logger << DEBUG << "About to invoke dispatcher: " << var->get_name () << " - " << var->get_value() << endl;
111  dispatcher.dispatch (var);
112 }
Logger< char > logger
Definition: predictor.h:87
DictionaryPredictor(Configuration *, ContextTracker *, const char *)
virtual void learn(const std::vector< std::string > &change)
void dispatch(const Observable *var)
Definition: dispatcher.h:73
virtual Prediction predict(const size_t size, const char **filter) const
Generate prediction.
virtual void set_logger(const std::string &level)
Definition: predictor.cpp:88
std::string config
Definition: presageDemo.cpp:70
const std::string PREDICTORS
Definition: predictor.h:81
const std::string name
Definition: predictor.h:77
void set_dictionary(const std::string &value)
static double toDouble(const std::string)
Definition: utility.cpp:258
virtual void update(const Observable *variable)
virtual std::string get_name() const =0
void map(Observable *var, const mbr_func_ptr_t &ptr)
Definition: dispatcher.h:62
ContextTracker * contextTracker
Definition: predictor.h:83
void set_probability(const std::string &value)
void addSuggestion(Suggestion)
Definition: prediction.cpp:90
Tracks user interaction and context.
virtual std::string get_value() const =0
std::string getPrefix() const
Dispatcher< DictionaryPredictor > dispatcher
virtual bool token_satisfies_filter(const std::string &token, const std::string &prefix, const char **filter) const
Definition: predictor.cpp:95
const Logger< _charT, _Traits > & endl(const Logger< _charT, _Traits > &lgr)
Definition: logger.h:278