presage  0.8.7
suggestion.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************
00003  *  Presage, an extensible predictive text entry system
00004  *  ---------------------------------------------------
00005  *
00006  *  Copyright (C) 2008  Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
00007 
00008     This program is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     This program is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License along
00019     with this program; if not, write to the Free Software Foundation, Inc.,
00020     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00021                                                                              *
00022                                                                 **********(*)*/
00023 
00024 
00025 #include "suggestion.h"
00026 
00027 const double Suggestion::MIN_PROBABILITY = 0.0;
00028 const double Suggestion::MAX_PROBABILITY = 1.0;
00029 
00030 Suggestion::Suggestion(std::string s, double p)
00031 {
00032     setWord(s);
00033     setProbability(p);
00034 }
00035 
00036 Suggestion::~Suggestion() {}
00037 
00038 
00039 bool Suggestion::operator== (const Suggestion& right) const
00040 {
00041     if( word == right.word && probability == right.probability )
00042         return true;
00043     else
00044         return false;
00045 }
00046 
00047 bool Suggestion::operator!= (const Suggestion& right) const
00048 {
00049     return !(*this == right);
00050 }
00051 
00052 bool Suggestion::operator< (const Suggestion& right) const
00053 {
00054     if( probability < right.probability ) {
00055         return true;
00056     } else if( probability == right.probability ) {
00057         return ( word < right.word );
00058     } else {
00059         return false;
00060     }
00061 }
00062 
00063 
00064 std::string Suggestion::getWord() const
00065 {
00066     return word;
00067 }
00068 
00069 double Suggestion::getProbability() const
00070 {
00071     return probability;
00072 }
00073 
00074 void Suggestion::setWord(std::string s)
00075 {
00076     word = s;
00077 }
00078 
00079 void Suggestion::setProbability(double p)
00080 {
00081     // this should validate that probability is within range [MIN,
00082     // MAX], but certain predictors multiply probability by a delta,
00083     // hence only checking lower bound
00084     if (p >= MIN_PROBABILITY) { //  && p <= MAX_PROBABILITY) {
00085         probability = p;
00086     } else {
00087         std::stringstream ss;
00088         ss << "Suggestion " << word << " probability value "
00089            << p << " out of [" << MIN_PROBABILITY << ", "
00090            << "inf]"; // << MAX_PROBABILITY << "] range";
00091         throw SuggestionException(PRESAGE_INVALID_SUGGESTION_ERROR, ss.str());
00092     }
00093 }
00094 
00095 std::string Suggestion::toString() const
00096 {
00097     std::stringstream ss;
00098     ss << "Word: " << word << " Probability: " << probability << std::endl;
00099     return ss.str();
00100 }
00101 
00102 
00103 std::ostream &operator<<( std::ostream &output, const Suggestion &s)
00104 {
00105     output << s.word << ' ' << s.probability;
00106     return output;
00107 }