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
DictionaryPredictor(Configuration * config,ContextTracker * ht,const char * name)30 DictionaryPredictor::DictionaryPredictor (Configuration* config, ContextTracker* ht, const char* name)
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
44 dispatcher.map (config->find (LOGGER), & DictionaryPredictor::set_logger);
45 dispatcher.map (config->find (DICTIONARY), & DictionaryPredictor::set_dictionary);
46 dispatcher.map (config->find (PROBABILITY), & DictionaryPredictor::set_probability);
47 }
48
~DictionaryPredictor()49 DictionaryPredictor::~DictionaryPredictor()
50 {
51 // intentionally empty
52 }
53
set_dictionary(const std::string & value)54 void DictionaryPredictor::set_dictionary (const std::string& value)
55 {
56 dictionary_path = value;
57 logger << INFO << "DICTIONARY: " << value << endl;
58 }
59
60
set_probability(const std::string & value)61 void DictionaryPredictor::set_probability (const std::string& value)
62 {
63 probability = Utility::toDouble (value);
64 logger << INFO << "PROBABILITY: " << value << endl;
65 }
66
predict(const size_t max_partial_predictions_size,const char ** filter) const67 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
learn(const std::vector<std::string> & change)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
update(const Observable * var)108 void DictionaryPredictor::update (const Observable* var)
109 {
110 logger << DEBUG << "About to invoke dispatcher: " << var->get_name () << " - " << var->get_value() << endl;
111 dispatcher.dispatch (var);
112 }
113