Welcome to mirror list, hosted at ThFree Co, Russian Federation.

_SuffixArrayScanningBase.cpp « SuffixArrayScan « SuffixArrayApplications « Src - github.com/moses-smt/salm.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9050408a24f680715364b3adeecacee67077d601 (plain)
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
331
332
333
334
335
336
337
338
/**
* Revision $Rev: 3794 $
* Last Modified $LastChangedDate: 2007-06-29 02:17:32 -0400 (Fri, 29 Jun 2007) $
**/

#include "_SuffixArrayScanningBase.h"
#include <iostream>
#include <stdlib.h>

using namespace std;

C_SuffixArrayScanningBase::C_SuffixArrayScanningBase()
{
	this->countOfCountsTable = 0;	//no memory has been allocated
	this->maxFreqConsidered = 1000;	//for freq >1000, no need to discount, MLE is good enough
}

C_SuffixArrayScanningBase::C_SuffixArrayScanningBase(const char * filename, unsigned int maxN)
{
	this->countOfCountsTable = 0;  //no memory has been allocated
	this->maxFreqConsidered = 1000; //for freq >1000, no need to discount, MLE is good enough

	//load suffix array
	this->loadData(filename, false, true, true);

	this->initializeForScanning(filename, maxN);
}

void C_SuffixArrayScanningBase::setParam_maxFreqConsidered(int maxFreqConsidered)
{
	this->maxFreqConsidered = maxFreqConsidered;
}


/**
* Initialize data structure needed for scanning after the suffix array has been loaded
**/
void C_SuffixArrayScanningBase::initializeForScanning(const char * filename, unsigned int maxN)
{
	this->maxN = maxN;
	this->nGramScanningList = (S_nGramScanningInfoElement *) malloc(sizeof(S_nGramScanningInfoElement)*this->maxN);
	this->countOfCountsTable = 0;	//no memory has been allocated

	//initialize the scanning list
	for(int i=0;i<this->maxN;i++){
		this->nGramScanningList[i].freqSoFar=0;
		this->nGramScanningList[i].vocId = 0;
		this->nGramScanningList[i].freqThreshForOutput = (unsigned int) -1;	//default, do not output
	}

	//get vocID for sentEnd
	this->vocIdForSentEnd = this->voc->returnId(C_String("_END_OF_SENTENCE_"));

	if(this->vocIdForSentEnd==0){
		cerr<<"VocID for _END_OF_SENTENCE_ can not be found. Critical error.\n";
		exit(0);
	}

	this->vocIdForSentStart = this->voc->returnId(C_String("_SENTENCE_START_"));
	if(this->vocIdForSentStart==0){
		cerr<<"VocID for _SENTENCE_START_ can not be found. Critical error.\n";
		exit(0);
	}
	
	this->vocIdForCorpusEnd = this->voc->returnId(C_String("_END_OF_CORPUS_"));
	if(this->vocIdForCorpusEnd==0){
		cerr<<"VocID for _END_OF_CORPUS_ can not be found. Critical error.\n";
		exit(0);
	}
}

C_SuffixArrayScanningBase::~C_SuffixArrayScanningBase()
{
	free(this->nGramScanningList);

	if(this->countOfCountsTable!=0){
		free(this->countOfCountsTable);
	}

}

void C_SuffixArrayScanningBase::setNgramOutputFreqThresh(int n, unsigned int freqThresh)
{
	if(n>this->maxN){
		cerr<<"Illegal operation.n="<<n<<" is greater than maxN="<<this->maxN<<endl;
		exit(0);
	}

	this->nGramScanningList[n-1].freqThreshForOutput = freqThresh;
}

void C_SuffixArrayScanningBase::scanSuffixArrayForHighFreqNgramType()
{
	this->scanSuffixArray('H');

}

