presage
0.8.7
|
00001 00002 /****************************************************** 00003 * Presage, an extensible predictive text entry system 00004 * --------------------------------------------------- 00005 * 00006 * Copyright (C) 2008 Matteo Vescovi <matteo.vescovi@yahoo.co.uk> 00007 00008 This program is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 2 of the License, or 00011 (at your option) any later version. 00012 00013 This program is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License along 00019 with this program; if not, write to the Free Software Foundation, Inc., 00020 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00021 * 00022 **********(*)*/ 00023 00024 00025 #include "dictionaryPredictor.h" 00026 00027 #include <assert.h> 00028 00029 const char* DictionaryPredictor::LOGGER = "Presage.Predictors.DictionaryPredictor.LOGGER"; 00030 const char* DictionaryPredictor::DICTIONARY = "Presage.Predictors.DictionaryPredictor.DICTIONARY"; 00031 const char* DictionaryPredictor::PROBABILITY = "Presage.Predictors.DictionaryPredictor.PROBABILITY"; 00032 00033 DictionaryPredictor::DictionaryPredictor(Configuration* config, ContextTracker* ht) 00034 : Predictor(config, 00035 ht, 00036 "DictionaryPredictor", 00037 "DictionaryPredictor, dictionary lookup", 00038 "DictionaryPredictor, a dictionary based predictor that generates a prediction by extracting tokens that start with the current prefix from a given dictionary" 00039 ), 00040 dispatcher (this) 00041 { 00042 // build notification dispatch map 00043 dispatcher.map (config->find (LOGGER), & DictionaryPredictor::set_logger); 00044 dispatcher.map (config->find (DICTIONARY), & DictionaryPredictor::set_dictionary); 00045 dispatcher.map (config->find (PROBABILITY), & DictionaryPredictor::set_probability); 00046 } 00047 00048 DictionaryPredictor::~DictionaryPredictor() 00049 { 00050 // intentionally empty 00051 } 00052 00053 void DictionaryPredictor::set_dictionary (const std::string& value) 00054 { 00055 dictionary_path = value; 00056 logger << INFO << "DICTIONARY: " << value << endl; 00057 } 00058 00059 00060 void DictionaryPredictor::set_probability (const std::string& value) 00061 { 00062 probability = Utility::toDouble (value); 00063 logger << INFO << "PROBABILITY: " << value << endl; 00064 } 00065 00066 Prediction DictionaryPredictor::predict(const size_t max_partial_predictions_size, const char** filter) const 00067 { 00068 Prediction result; 00069 00070 std::string candidate; 00071 std::string prefix = contextTracker->getPrefix(); 00072 00073 std::ifstream dictionary_file; 00074 dictionary_file.open(dictionary_path.c_str()); 00075 if(!dictionary_file) 00076 logger << ERROR << "Error opening dictionary: " << dictionary_path << endl; 00077 assert(dictionary_file); // REVISIT: handle with exceptions 00078 00079 // scan file entries until we get enough suggestions 00080 unsigned int count = 0; 00081 while(dictionary_file >> candidate && count < max_partial_predictions_size) { 00082 if(candidate.find(prefix) == 0) { 00083 result.addSuggestion(Suggestion(candidate,probability)); 00084 count++; 00085 logger << NOTICE << "Found valid token: " << candidate << endl; 00086 } else { 00087 logger << INFO << "Discarding invalid token: " << candidate << endl; 00088 } 00089 } 00090 00091 dictionary_file.close(); 00092 00093 return result; 00094 } 00095 00096 void DictionaryPredictor::learn(const std::vector<std::string>& change) 00097 { 00098 std::cout << "DictionaryPredictor::learn() method called" << std::endl; 00099 std::cout << "DictionaryPredictor::learn() method exited" << std::endl; 00100 } 00101 00102 void DictionaryPredictor::update (const Observable* var) 00103 { 00104 logger << DEBUG << "About to invoke dispatcher: " << var->get_name () << " - " << var->get_value() << endl; 00105 dispatcher.dispatch (var); 00106 }