presage  0.8.7
prediction.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 "prediction.h"
00026 #include <assert.h>
00027 
00028 Prediction::Prediction()
00029 {}
00030 
00031 Prediction::~Prediction()
00032 {}
00033 
00034 const Prediction &Prediction::operator=( const Prediction &right )
00035 {
00036     if( &right != this ) {
00037         suggestions = right.suggestions;
00038 
00039         //assert( ( suggestions == right.suggestions ) );
00040     }
00041 
00042     return *this;
00043 }
00044 
00045 bool Prediction::operator== (const Prediction& right) const
00046 {
00047     // same instance is obviously equal to itself
00048     if (&right == this) {
00049         return true;
00050     } else {
00051         if (size() != right.size()) {
00052             return false;
00053         } else {
00054             // need to compare each suggestion
00055             bool result = true;
00056             size_t i = 0;
00057             while (i < size() && result) {
00058                 if (getSuggestion(i) != right.getSuggestion(i)) {
00059                     result = false;
00060                 }
00061                 i++;
00062             }
00063             return result;
00064         }
00065     }
00066 }
00067 
00068 size_t Prediction::size() const
00069 {
00070     return suggestions.size();
00071 }
00072 
00073 Suggestion Prediction::getSuggestion(int i) const
00074 {
00075     assert( i >= 0 && static_cast<unsigned int>(i) < suggestions.size() );
00076 
00077     return suggestions[i];
00078 }
00079 
00080 Suggestion Prediction::getSuggestion(std::string token) const
00081 {
00082     for (size_t i = 0; i < suggestions.size(); i++) {
00083         if (suggestions[i].getWord() == token) {
00084             return suggestions[i];
00085         }
00086     }
00087     return Suggestion();
00088 }
00089 
00090 void Prediction::addSuggestion(Suggestion s)
00091 {
00092     // insert s so that suggestions vector is sorted
00093 
00094     // handle empty vector first
00095     if( suggestions.empty() ) {
00096         suggestions.push_back( s );
00097     } else {
00098         std::vector< Suggestion >::iterator i = suggestions.begin();
00099         while( i != suggestions.end() && s < *i ) {
00100             i++;
00101         }
00102         suggestions.insert( i, s );
00103     }
00104 }
00105 
00106 std::string Prediction::toString() const
00107 {
00108     std::string str;
00109     std::vector<Suggestion>::const_iterator i;
00110     for( i=suggestions.begin(); i!=suggestions.end(); i++ ) {
00111         str += i->toString();
00112     }
00113     return str;
00114 }
00115 
00116 std::ostream &operator<<( std::ostream &output, const Prediction &p )
00117 {
00118     std::vector<Suggestion>::const_iterator i;
00119     for( i=p.suggestions.begin(); i!=p.suggestions.end(); i++ ) {
00120         output << *i << std::endl;
00121     }
00122 
00123     return output;
00124 }