/// Count of counts is the number of n-gram types that occur a certain times in the corpus.
/// Count of counts is important information in LM smoothing
/// We scan the corpus for n-gram's type/token frequency and collect information for 1-gram, 2-gram,...and up to maxFreqConsidered-gram
void C_SuffixArrayScanningBase::scanSuffixArrayForCountofCounts(int maxFreqConsidered)
{
	this->maxFreqConsidered = maxFreqConsidered;
	this->constructCountOfCountsTable();
	
	//output the count of counts
	cout<<this->maxN<<"\t"<<maxFreqConsidered<<endl;
	for(int i=0;i<this->maxN;i++){
		cout<<i+1<<endl;
		
		unsigned int * ccTableForThisN = this->countOfCountsTable + i*maxFreqConsidered;
		for(int freq=0;freq<maxFreqConsidered;freq++){
			cout<<freq+1<<"\t"<<ccTableForThisN[freq]<<endl;
		}
	}
	
}

///Check from 1-gram to maxN-gram for type-token information
///the process is similar to "scanSuffixArrayForHighFreqNgramType"
void C_SuffixArrayScanningBase::scanSuffixArrayForTypeToken()
{
	this->typeFreq = (unsigned int *) malloc(sizeof(unsigned int)*maxN);
	this->tokenFreq = (unsigned int *) malloc(sizeof(unsigned int)*maxN);

	//initialize
	for(int n=0;n<maxN;n++){
		this->typeFreq[n]=0;
		this->tokenFreq[n]=0;
	}


	//scan the suffix array
	this->scanSuffixArray('T');

	//output
	cout<<"n\tType\tToken\n";
	for(int i=0;i<this->maxN;i++){
		cout<<i+1<<"\t"<<typeFreq[i]<<"\t"<<tokenFreq[i]<<endl;
	}
}

/**
* Allocate memory for count-of-counts table and scan the corpus to fill in count of counts
* memory will be freed in the destructor
**/
void C_SuffixArrayScanningBase::constructCountOfCountsTable()
{
	if(this->countOfCountsTable!=0){	//if there is already a count of counts table
		free(this->countOfCountsTable);
	}

	this->countOfCountsTable = (unsigned int *) malloc(sizeof(unsigned int)*this->maxN*this->maxFreqConsidered);

	if(this->countOfCountsTable==NULL){
		cerr<<"Count of counts table can not be initialized. Exit\n";
		exit(0);
	}

	for(int c=0;c<this->maxN*this->maxFreqConsidered;c++){
		this->countOfCountsTable[c]=0;
	}

	this->scanSuffixArray('C');


}

