forked from zvelo/ngrams
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngrams.h
224 lines (179 loc) · 4.91 KB
/
ngrams.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*******************************************************************
C++ Package of Ternary Search Tree
Copyright (C) 2006 Zheyuan Yu
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Read full GPL at http://www.gnu.org/copyleft/gpl.html
Email me at [email protected] if you have any question or comment
WebSite: http://www.cs.dal.ca/~zyu
*************************************************************************/
#ifndef _Ngrams_h
#define _Ngrams_h
#include "INgrams.h"
#include "ternarySearchTree.h"
#include "config.h"
/**
* class for common ngram operations
* Revisions:
* Feb 18, 2006. Jerry Yu
* Initial implementation
*/
class Ngrams : public INgrams
{
public:
Ngrams( int newNgramN, const char * newText, const char * newDelimiters = Config::getDefaultDelimiters(), const char * newStopChars = Config::getDefaultStopChars() );
~Ngrams()
{
releaseQueue();
delete[] totals;
delete[] uniques;
}
/**
* feed a token in, the token will be processed internally to generating ngram
*
* for word ngram, the token will be word String,
* for character ngram, the token will be a String of the character.
*/
virtual void addToken ( const String & token );
/**
* set delimiters
*/
void setDelimiters( const char * newDelimiters )
{
this->delimiters = newDelimiters ;
}
void setStopChars( const char * newStopChars )
{
this->stopChars = newStopChars;
}
String & getText()
{
return this->text;
}
int getN()
{
return ngramN;
}
/**
* This methods return true if given char is set to be a delimiter
* @param c - input character
* @return true if c is set to be a delimiter
*/
bool isDelimiter( int c ) const
{
return strchr( this->delimiters.c_str(), c ) != NULL;
}
/**
* This methods return true if given char is set to be a stop char
* @param c - input char
* @return true if c is set to be a stop char
*/
bool isStopChar( int c ) const
{
return strchr( this->stopChars.c_str(), c ) != NULL;
}
/**
* total ngrams ( duplications are counted ), which is total of frequency of each ngram
*/
int total()
{
int ret = 0;
for ( int i = 1; i<= ngramN; i++ )
{
ret += total( i );
}
return ret;
}
/**
* total ngrams for given N of ngram( duplications are counted ), which is total of frequency of each ngram
*/
int total( int n )
{
return n > 0 && n <= ngramN ? totals [ n - 1 ] : 0;
}
/**
* get total number of unique ngrams
*/
int count()
{
return ngramTable.count();
}
/**
* get total number of unique ngrams for given N
*/
int count( int n )
{
return n > 0 && n <= ngramN ? uniques [ n - 1 ] : 0;
}
protected:
TernarySearchTree< NgramValue > ngramTable;
String delimiters;
struct TokenNode
{
String token;
TokenNode * next;
TokenNode ( const char * newToken ) : token( newToken ), next( 0 )
{
}
};
TokenNode * head, * tail;
int ngramN; // default number of ngrams
/**
* add a ngram to the ngram list.
* if it is not on the list, add it, otherwise increase the ngram frequent count by 1
* @param n - number of the ngram
*/
void addNgram( const char * ngram, int n );
/**
* get all the items( key & value pairs ) in the tree
* @param n - n of ngram
* @return vector that contains all the item pointers
*/
Vector< TstItem<NgramValue> * > & getItems( )
{
return ngramTable.getItems();
}
private:
String text; // text content
String stopChars;
int tokenCount; //used for counting when parsing text
int * totals; // array for count total grams ( duplicated are counted ) for each N
int * uniques; // array for counting unique grams for each each N
/**
* add token to the queue. The queue will be used to generate ngram
* @param token - token to be added to the queue.
* @return total number of tokens in the queue
*/
int pushQueue( const char * token );
void popQueue();
/**
* Generate ngrams when queue has NGRAM_N - 1 tokens.
* the token queue need to be processed specially for the first NGRAM_N - 1 tokens,
* also need to be called if less than NGRAM_N tokens in the whole input text.
* @param count - total items in the queue
*/
virtual void preParse( int count )=0;
/**
* Once the queue is full, it will start to pop out ngrams
* for each new token pushing in
*/
virtual void parse(){};
// release queue memory
void releaseQueue()
{
while ( tokenCount > 0 )
{
popQueue();
}
}
};
#endif