~ubuntu-branches/ubuntu/trusty/presage/trusty-proposed

« back to all changes in this revision

Viewing changes to src/lib/predictors/recencyPredictor.h

  • Committer: Bazaar Package Importer
  • Author(s): Matteo Vescovi
  • Date: 2011-08-06 09:26:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110806092615-0wvhajaht9974ncx
Tags: upstream-0.8.6
Import upstream version 0.8.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#ifndef PRESAGE_RECENCYPREDICTOR
 
26
#define PRESAGE_RECENCYPREDICTOR
 
27
 
 
28
#include "predictor.h"
 
29
#include "../core/logger.h"
 
30
#include "../core/dispatcher.h"
 
31
 
 
32
/** Recency predictor, a recency promotion statistical predictor.
 
33
 *
 
34
 * RecencyPredictor, based on recency promotion principle, generates
 
35
 * predictions by assigning exponentially decaying probability values
 
36
 * to previously encountered tokens. Tokens are assigned a probability
 
37
 * value that decays exponentially with their distance from the
 
38
 * current token, thereby promoting context recency
 
39
 
 
40
 * (from Wikipedia) A quantity is said to be subject to exponential
 
41
 * decay if it decreases at a rate proportional to its
 
42
 * value. Symbolically, this can be expressed as the following
 
43
 * differential equation, where N is the quantity and λ is a positive
 
44
 * number called the decay constant.
 
45
 *
 
46
 *  frac{dN}{dt} = -lambda N.
 
47
 *
 
48
 * The solution to this equation is below:
 
49
 *
 
50
 *  N(t) = N_0 e^{-lambda t}
 
51
 *
 
52
 * Here N(t) is the quantity at time t, and N0 = N(0) is the (initial)
 
53
 * quantity, at time t = 0.
 
54
 *
 
55
 * This is the form of the equation that is most commonly used to
 
56
 * describe exponential decay. The constant of integration N0 denotes
 
57
 * the original quantity at t = 0. (The notation λ for the decay
 
58
 * constant is a remnant of the usual notation for an eigenvalue. In
 
59
 * this case, λ is the eigenvalue of the opposite of the
 
60
 * differentiation operator with N(t) as the corresponding
 
61
 * eigenfunction).
 
62
 *
 
63
 */
 
64
class RecencyPredictor : public Predictor, public Observer {
 
65
public:
 
66
    RecencyPredictor(Configuration*, ContextTracker*);
 
67
    ~RecencyPredictor();
 
68
 
 
69
    virtual Prediction predict(const size_t size, const char** filter) const;
 
70
 
 
71
    virtual void learn(const std::vector<std::string>& change);
 
72
 
 
73
    virtual void update (const Observable* variable);
 
74
 
 
75
private:
 
76
    void set_lambda           (const std::string& value);
 
77
    void set_n_0              (const std::string& value);
 
78
    void set_cutoff_threshold (const std::string& value);
 
79
 
 
80
    static const char* LOGGER;
 
81
    static const char* LAMBDA;
 
82
    static const char* N_0;
 
83
    static const char* CUTOFF_THRESHOLD;
 
84
    
 
85
    double lambda;
 
86
    double n_0;
 
87
    size_t cutoff_threshold;
 
88
 
 
89
    Dispatcher<RecencyPredictor> dispatcher;
 
90
 
 
91
};
 
92
 
 
93
#endif // PRESAGE_RECENCYPREDICTOR