presage  0.9.2~beta
prediction.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 "prediction.h"
26 #include <assert.h>
27 
29 {}
30 
32 {}
33 
35 {
36  if( &right != this ) {
37  suggestions = right.suggestions;
38 
39  //assert( ( suggestions == right.suggestions ) );
40  }
41 
42  return *this;
43 }
44 
45 bool Prediction::operator== (const Prediction& right) const
46 {
47  // same instance is obviously equal to itself
48  if (&right == this) {
49  return true;
50  } else {
51  if (size() != right.size()) {
52  return false;
53  } else {
54  // need to compare each suggestion
55  bool result = true;
56  size_t i = 0;
57  while (i < size() && result) {
58  if (getSuggestion(i) != right.getSuggestion(i)) {
59  result = false;
60  }
61  i++;
62  }
63  return result;
64  }
65  }
66 }
67 
68 size_t Prediction::size() const
69 {
70  return suggestions.size();
71 }
72 
74 {
75  assert( i >= 0 && static_cast<unsigned int>(i) < suggestions.size() );
76 
77  return suggestions[i];
78 }
79 
80 Suggestion Prediction::getSuggestion(std::string token) const
81 {
82  for (size_t i = 0; i < suggestions.size(); i++) {
83  if (suggestions[i].getWord() == token) {
84  return suggestions[i];
85  }
86  }
87  return Suggestion();
88 }
89 
91 {
92  // insert s so that suggestions vector is sorted
93 
94  // handle empty vector first
95  if( suggestions.empty() ) {
96  suggestions.push_back( s );
97  } else {
98  std::vector< Suggestion >::iterator i = suggestions.begin();
99  while( i != suggestions.end() && s < *i ) {
100  ++i;
101  }
102  suggestions.insert( i, s );
103  }
104 }
105 
106 std::string Prediction::toString() const
107 {
108  std::string str;
109  std::vector<Suggestion>::const_iterator i;
110  for( i=suggestions.begin(); i!=suggestions.end(); ++i ) {
111  str += i->toString();
112  }
113  return str;
114 }
115 
116 std::ostream &operator<<( std::ostream &output, const Prediction &p )
117 {
118  std::vector<Suggestion>::const_iterator i;
119  for( i=p.suggestions.begin(); i!=p.suggestions.end(); ++i ) {
120  output << *i << std::endl;
121  }
122 
123  return output;
124 }
std::string toString() const
Definition: prediction.cpp:106
bool operator==(const Prediction &) const
Definition: prediction.cpp:45
std::vector< Suggestion > suggestions
Definition: prediction.h:87
Suggestion getSuggestion(int=0) const
Definition: prediction.cpp:73
size_t size() const
Definition: prediction.cpp:68
const Prediction & operator=(const Prediction &)
Definition: prediction.cpp:34
void addSuggestion(Suggestion)
Definition: prediction.cpp:90
std::ostream & operator<<(std::ostream &output, const Prediction &p)
Definition: prediction.cpp:116
const Logger< _charT, _Traits > & endl(const Logger< _charT, _Traits > &lgr)
Definition: logger.h:278