presage 0.9.1
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
27const double Suggestion::MIN_PROBABILITY = 0.0;
28const double Suggestion::MAX_PROBABILITY = 1.0;
29
30Suggestion::Suggestion(std::string s, double p)
31{
32 setWord(s);
34}
35
37
38
39bool 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
47bool Suggestion::operator!= (const Suggestion& right) const
48{
49 return !(*this == right);
50}
51
52bool 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
64std::string Suggestion::getWord() const
65{
66 return word;
67}
68
70{
71 return probability;
72}
73
74void Suggestion::setWord(std::string s)
75{
76 word = s;
77}
78
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";
92 }
93}
94
95std::string Suggestion::toString() const
96{
97 std::stringstream ss;
98 ss << "Word: " << word << " Probability: " << probability << std::endl;
99 return ss.str();
100}
101
102
103std::ostream &operator<<( std::ostream &output, const Suggestion &s)
104{
105 output << s.word << ' ' << s.probability;
106 return output;
107}
std::string getWord() const
Definition: suggestion.cpp:64
std::string word
Definition: suggestion.h:82
bool operator<(const Suggestion &) const
Definition: suggestion.cpp:52
bool operator!=(const Suggestion &) const
Definition: suggestion.cpp:47
std::string toString() const
Definition: suggestion.cpp:95
static const double MAX_PROBABILITY
Definition: suggestion.h:69
static const double MIN_PROBABILITY
Definition: suggestion.h:68
void setWord(std::string)
Definition: suggestion.cpp:74
double getProbability() const
Definition: suggestion.cpp:69
Suggestion(std::string="", double=0.0)
Definition: suggestion.cpp:30
void setProbability(double)
Definition: suggestion.cpp:79
bool operator==(const Suggestion &) const
Definition: suggestion.cpp:39
double probability
Definition: suggestion.h:83
const Logger< _charT, _Traits > & endl(const Logger< _charT, _Traits > &lgr)
Definition: logger.h:278
@ PRESAGE_INVALID_SUGGESTION_ERROR
std::ostream & operator<<(std::ostream &output, const Suggestion &s)
Definition: suggestion.cpp:103