presage
0.9.1
src
lib
predictors
dictionaryPredictor.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 "
dictionaryPredictor.h
"
26
27
#include <assert.h>
28
29
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
49
DictionaryPredictor::~DictionaryPredictor
()
50
{
51
// intentionally empty
52
}
53
54
void
DictionaryPredictor::set_dictionary
(
const
std::string& value)
55
{
56
dictionary_path
= value;
57
logger
<< INFO <<
"DICTIONARY: "
<< value <<
endl
;
58
}
59
60
61
void
DictionaryPredictor::set_probability
(
const
std::string& value)
62
{
63
probability
=
Utility::toDouble
(value);
64
logger
<< INFO <<
"PROBABILITY: "
<< value <<
endl
;
65
}
66
67
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
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
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
}
Suggestion
Definition:
suggestion.h:50
Observable::get_name
virtual std::string get_name() const =0
Utility::toDouble
static double toDouble(const std::string)
Definition:
utility.cpp:258
DictionaryPredictor::PROBABILITY
std::string PROBABILITY
Definition:
dictionaryPredictor.h:77
Prediction
Definition:
prediction.h:47
Dispatcher::map
void map(Observable *var, const mbr_func_ptr_t &ptr)
Definition:
dispatcher.h:82
Predictor::set_logger
virtual void set_logger(const std::string &level)
Definition:
predictor.cpp:88
ContextTracker::getPrefix
std::string getPrefix() const
Definition:
contextTracker.cpp:186
Predictor::name
const std::string name
Definition:
predictor.h:97
Predictor
Definition:
predictor.h:46
Predictor::contextTracker
ContextTracker * contextTracker
Definition:
predictor.h:103
ContextTracker
Tracks user interaction and context.
Definition:
contextTracker.h:155
DictionaryPredictor::learn
virtual void learn(const std::vector< std::string > &change)
Definition:
dictionaryPredictor.cpp:102
config
std::string config
Definition:
presageDemo.cpp:70
Observable::get_value
virtual std::string get_value() const =0
Prediction::addSuggestion
void addSuggestion(Suggestion)
Definition:
prediction.cpp:90
dictionaryPredictor.h
DictionaryPredictor::DICTIONARY
std::string DICTIONARY
Definition:
dictionaryPredictor.h:76
DictionaryPredictor::update
virtual void update(const Observable *variable)
Definition:
dictionaryPredictor.cpp:108
Dispatcher::dispatch
void dispatch(const Observable *var)
Definition:
dispatcher.h:93
endl
const Logger< _charT, _Traits > & endl(const Logger< _charT, _Traits > &lgr)
Definition:
logger.h:278
DictionaryPredictor::probability
double probability
Definition:
dictionaryPredictor.h:80
DictionaryPredictor::~DictionaryPredictor
~DictionaryPredictor()
Definition:
dictionaryPredictor.cpp:49
DictionaryPredictor::set_dictionary
void set_dictionary(const std::string &value)
Definition:
dictionaryPredictor.cpp:54
DictionaryPredictor::predict
virtual Prediction predict(const size_t size, const char **filter) const
Generate prediction.
Definition:
dictionaryPredictor.cpp:67
Predictor::PREDICTORS
const std::string PREDICTORS
Definition:
predictor.h:101
Configuration
Definition:
configuration.h:36
DictionaryPredictor::DictionaryPredictor
DictionaryPredictor(Configuration *, ContextTracker *, const char *)
Definition:
dictionaryPredictor.cpp:30
Observable
Definition:
observable.h:37
DictionaryPredictor::LOGGER
std::string LOGGER
Definition:
dictionaryPredictor.h:75
Predictor::token_satisfies_filter
virtual bool token_satisfies_filter(const std::string &token, const std::string &prefix, const char **filter) const
Definition:
predictor.cpp:95
Predictor::logger
Logger< char > logger
Definition:
predictor.h:107
DictionaryPredictor::dispatcher
Dispatcher< DictionaryPredictor > dispatcher
Definition:
dictionaryPredictor.h:82
DictionaryPredictor::dictionary_path
std::string dictionary_path
Definition:
dictionaryPredictor.h:79
DictionaryPredictor::set_probability
void set_probability(const std::string &value)
Definition:
dictionaryPredictor.cpp:61
Generated on Wed May 6 2020 21:01:16 for presage by
1.8.17