presage  0.8.7
smoothedNgramPredictor.h
Go to the documentation of this file.
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 #ifndef PRESAGE_SMOOTHEDNGRAMPREDICTOR
00026 #define PRESAGE_SMOOTHEDNGRAMPREDICTOR
00027 
00028 #include "predictor.h"
00029 #include "../core/logger.h"
00030 #include "../core/dispatcher.h"
00031 #include "dbconnector/sqliteDatabaseConnector.h"
00032 
00033 #include <assert.h>
00034 
00035 #if defined(HAVE_SQLITE3_H) 
00036 # include <sqlite3.h>
00037 #elif defined(HAVE_SQLITE_H)
00038 # include <sqlite.h>
00039 #else
00040 # error "SQLite is required. Please install SQLite."
00041 #endif
00042 
00043 
00047 class SmoothedNgramPredictor : public Predictor, public Observer {
00048 public:
00049     SmoothedNgramPredictor(Configuration*, ContextTracker*);
00050     ~SmoothedNgramPredictor();
00051 
00052     virtual Prediction predict(const size_t size, const char** filter) const;
00053 
00054     virtual void learn(const std::vector<std::string>& change);
00055 
00056     virtual void update (const Observable* variable);
00057 
00058 private:
00059     static const char* LOGGER;
00060     static const char* DBFILENAME;
00061     static const char* DELTAS;
00062     static const char* LEARN;
00063     static const char* DATABASE_LOGGER;
00064 
00065     unsigned int count(const std::vector<std::string>& tokens, int offset, int ngram_size) const;
00066     void check_learn_consistency(const Ngram& name) const;
00067 
00068     void set_dbfilename (const std::string& filename);
00069     void set_deltas (const std::string& deltas);
00070     void set_database_logger_level (const std::string& value);
00071     void set_learn (const std::string& deltas);
00072 
00073     DatabaseConnector*  db;
00074     std::string         dbfilename;
00075     std::string         dbloglevel;
00076     std::vector<double> deltas;
00077     bool                wanna_learn;
00078 
00079     Dispatcher<SmoothedNgramPredictor> dispatcher;
00080 };
00081 
00082 #endif // PRESAGE_SMOOTHEDNGRAMPREDICTOR