~ubuntu-branches/ubuntu/trusty/presage/trusty-proposed

« back to all changes in this revision

Viewing changes to src/lib/core/prediction.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Matteo Vescovi
  • Date: 2011-08-06 09:26:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110806092615-0wvhajaht9974ncx
Tags: upstream-0.8.6
ImportĀ upstreamĀ versionĀ 0.8.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
28
Prediction::Prediction()
 
29
{}
 
30
 
 
31
Prediction::~Prediction()
 
32
{}
 
33
 
 
34
const Prediction &Prediction::operator=( const Prediction &right )
 
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
 
 
73
Suggestion Prediction::getSuggestion(int i) const
 
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
 
 
90
void Prediction::addSuggestion(Suggestion s)
 
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
}