presage 0.9.1
ngram.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 "ngram.h"
26
27Ngram::Ngram(const int size)
28{
29 assert(size > 0);
30 N = size;
31 ngrams = new std::string[N];
32}
33
35{
36 delete[] ngrams;
37}
38
40{
41 if (&other != this) {
42 for (int i = 0; i < N; i++) {
43 ngrams[i] = other.ngrams[i];
44 }
45 }
46
47 return *this;
48}
49
50
51bool Ngram::operator<( const Ngram& other ) const
52{
53 if (&other != this) {
54 for (int i = N - 1; i >= 0; i--) {
55 if (ngrams[i] < other.ngrams[i]) {
56 return true;
57 }
58 }
59 }
60
61 return false;
62}
63
64std::string Ngram::toString() const
65{
66 std::string str;
67 for (int i = 0; i < N; i++) {
68 str += "<" + ngrams[i] + "> ";
69 }
70 return str;
71}
72
73std::string Ngram::getNgram(const int n) const
74{
75 assert(n >= 0 && n < N);
76 return ngrams[n];
77}
78
79void Ngram::setNgram(const int n, const std::string str)
80{
81 assert(n >= 0 && n < N);
82 ngrams[n] = str;
83}
84
85int Ngram::getN() const
86{
87 return N;
88}
89
90std::ostream& operator<<( std::ostream& output, const Ngram& b )
91{
92 output << b.toString();
93
94 return output;
95}
96
Definition: ngram.h:33
void setNgram(const int n, const std::string str)
Definition: ngram.cpp:79
Ngram(const int N)
Definition: ngram.cpp:27
Ngram & operator=(const Ngram &)
Definition: ngram.cpp:39
int getN() const
Definition: ngram.cpp:85
bool operator<(const Ngram &) const
Definition: ngram.cpp:51
int N
Definition: ngram.h:50
~Ngram()
Definition: ngram.cpp:34
std::string toString() const
Definition: ngram.cpp:64
std::string * ngrams
Definition: ngram.h:51
std::string getNgram(const int n) const
Definition: ngram.cpp:73
std::ostream & operator<<(std::ostream &output, const Ngram &b)
Definition: ngram.cpp:90