/**
* Scan through the indexed corpus and according to the action type, 
* perform actions accordingly when seeing a new n-gram type
**/
void C_SuffixArrayScanningBase::scanSuffixArray(char actionType)
{
	
	int i,j;
	bool stillMeaningful = true;	
	TextLenType saPos=0;

	while(stillMeaningful && ( saPos<this->corpusSize ) ){

		TextLenType posInCorpus = this->suffix_list[saPos];
		IndexType wordInCorpus = this->corpus_list[posInCorpus];

		if(wordInCorpus<this->sentIdStart){	//SA positions pointing to sentID are not interesting
			
			if((wordInCorpus!=this->vocIdForSentStart)&&(wordInCorpus!=this->vocIdForSentEnd)&&(wordInCorpus!=this->vocIdForCorpusEnd)){	//n-grams start with <s> and </s>, or <end of corpus> are not interested
			
				bool quit =false;
				i=0;

				while(!quit && (i<this->maxN)){
					wordInCorpus = this->corpus_list[posInCorpus+i];
					if(						
						(wordInCorpus<this->sentIdStart)&&
						(wordInCorpus!=this->vocIdForSentEnd)&&
						(wordInCorpus!=this->vocIdForSentStart)&&
						(wordInCorpus==this->nGramScanningList[i].vocId)){	//still match

						this->nGramScanningList[i].freqSoFar++;
					}
					else{	//we will have new (i+1) and longer n-grams soon, before that check if we should increase the count of counts for n because of this n-gram type
									
						bool validNgramUpSoFar = true;
						unsigned int freqSoFar;
						C_String tmpPhrase; //for output high freq n-grams

						//prepare the prefix of the n-grams
						if(actionType=='H'){
							//common i-gram
							for(j=0;j<=i-1;j++){
								if(this->nGramScanningList[j].vocId==0){	//one of the word in the common i-gram is a NULL word, not a valid n-gram
									validNgramUpSoFar = false;
								}
								tmpPhrase.appending(this->voc->getText(this->nGramScanningList[j].vocId));
								tmpPhrase.appending(C_String(" "));
							}	
						}


						for(j=i;j<this->maxN;j++){				
							
							
							if(this->nGramScanningList[j].vocId==0){		//a NULL word, then this n-gram and longer ones in the scan window are invalid
								validNgramUpSoFar = false;
							}

							if(validNgramUpSoFar){		//perform actions depends on actionType
								
								switch(actionType){

								case 'C':	//count of counts
									freqSoFar = this->nGramScanningList[j].freqSoFar;
									if( (freqSoFar > 0) && ( freqSoFar <= this->maxFreqConsidered) ){
										//increase the count for (j+1)-gram with freq freqSoFar
										this->countOfCountsTable[j*this->maxFreqConsidered+freqSoFar-1]++;
									}
									break;

								case 'H':	//output high-freq n-grams
									tmpPhrase.appending(this->voc->getText(this->nGramScanningList[j].vocId));
									tmpPhrase.appending(C_String(" "));

									if(this->nGramScanningList[j].freqSoFar>=this->nGramScanningList[j].freqThreshForOutput){							
										cout<<tmpPhrase.toString()<<"\t"<<this->nGramScanningList[j].freqSoFar<<endl;
									}
									break;

								case 'T':	//type-token statistics
									if(this->nGramScanningList[j].freqSoFar>0){
										typeFreq[j]++;
									}

									tokenFreq[j]+=this->nGramScanningList[j].freqSoFar;

									break;	
								default: 
									cerr<<"Unknown action!\n";
									exit(-1);
								}
							}

							//finished output, now clear the list from point of i
							if((posInCorpus+j)<this->corpusSize){
								wordInCorpus = this->corpus_list[posInCorpus+j];
							}
							else{
								wordInCorpus = 0;	//out of bound for corpus
							}

							if((wordInCorpus==0)||(wordInCorpus>=this->sentIdStart)||(wordInCorpus==this->vocIdForSentEnd)||(wordInCorpus==this->vocIdForSentStart)){
								wordInCorpus=0;	//write 0 for <sentId>, <s> and </s>
								this->nGramScanningList[j].freqSoFar = 0;
							}
							else{
								this->nGramScanningList[j].freqSoFar = 1;
							}

							this->nGramScanningList[j].vocId = wordInCorpus;							
						}

						quit=true;	//at i+1 gram, already not match, no need to check for longer
					}

					i++;
				}
			}
		}
		else{
			stillMeaningful = false;	//once vocID is getting larger/equal than sentIdStart, everything follows it are <sentId> and no actual text
		}

		saPos++;
	}

	//at the end of corpus (according to suffix order)
	C_String finalTmpString;	//for output high-freq n-gram type
	bool validNgramUpSoFar = true;
	unsigned int freqSoFar;
	for(i=0;i<this->maxN;i++){
		if(this->nGramScanningList[i].vocId==0){	//invalide word
			validNgramUpSoFar = false;
		}

		if(validNgramUpSoFar){
			switch(actionType){
			case 'C':	//for count-of-counts
				freqSoFar = this->nGramScanningList[i].freqSoFar;
				if( (freqSoFar > 0) && ( freqSoFar <= this->maxFreqConsidered) ){
					//increase the count for (i+1)-gram with freq freqSoFar
					this->countOfCountsTable[i*this->maxFreqConsidered+freqSoFar-1]++;
				}
				break;

			case 'H':	//for high-freq n-gram types
				finalTmpString.appending(this->voc->getText(this->nGramScanningList[i].vocId));
				finalTmpString.appending(C_String(" "));
				if(this->nGramScanningList[i].freqSoFar>this->nGramScanningList[i].freqThreshForOutput){			
					cout<<finalTmpString.toString()<<"\t"<<this->nGramScanningList[i].freqSoFar<<endl;
				}
				break;

			case 'T':	//for type-token statistics
				if(this->nGramScanningList[i].freqSoFar>0){
					typeFreq[i]++;
				}

				tokenFreq[i]+=this->nGramScanningList[i].freqSoFar;
				break;

			default: 
				cerr<<"Unknown action!\n";
				exit(-1);
			}
		}
	}

}