presage  0.9.2~beta
combiner.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 "combiner.h"
26 #include "profile.h"
27 
28 #include <set>
29 
31 {
32  // intentionally empty
33 }
34 
36 {
37  // intentionally empty
38 }
39 
55 Prediction Combiner::filter(const Prediction& prediction) const
56 {
57  Prediction result;
58 
59  std::set<std::string> seen_tokens;
60 
61  size_t size = prediction.size();
62  Suggestion suggestion;
63  std::string token;
64  for (size_t i = 0; i < size; i++)
65  {
66  suggestion = prediction.getSuggestion(i);
67  token = suggestion.getWord();
68  //std::cerr << "[filter] token: " << token << std::endl;
69  if (seen_tokens.find(token) == seen_tokens.end())
70  {
71  // if token has not been seen before, then look for
72  // potential duplicates and incrementally add the
73  // interpolated combined probability and remember that
74  // this token has now been processed
75  //
76 
77  //std::cerr << "[filter] searching for possible duplicates" << std::endl;
78  for (int j = i + 1; j < size; j++)
79  {
80  if (token == prediction.getSuggestion(j).getWord())
81  {
82  // duplicate of token found, increment probability, up to MAX_PROBABILITY
83  suggestion.setProbability(
84  ( (suggestion.getProbability() + prediction.getSuggestion(j).getProbability()) > Suggestion::MAX_PROBABILITY
86  : (suggestion.getProbability() + prediction.getSuggestion(j).getProbability()))
87  );
88  //std::cerr << "[filter] duplicate found, adjusting probability" << std::endl;
89  }
90  }
91  seen_tokens.insert(token);
92  result.addSuggestion(suggestion);
93  //std::cerr << "[filter] added token " << token << std::endl;
94  }
95  }
96 
97  return result;
98 }
Combiner()
Definition: combiner.cpp:30
std::string getWord() const
Definition: suggestion.cpp:64
static const double MAX_PROBABILITY
Definition: suggestion.h:69
virtual Prediction filter(const Prediction &prediction) const
Definition: combiner.cpp:55
double getProbability() const
Definition: suggestion.cpp:69
void setProbability(double)
Definition: suggestion.cpp:79
Suggestion getSuggestion(int=0) const
Definition: prediction.cpp:73
size_t size() const
Definition: prediction.cpp:68
void addSuggestion(Suggestion)
Definition: prediction.cpp:90
virtual ~Combiner()
Definition: combiner.cpp:35