presage 0.9.2~beta
simulator.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 "simulator.h"
26
27#include <iostream>
28
30 std::stringstream& sstream,
31 const std::string config)
32 : m_sstream(sstream)
33{
34 presagePtr = new Presage(callback, config);
35
36 autoSpace = true;
37
38 ki = 0;
39 ks = 1;
40 kn = 0;
41
42 silent_mode = false;
43}
44
45
47{
48 delete presagePtr;
49}
50
51
52void Simulator::simulate( std::string str )
53{
54
55 // Presage predicts a word even when the prefix
56 // is a null string. This initial call to the predict
57 // method simulates this condition.
58 bool hit = find (presagePtr->predict(), str);
59
60 // If the correct predicted word is returned, then
61 // we've got a hit! If this happens when the prefix is
62 // a null string, kudos to the developer (me)!
63 // Presage predicted the word in one try!
64 // We need to update the Presage object with the whole
65 // string and take the trailing space into account.
66 if (hit) {
67 kn += str.size() + 1;
68 ks++;
69 //presagePtr->update (str + " ");
70 m_sstream << str << ' ';
71 if( !autoSpace ) {
72 ki++;
73 }
74 } else {
75 // If we didn't get it right on the first guess,
76 // let's guess again until we get a hit or we
77 // run out of characters that make up the current
78 // word we're trying to predict.
79 std::string::size_type i = 0;
80
81 while( i < str.size() && !hit ) {
82
83 // predict using new keystroke
84 std::string up;
85 up += str[i];
86 m_sstream << up;
87 hit = find(presagePtr->predict(), str);
88
89 // simulate character keystroke
90 ki++;
91
92 // position iterator on next character
93 i++;
94 }
95
96 // If we got a hit, we've got the correct
97 // predicted word after having used up some
98 // of its characters to feed to Presage.
99 if( hit ) {
100
101// presagePtr->complete(str);
102// presagePtr->update(" ");
103 m_sstream << str.substr(i) << ' '; // THIS SHOULD REALLY BE STRING REMAINDER!!!
104
105 kn += str.size() + 1;
106 ki++;
107
108
109 // To bad we were not able to get the right
110 // prediction... no hit for us.
111 // That means we ran each character of the
112 // string through Presage. We only need
113 // to cater for our beloved trailing
114 // whitespace then.
115 } else {
116
117 // If we're positioned at the end of the
118 // string, we've used all characters up
119 // and got the right prediction right on
120 // our last chance.
121 // Selecting the prediction or entering
122 // space is equivalent (supposing selecting
123 // the prediction counts as one keystroke).
124 // We'll simulate entering a whitespace.
125 if( i == str.size() ) {
126
127// presagePtr->update( " " );
128 m_sstream << ' ';
129 ki++;
130 kn += str.size() + 1;
131
132 } else {
133
134 // Else we got the right prediction
135 // b4 we got to the end of the string
136 // Let's update Presage with
137 // the remainder of the string we
138 // didn't use and take care of our
139 // trailing whitespace.
140// std::string suffix;
141// suffix.insert( suffix.begin(), i, str.end() );
142// presagePtr->update( suffix + " " );
143 m_sstream << str.substr(i) << ' ';
144 if( !autoSpace ) {
145 ki++;
146 }
147 }
148 }
149 }
150}
151
152
153// void Simulator::reset()
154// {
155// delete presagePtr;
156// presagePtr = new Presage;
157//
158// ki = 0;
159// ks = 1;
160// kn = 0;
161// }
162
163
165{
166 std::cout << std::endl
167 << "============================" << std::endl
168 << "Keystroke Savings Rate (KSR)" << std::endl
169 << " ki + ks " << std::endl
170 << "KSR = (1 - ------- ) * 100" << std::endl
171 << " kn " << std::endl
172 << "where: " << std::endl
173 << " ki = actual keystrokes" << std::endl
174 << " ks = keystrokes required to select suggestion" << std::endl
175 << " kn = keystrokes required with no prediction enabled" << std::endl
176 << std::endl
177 << "ki : " << ki << std::endl
178 << "ks : " << ks << std::endl
179 << "kn : " << kn << std::endl
180 << std::endl
181 << "KSR: " << getKSR() << std::endl;
182}
183
184
186{
187 return ki;
188}
189
190
192{
193 return ks;
194}
195
196
198{
199 return kn;
200}
201
202
203double Simulator::getKSR() const
204{
205 return ( ( 1 - ( static_cast<double>( ki + ks ) / static_cast<double>( kn ) ) ) * 100 );
206}
207
208
209void Simulator::setKs( int value )
210{
211 if( value > 0 )
212 ks = value;
213}
214
215
216bool Simulator::find( const std::vector<std::string>& w, const std::string& t ) const
217{
218 if (!silent_mode) {
219 std::cout << "===> " << t << std::endl
220 << " > " << presagePtr->prefix() << std::endl;
221 }
222 bool found = false;
223 std::vector<std::string>::const_iterator i = w.begin();
224 while( i != w.end() && !found ) {
225 if (!silent_mode) {
226 std::cout << *i << std::endl;
227 }
228 if( *i == t )
229 found = true;
230 i++;
231 }
232
233 return found;
234}
235
237{
238 return autoSpace;
239}
240
242{
243 autoSpace = b;
244}
245
247{
248 silent_mode = mode;
249}
Presage, the intelligent predictive text entry platform.
Definition: presage.h:107
std::string prefix() const
Returns the current prefix.
Definition: presage.cpp:212
std::vector< std::string > predict()
Obtain a prediction.
Definition: presage.cpp:64
std::stringstream & m_sstream
Definition: simulator.h:87
double getKSR() const
Definition: simulator.cpp:203
void silentMode(bool)
Definition: simulator.cpp:246
void setAutoSpace(bool)
Definition: simulator.cpp:241
void simulate(std::string)
Definition: simulator.cpp:52
bool silent_mode
Definition: simulator.h:85
void results() const
Definition: simulator.cpp:164
Simulator(PresageCallback *callback, std::stringstream &sstream, const std::string="")
Definition: simulator.cpp:29
int ki
Definition: simulator.h:81
int getKs() const
Definition: simulator.cpp:191
bool getAutoSpace() const
Definition: simulator.cpp:236
int kn
Definition: simulator.h:83
bool find(const std::vector< std::string > &, const std::string &) const
Definition: simulator.cpp:216
void setKs(int)
Definition: simulator.cpp:209
int getKn() const
Definition: simulator.cpp:197
bool autoSpace
Definition: simulator.h:79
Presage * presagePtr
Definition: simulator.h:75
int ks
Definition: simulator.h:82
int getKi() const
Definition: simulator.cpp:185
const Logger< _charT, _Traits > & endl(const Logger< _charT, _Traits > &lgr)
Definition: logger.h:278
std::string config
Definition: presageDemo.cpp:70