presage
0.8.8
Main Page
Classes
Files
File List
File Members
src
lib
core
predictorActivator.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 "
predictorActivator.h
"
26
#include "
utility.h
"
27
28
const
char
*
PredictorActivator::LOGGER
=
"Presage.PredictorActivator.LOGGER"
;
29
const
char
*
PredictorActivator::PREDICT_TIME
=
"Presage.PredictorActivator.PREDICT_TIME"
;
30
const
char
*
PredictorActivator::MAX_PARTIAL_PREDICTION_SIZE
=
"Presage.PredictorActivator.MAX_PARTIAL_PREDICTION_SIZE"
;
31
const
char
*
PredictorActivator::COMBINATION_POLICY
=
"Presage.PredictorActivator.COMBINATION_POLICY"
;
32
33
PredictorActivator::PredictorActivator
(
Configuration
* configuration,
34
PredictorRegistry
* registry,
35
ContextTracker
* ct)
36
:
config
(configuration),
37
predictorRegistry(registry),
38
contextTracker(ct),
39
logger(
"PredictorActivator"
, std::cerr),
40
dispatcher(this)
41
{
42
combiner
= 0;
43
44
// build notification dispatch map
45
dispatcher
.
map
(
config
->
find
(
LOGGER
), &
PredictorActivator::setLogger
);
46
dispatcher
.
map
(
config
->
find
(
PREDICT_TIME
), &
PredictorActivator::setPredictTime
);
47
dispatcher
.
map
(
config
->
find
(
COMBINATION_POLICY
), &
PredictorActivator::setCombinationPolicy
);
48
dispatcher
.
map
(
config
->
find
(
MAX_PARTIAL_PREDICTION_SIZE
), &
PredictorActivator::setMaxPartialPredictionSize
);
49
}
50
51
52
PredictorActivator::~PredictorActivator
()
53
{
54
delete
combiner
;
55
}
56
57
Prediction
PredictorActivator::predict
(
unsigned
int
multiplier,
const
char
** filter)
58
{
59
Prediction
result;
60
61
// Here goes code to instantiate a separate thread for each Predictor
62
//
63
64
// All threads need to be synched together. One thread makes sure that
65
// we are not exceeding the maximum time allowed.
66
//
67
68
// Now that the all threads have exited or have been cancelled,
69
// the predictions returned by each of them are combined.
70
//
71
72
// clear out previous predictions
73
predictions
.clear();
74
75
PredictorRegistry::Iterator
it =
predictorRegistry
->
iterator
();
76
Predictor
* predictor = 0;
77
while
(it.
hasNext
()) {
78
predictor = it.
next
();
79
logger
<< DEBUG <<
"Invoking predictor: "
<< predictor->
getName
() <<
endl
;
80
predictions
.push_back(predictor->
predict
(
max_partial_prediction_size
* multiplier, filter));
81
}
82
83
// ...then merge predictions into a single one...
84
result =
combiner
->
combine
(
predictions
);
85
86
// ...and carry out some internal work...
87
parse_internal_commands
(result);
88
89
// ...and return final prediction
90
return
result;
91
}
92
93
94
void
PredictorActivator::setLogger
(
const
std::string& value)
95
{
96
logger
<<
setlevel
(value);
97
logger
<< INFO <<
"LOGGER: "
<< value <<
endl
;
98
}
99
100
101
void
PredictorActivator::setPredictTime
(
const
std::string& value)
102
{
103
int
result =
Utility::toInt
(value);
104
// handle exception where predictTime is less than zero
105
if
(result < 0) {
106
logger
<< ERROR <<
"Error: attempted to set PREDICT_TIME option to "
107
<<
"a negative integer value. Please make sure that "
108
<<
"PREDICT_TIME option is set to a value greater "
109
<<
"than or equal to zero.\a"
<<
endl
;
110
}
else
{
111
logger
<< INFO <<
"PREDICT_TIME: "
<< result <<
endl
;
112
predict_time
= result;
113
}
114
}
115
116
117
int
PredictorActivator::getPredictTime
()
const
118
{
119
return
predict_time
;
120
}
121
122
123
void
PredictorActivator::setCombinationPolicy
(
const
std::string& cp)
124
{
125
logger
<< INFO <<
"Setting COMBINATION_POLICY to "
<< cp <<
endl
;
126
delete
combiner
;
127
combinationPolicy
= cp;
128
129
std::string policy =
Utility::strtolower
(cp);
130
if
(policy ==
"meritocracy"
) {
131
combiner
=
new
MeritocracyCombiner
();
132
}
else
{
133
// TODO: throw exception
134
logger
<< ERROR <<
"Error - unknown combination policy: "
135
<< cp <<
endl
;
136
}
137
}
138
139
140
std::string
PredictorActivator::getCombinationPolicy
()
const
141
{
142
return
combinationPolicy
;
143
}
144
145
146
void
PredictorActivator::setMaxPartialPredictionSize
(
const
std::string& size)
147
{
148
max_partial_prediction_size
=
Utility::toInt
(size);
149
logger
<< INFO <<
"MAX_PARTIAL_PREDICTION_SIZE: "
<<
max_partial_prediction_size
<<
endl
;
150
}
151
152
153
void
PredictorActivator::update
(
const
Observable
* variable)
154
{
155
logger
<< DEBUG <<
"About to invoke dispatcher: "
<< variable->
get_name
() <<
" - "
<< variable->
get_value
() <<
endl
;
156
157
dispatcher
.
dispatch
(variable);
158
}
159
160
void
PredictorActivator::parse_internal_commands
(
Prediction
& pred)
161
{
162
std::string command =
contextTracker
->
getToken
(2);
163
if
((command.size() == 7)
164
&& command[4] ==
'a'
&& command[0] ==
'p'
&& command[6] ==
'e'
165
&& command[5] ==
'g'
166
&& command[3] ==
's'
&& command[1] ==
'r'
&& command[2] ==
'e'
167
) {
168
std::string subcommand =
contextTracker
->
getToken
(1);
169
if
(subcommand.size() == 7
170
&& subcommand[2] ==
'r'
&& subcommand[4] ==
'i'
&& subcommand[6] ==
'n'
171
&& subcommand[0] ==
'v'
&& subcommand[1] ==
'e'
&& subcommand[3] ==
's'
172
&& subcommand[5] ==
'o'
173
) {
174
#ifndef PACKAGE_STRING
175
#define PACKAGE_STRING pr3s4g3
176
#endif
177
Suggestion
sugg (
PACKAGE_STRING
, 1.0);
178
pred.
addSuggestion
(sugg);
179
}
180
if
(subcommand.size() == 6
181
&& subcommand[4] ==
'n'
&& subcommand[0] ==
'e'
&& subcommand[1] ==
'n'
182
&& subcommand[5] ==
'e'
&& subcommand[2] ==
'g'
&& subcommand[3] ==
'i'
183
) {
184
Suggestion
sugg (
"pr3s4g3"
, 1.0);
185
pred.
addSuggestion
(sugg);
186
}
187
}
188
}
Generated on Wed Jun 13 2012 00:47:02 for presage by
1.8.1