presage
0.8.8
Main Page
Classes
Files
File List
File Members
src
lib
core
suggestion.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 "
suggestion.h
"
26
27
const
double
Suggestion::MIN_PROBABILITY
= 0.0;
28
const
double
Suggestion::MAX_PROBABILITY
= 1.0;
29
30
Suggestion::Suggestion
(std::string s,
double
p)
31
{
32
setWord
(s);
33
setProbability
(p);
34
}
35
36
Suggestion::~Suggestion
() {}
37
38
39
bool
Suggestion::operator==
(
const
Suggestion
& right)
const
40
{
41
if
(
word
== right.
word
&&
probability
== right.
probability
)
42
return
true
;
43
else
44
return
false
;
45
}
46
47
bool
Suggestion::operator!=
(
const
Suggestion
& right)
const
48
{
49
return
!(*
this
== right);
50
}
51
52
bool
Suggestion::operator<
(
const
Suggestion
& right)
const
53
{
54
if
(
probability
< right.
probability
) {
55
return
true
;
56
}
else
if
(
probability
== right.
probability
) {
57
return
(
word
< right.
word
);
58
}
else
{
59
return
false
;
60
}
61
}
62
63
64
std::string
Suggestion::getWord
()
const
65
{
66
return
word
;
67
}
68
69
double
Suggestion::getProbability
()
const
70
{
71
return
probability
;
72
}
73
74
void
Suggestion::setWord
(std::string s)
75
{
76
word
= s;
77
}
78
79
void
Suggestion::setProbability
(
double
p)
80
{
81
// this should validate that probability is within range [MIN,
82
// MAX], but certain predictors multiply probability by a delta,
83
// hence only checking lower bound
84
if
(p >=
MIN_PROBABILITY
) {
// && p <= MAX_PROBABILITY) {
85
probability
= p;
86
}
else
{
87
std::stringstream ss;
88
ss <<
"Suggestion "
<<
word
<<
" probability value "
89
<< p <<
" out of ["
<<
MIN_PROBABILITY
<<
", "
90
<<
"inf]"
;
// << MAX_PROBABILITY << "] range";
91
throw
SuggestionException
(
PRESAGE_INVALID_SUGGESTION_ERROR
, ss.str());
92
}
93
}
94
95
std::string
Suggestion::toString
()
const
96
{
97
std::stringstream ss;
98
ss <<
"Word: "
<<
word
<<
" Probability: "
<<
probability
<<
std::endl
;
99
return
ss.str();
100
}
101
102
103
std::ostream &
operator<<
( std::ostream &output,
const
Suggestion
&s)
104
{
105
output << s.
word
<<
' '
<< s.
probability
;
106
return
output;
107
}
Generated on Wed Jun 13 2012 00:47:02 for presage by
1.8.1