presage
0.8.7
|
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 "ngram.h" 00026 00027 Ngram::Ngram(const int size) 00028 { 00029 assert(size > 0); 00030 N = size; 00031 ngrams = new std::string[N]; 00032 } 00033 00034 Ngram::~Ngram() 00035 { 00036 delete[] ngrams; 00037 } 00038 00039 Ngram& Ngram::operator=( const Ngram& other ) 00040 { 00041 if (&other != this) { 00042 for (int i = 0; i < N; i++) { 00043 ngrams[i] = other.ngrams[i]; 00044 } 00045 } 00046 00047 return *this; 00048 } 00049 00050 00051 bool Ngram::operator<( const Ngram& other ) const 00052 { 00053 if (&other != this) { 00054 for (int i = N - 1; i >= 0; i--) { 00055 if (ngrams[i] < other.ngrams[i]) { 00056 return true; 00057 } 00058 } 00059 } 00060 00061 return false; 00062 } 00063 00064 std::string Ngram::toString() const 00065 { 00066 std::string str; 00067 for (int i = 0; i < N; i++) { 00068 str += "<" + ngrams[i] + "> "; 00069 } 00070 return str; 00071 } 00072 00073 std::string Ngram::getNgram(const int n) const 00074 { 00075 assert(n >= 0 && n < N); 00076 return ngrams[n]; 00077 } 00078 00079 void Ngram::setNgram(const int n, const std::string str) 00080 { 00081 assert(n >= 0 && n < N); 00082 ngrams[n] = str; 00083 } 00084 00085 int Ngram::getN() const 00086 { 00087 return N; 00088 } 00089 00090 std::ostream& operator<<( std::ostream& output, const Ngram& b ) 00091 { 00092 output << b.toString(); 00093 00094 return output; 00095 } 00096