-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathWordNgrams.cpp
330 lines (281 loc) · 7.2 KB
/
WordNgrams.cpp
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*******************************************************************
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 jerryy@gmail.com if you have any question or comment
WebSite: http://www.cs.dal.ca/~zyu
*************************************************************************/
#include "WordNgrams.h"
#include <sstream>
using ::std::istringstream;
using ::std::map;
using ::std::string;
WordNgrams::WordNgrams( int newNgramN, const char * newText, const char * newDelimiters, const char * newStopChars ) :
Ngrams( newNgramN, newText, newDelimiters, newStopChars )
{
addTokens();
}
WordNgrams::~WordNgrams()
{
}
void WordNgrams::addTokens()
{
int count = 0;
char c;
bool isSpecialChar = false;
String token;
token.reserve(256);
istringstream is(getText().c_str());
while ( ( c = (char)is.get() ) != EOF )
{
if ( isDelimiter( c ) || isStopChar ( c ) )
{
if ( !isSpecialChar && token.length() >0 )
{
this->addToken( token );
token.empty();
++count;
isSpecialChar = true;
}
else
{
isSpecialChar = false;
}
}
else
{
token.append( c );
isSpecialChar = false;
}
}
if ( token.length() > 0 )
{
++count;
this->addToken( token );
}
// special processing need to be done, if less than NGRAM_N tokens in the whole input text.
if ( count < this->getN() )
{
preParse( count );
}
/* int padding = ngramN - count % ngramN;
for ( int i=0; i< padding; i++)
{
addToken( "_" );
}
*/
//fclose( fp );
}
void WordNgrams::addToken ( const String & token )
{
char buff[32];
this->encodeInteger( this->AddToWordTable( token.isNumber() ? "<NUMBER>" : token.c_str() ), ENCODE_BASE, buff );
this->Ngrams::addToken( buff );
}
void WordNgrams::preParse( int count )
{
TokenNode * p, * newHead;
p = newHead = head;
String ngram;
ngram.reserve(256);
while ( newHead )
{
p = newHead;
ngram.empty();
for ( unsigned short i = 0; i< count; i++ )
{
ngram += p->token;
//printf("%d ngram %s.\n", i, ngram.c_str() );
this->addNgram( ngram.c_str(), i+1 );
if ( p == tail )
{
break;
}
else
{
ngram += ENCODE_WORD_DELIMITER;
}
p = p->next;
}
newHead = newHead->next;
}
}
void WordNgrams::parse()
{
TokenNode * p, * newHead;
p = newHead = head;
String ngram;
ngram.reserve( 256 );
while ( newHead )
{
unsigned short n = 0;
p = newHead;
ngram.empty();
while ( p )
{
ngram += p->token;
++n;
p = p->next;
if ( p )
ngram += ENCODE_WORD_DELIMITER;
}
//printf("%d ngram %s.\n", n, ngram.c_str() );
if ( n > 0 )
{
this->addNgram( ngram.c_str(), n );
}
newHead = newHead->next;
}
}
unsigned WordNgrams::AddToWordTable( const char * word )
{
unsigned id;
unsigned * value = wordTable.getValue( word );
if ( value )
{
id = *value;
}
else
{
id = wordTable.count();
wordTable.add( word, id );
}
return id;
}
void WordNgrams::output()
{
int ngramN = this->getN();
printf("BEGIN OUTPUT\n");
printf("Total %d unique ngram in %d ngrams.\n", this->count(), this->total() );
fprintf( stderr, "Total %d unique ngram in %d ngrams.\n", this->count(), this->total() );
for ( int i=1; i<=ngramN; i++ )
{
// Get sorted item list
Vector< NgramToken * > ngramVector;
this->getNgrams( ngramVector, i );
printf("\n%d-GRAMS\n", i);
printf("Total %d unique ngrams in %d %d-grams.\n", this->count( i ), this->total( i ), i );
fprintf( stderr, "Total %d unique ngrams in %d %d-grams.\n", this->count( i ), this->total( i ), i );
printf("------------------------\n");
size_t count = ngramVector.count();
ngramVector.sort( INgrams::compareFunction );
for ( unsigned j=0; j < count; j++ )
{
NgramToken * ngramToken = ngramVector[ j ];
//outputWordNgram( ngramToken.ngram, ngramToken.value.frequency, i );
printf("%s\t%d\n", ngramToken->ngram.c_str(), ngramToken->value.frequency );
delete ngramToken;
}
}
}
void WordNgrams::process(map<string, uint32_t>* ngr) {
int ngramN = this->getN();
for (int i = 1; i <= ngramN; i++) {
Vector<NgramToken*> ngramVector;
this->getNgrams(ngramVector, i);
size_t count = ngramVector.count();
ngramVector.sort(INgrams::compareFunction);
for (unsigned j = 0; j < count; j++) {
NgramToken* ngramToken = ngramVector[j];
(*ngr)[ngramToken->ngram.c_str()] = ngramToken->value.frequency;
delete ngramToken;
}
}
}
void WordNgrams::getNgrams( Vector< NgramToken * > & ngramVector, int n )
{
// Get sorted item list
Vector< TstItem< NgramValue > * > & itemVector = this->getItems( );
size_t count = itemVector.count();
String decodedKey;
decodedKey.reserve( 256 );
for ( unsigned i=0; i < count; i++ )
{
TstItem< NgramValue > * item = itemVector[i];
if ( item->value.n == n )
{
// decode the key to readable String
decodedKey.empty();
this->decodeWordNgram( item->key, n, decodedKey );
ngramVector.add( new NgramToken( decodedKey, item->value ) );
//ngramVector.add( NgramToken( decodedKey, item->value ) );
}
}
}
void WordNgrams::encodeInteger( int num, int bas, char* buff )
{
unsigned short index = 0;
int rem;
while ( num >= bas )
{
rem = num % bas;
//rem = rem == 0 ? ENCODE_NULL : rem;
if ( !rem )
{
rem = ENCODE_NULL;
}
num /= bas;
sprintf( buff + index++, "%c", rem );
}
if ( !num )
{
num = ENCODE_NULL;
}
sprintf( buff + index, "%c%c", num, '\0' );
}
int WordNgrams::decodeInteger( unsigned char * buffer, int bas )
{
int num=0, accumulate = 1;
unsigned short index = 0;
unsigned char c;
while ( ( c = buffer[index] ) != 0 )
{
//printf("%d.", c );
if ( index++ > 0 )
{
accumulate *= bas;
}
if ( c == WordNgrams::ENCODE_WORD_DELIMITER )
{
break;
}
num += ( c == ENCODE_NULL ? 0 : c ) * accumulate;
}
//printf("\n");
return num;
}
void WordNgrams::decodeWordNgram( const String & ngram, int n, String & decodedNgram )
{
//printf("outputWordNgram %s.\n", ngram.c_str() );
int index = 0;
int loop = 0;
const unsigned char * p = (unsigned char *)ngram.c_str();
unsigned char buff[32];
while ( loop++ < n )
{
while ( *p != WordNgrams::ENCODE_WORD_DELIMITER && *p != '\0' )
{
buff[ index ++ ] = (unsigned char)*p++;
}
++p;
buff[ index ] = '\0';
index = 0;
//printf("ID -- %d.\n", decodeInteger( buff, ENCODE_BASE ) );
decodedNgram += this->wordTable.getKey( decodeInteger( buff, ENCODE_BASE ) );
if ( loop < n )
{
decodedNgram.append( ' ' );
}
}
}