-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCharNgrams.cpp
More file actions
180 lines (155 loc) · 4.43 KB
/
CharNgrams.cpp
File metadata and controls
180 lines (155 loc) · 4.43 KB
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
/*******************************************************************
C++ Package of ngrams
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 "CharNgrams.h"
#include <sstream>
using ::std::istringstream;
CharNgrams::CharNgrams( int newNgramN, const char * newText, const char * newDelimiters, const char * newStopChars ) :
Ngrams( newNgramN, newText, newDelimiters, newStopChars )
{
addTokens();
}
CharNgrams::~CharNgrams()
{
}
void CharNgrams::addTokens()
{
int count = 0;
char c[2];
c[1]=0;
bool isSpecialChar = false;
istringstream is(getText().c_str());
while ( ( c[0] = (char)toupper(is.get()) ) != EOF )
{
if ( isStopChar( c[0] ) )
{
c[0]=this->delimiters[0];
}
if ( isDelimiter( c[0] ) )
{
c[0] = '_';
if ( !isSpecialChar )
{
addToken( c );
count++;
}
isSpecialChar = true;
}
else
{
addToken( c );
isSpecialChar = false;
count++;
}
}
// special processing need to be done, if less than NGRAM_N tokens in the whole input text.
if ( count < this->getN() )
{
preParse( count );
}
}
void CharNgrams::preParse( int count )
{
TokenNode * p, * newHead;
p = newHead = head;
String ngram;
ngram.reserve( 64 );
while ( newHead )
{
p = newHead;
ngram.empty();
for ( int 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;
}
p = p->next;
}
newHead = newHead->next;
}
}
void CharNgrams::parse()
{
TokenNode * p, * newHead;
p = newHead = head;
String ngram;
ngram.reserve( 64 );
while ( newHead )
{
int n = 0;
p = newHead;
ngram.empty();
while ( p )
{
ngram += p->token;
n++;
p = p->next;
}
//printf("%d ngram %s.\n", n, ngram.c_str() );
if ( n > 0 )
{
this->addNgram( ngram.c_str(), n );
}
newHead = newHead->next;
}
}
void CharNgrams::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 ( Total %d unique ngrams in %d grams )\n", i, this->count( i ), this->total( i ) );
fprintf( stderr, "\n%d-GRAMS ( Total %d unique ngrams in %d grams )\n", i, this->count( i ), this->total( i ) );
printf("------------------------\n");
size_t count = ngramVector.count();
ngramVector.sort( INgrams::compareFunction );
for ( unsigned j=0; j < count; j++ )
{
NgramToken * ngramToken = ngramVector[ j ];
printf("%s\t%d\n", ngramToken->ngram.c_str(), ngramToken->value.frequency );
delete ngramToken;
}
}
}
void CharNgrams::getNgrams( Vector< NgramToken * > & ngramVector, int n )
{
// Get sorted item list
//Vector<String> & keyVector = getKeys( );
//Vector<NgramValue> & valueVector = getValues( );
Vector< TstItem< NgramValue > * > & itemVector = getItems();
size_t count = itemVector.count();
for ( unsigned i=0; i < count; i++ )
{
TstItem< NgramValue > * item = itemVector[i];
if ( item && item->value.n == n )
{
ngramVector.add( new NgramToken( item->key, item->value ) );
//ngramVector.add( NgramToken( item->key, item->value ) );
}
}
}