-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepfile.C
562 lines (522 loc) · 15.8 KB
/
prepfile.C
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/****************************** -*- C++ -*- *****************************/
/* */
/* LA-Strings: language-aware text-strings extraction */
/* by Ralf Brown / Carnegie Mellon University */
/* */
/* File: prepfile.C preprocessed file input */
/* Version: 1.30 */
/* LastEdit: 2019-07-15 */
/* */
/* (c) Copyright 2010,2011,2012,2013,2014,2015,2019 */
/* Ralf Brown/Carnegie Mellon University */
/* 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, version 3. */
/* */
/* 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 (file COPYING) along with this program. If not, see */
/* http://www.gnu.org/licenses/ */
/* */
/************************************************************************/
#include <algorithm>
#include <errno.h>
#include "prepfile.h"
#include "framepac/file.h"
#include "framepac/list.h"
#include "framepac/message.h"
#include "framepac/string.h"
#include "framepac/texttransforms.h"
using namespace Fr ;
/************************************************************************/
/* Globals for this module */
/************************************************************************/
uint64_t PreprocessedInputFile::s_sample_bytes = ~0U ;
bool PreprocessedInputFile::s_sample_uniformly = true ;
bool PreprocessedInputFile::s_convert_Latin1 = false ;
bool PreprocessedInputFile::s_ignore_whitespace = false ;
BigramExtension PreprocessedInputFile::s_bigram_ext = BigramExt_None ;
unsigned PreprocessedInputFile::s_alignment = 1 ;
Fr::CharPtr PreprocessedInputFile::s_from_enc = nullptr ;
Fr::CharPtr PreprocessedInputFile::s_to_enc = nullptr ;
/************************************************************************/
/* Methods for class PreprocessedInputFile */
/************************************************************************/
PreprocessedInputFile::PreprocessedInputFile()
{
m_buffered_lines = Fr::List::emptyList() ;
m_max_sample_bytes = (size_t)~0 ;
m_uniform_sample = true ;
#ifndef NO_ICONV
m_conversion = (iconv_t)-1 ;
#endif /* NO_ICONV */
return ;
}
//----------------------------------------------------------------------
PreprocessedInputFile::PreprocessedInputFile(const char *filename,
size_t sample_limit,
bool uniform_sample,
const char *from_enc, const char *to_enc)
{
m_buffered_lines = Fr::List::emptyList() ;
#ifndef NO_ICONV
m_conversion = (iconv_t)-1 ;
#endif /* NO_ICONV */
open(filename,sample_limit,uniform_sample,from_enc,to_enc) ;
return ;
}
//----------------------------------------------------------------------
bool PreprocessedInputFile::initializeTransliteration(const char *from, const char *to)
{
#ifndef NO_ICONV
m_conversion = (from && to) ? iconv_open(to,from) : (iconv_t)-1 ;
if (m_conversion == (iconv_t)-1)
return !(from && to) ;
// reset conversion state
iconv(m_conversion,0,0,0,0) ;
#endif /* !NO_ICONV */
return true ;
}
//----------------------------------------------------------------------
bool PreprocessedInputFile::shutdownTransliteration()
{
#ifndef NO_ICONV
if (m_conversion != (iconv_t)-1)
iconv_close(m_conversion) ;
m_conversion = (iconv_t)-1 ;
#endif /* !NO_ICONV */
return true ;
}
//----------------------------------------------------------------------
CFile* PreprocessedInputFile::open_sampled_input_file(const char *filename, size_t max_bytes)
{
// load in the entire input file
auto fp = new Fr::CInputFile(filename) ;
if (!fp || !*fp)
return fp ;
ListBuilder lb ;
size_t numlines = 0 ;
size_t total_bytes = 0 ;
while (Fr::StringPtr line = fp->getline())
{
total_bytes += line->c_len() ;
numlines++ ;
lb += line.move() ;
}
Fr::ListPtr lines = lb ;
// subsample the lines we read to be just a little more than the
// desired number of bytes
double interval = max_bytes / (double)total_bytes ;
if (interval > 0.5) // adjustment for high sampling rates
interval += (interval-0.5)/6.0 ;
size_t sampled = 0 ;
if (interval >= 0.98)
{
m_buffered_lines = lines.move()->reverse() ;
// lines = Fr::List::emptyList() ;
sampled = total_bytes ;
}
else
{
double avgline = total_bytes / (double)numlines ;
double count = interval / 2.0 ;
m_buffered_lines = Fr::List::emptyList() ;
while (lines)
{
StringPtr line = reinterpret_cast<String*>(poplist(lines)) ;
if (!line) continue ; //FIXME
size_t len = line->c_len() ;
double increment = interval * len / avgline ;
if (((size_t)(count + increment) > (size_t)count) ||
interval >= 1.0)
{
pushlist(line.move(),m_buffered_lines) ;
sampled += len ;
}
count += increment ;
}
}
// if (verbose)
{
SystemMessage::status(" Sampled %lu bytes from input file (requested %lu, filesize=%lu)",
sampled,max_bytes,total_bytes) ;
}
// if we subsampled but didn't get enough bytes, try again with a higher
// limit
if (sampled < m_max_sample_bytes && total_bytes >= max_bytes)
{
fp->close() ;
if (m_buffered_lines) m_buffered_lines->free() ;
m_buffered_lines = Fr::List::emptyList() ;
max_bytes *= (max_bytes / (double)sampled * 1.01) ;
return open_sampled_input_file(filename,max_bytes) ;
}
else
{
// reset the EOF condition
fp->seek(0) ;
clearerr(fp->fp()) ;
}
return fp ;
}
//----------------------------------------------------------------------
bool PreprocessedInputFile::open(const char *filename, size_t sample_limit, bool uniform_sample,
const char *from_enc, const char *to_enc)
{
close() ;
m_filename = Fr::dup_string(filename) ;
m_max_sample_bytes = sample_limit ;
m_uniform_sample = uniform_sample ;
m_bigram_ext = s_bigram_ext ;
m_convert_Latin1 = s_convert_Latin1 ;
m_ignore_whitespace = s_ignore_whitespace ;
m_alignment = s_alignment ;
m_bytes_read = 0 ;
if (from_enc && to_enc)
initializeTransliteration(from_enc,to_enc) ;
if (sample_limit + 1 != 0
&& m_alignment == 1) // can't currently sample UTF16
{
m_fp = open_sampled_input_file(filename,sample_limit) ;
}
else
new (&m_fp) Fr::CInputFile(filename) ;
return good() ;
}
//----------------------------------------------------------------------
void PreprocessedInputFile::close()
{
if (m_fp)
{
// close the input file
m_fp = nullptr ;
}
original_buffer_len = 0 ;
// discard any remnants of transliteration
translit_buffer_ptr = 0 ;
translit_buffer_len = 0 ;
// discard any remnants of buffered data from subsampling
m_buffered_lines->free() ;
m_buffered_lines = Fr::List::emptyList() ;
buffered_char = '\0' ;
// free iconv() resources
shutdownTransliteration() ;
return ;
}
//----------------------------------------------------------------------
int PreprocessedInputFile::readInput(unsigned char *buf, size_t buflen)
{
if (m_buffered_lines && m_buffered_lines != Fr::List::emptyList())
{
int count = 0 ;
while ((unsigned long)count < buflen && m_buffered_lines != Fr::List::emptyList())
{
size_t len = ((String*)m_buffered_lines->front())->c_len() ;
if (count + len > buflen)
break ;
StringPtr line = (String*)poplist(m_buffered_lines) ;
if (!line) continue ;
std::copy_n(line->c_str(),len,buf) ;
buf += len ;
count += len ;
}
return count ;
}
else if (m_fp)
return m_fp.read(buf,buflen) ;
else
return -1 ;
}
//----------------------------------------------------------------------
int PreprocessedInputFile::fillBuffer()
{
#ifndef NO_ICONV
if (m_conversion != (iconv_t)-1)
{
// try to fill the input buffer
int remainder = sizeof(original_buffer) - original_buffer_len ;
int read_count = readInput(original_buffer+original_buffer_len,
remainder) ;
original_buffer_len += read_count ;
// convert as much as possible
char *origbuf = (char*)original_buffer ;
size_t orig_len = original_buffer_len ;
char *translitbuf = (char*)translit_buffer ;
size_t translit_len = sizeof(translit_buffer) ;
errno = 0 ;
size_t count = iconv(m_conversion,&origbuf,&orig_len,&translitbuf,
&translit_len) ;
if (count != (size_t)-1)
errno = 0 ;
if (errno == EILSEQ)
{
// bad input, try to skip ahead
if (translit_len > 0)
{
*translitbuf++ = *origbuf++ ;
orig_len-- ;
translit_len-- ;
}
}
else if (errno != EINVAL && errno != E2BIG)
{
// only need to deal with the error if it is not an
// incomplete multibyte sequence or running out of room in
// the output buffer; in this case, we just copy as much as
// we can to the output buffer
while (orig_len > 0 && translit_len > 0)
{
*translitbuf++ = *origbuf++ ;
orig_len-- ;
translit_len-- ;
}
// reset the conversion state
iconv(m_conversion,0,0,0,0) ;
}
else if (read_count == 0) // at end of file?
{
// anything still left in original_buffer is unconvertible
// bytes, so just copy them over
translit_buffer_len = original_buffer_len ;
if (original_buffer_len)
{
std::copy_n(original_buffer,original_buffer_len,translit_buffer) ;
original_buffer_len = 0 ;
}
translit_buffer_ptr = 0 ;
return translit_buffer_len ;
}
if (orig_len > 0)
{
// we have data remaining in the input buffer, so
// copy it to the beginning for the next refill
std::copy_n(origbuf,orig_len,original_buffer) ;
}
original_buffer_len = orig_len ;
translit_buffer_ptr = 0 ;
translit_buffer_len = (translitbuf - (char*)translit_buffer) ;
}
else
#endif /* NO_ICONV */
{
translit_buffer_ptr = 0 ;
translit_buffer_len
= readInput(translit_buffer,sizeof(translit_buffer)) ;
}
return translit_buffer_len ;
}
//----------------------------------------------------------------------
bool PreprocessedInputFile::moreData() const
{
if (m_bytes_read >= m_max_sample_bytes)
return false ;
return (translit_buffer_len > translit_buffer_ptr) || !m_fp.eof() ;
}
//----------------------------------------------------------------------
int PreprocessedInputFile::peekAtBuffer()
{
if (translit_buffer_ptr >= translit_buffer_len)
{
if (fillBuffer() <= 0)
return EOF ;
}
return translit_buffer[translit_buffer_ptr] ; // don't advance pointer
}
//----------------------------------------------------------------------
int PreprocessedInputFile::peekByte()
{
if (m_bytes_read >= m_max_sample_bytes)
return EOF ;
if (m_convert_Latin1)
{
if (buffered_char >= 0x80)
return buffered_char ;
if (ignoringWhitespace())
{
while (peekAtBuffer() == ' ')
{
(void)getFromBuffer() ;
}
}
return peekAtBuffer() ;
}
else if (m_bigram_ext == BigramExt_None)
{
if (ignoringWhitespace())
{
while (peekAtBuffer() == ' ')
{
(void)getFromBuffer() ;
}
}
return peekAtBuffer() ;
}
else if ((m_bytes_read & 1) != 0)
return buffered_char ;
else
return peekAtBuffer() ;
}
//----------------------------------------------------------------------
int PreprocessedInputFile::getFromBuffer()
{
if (translit_buffer_ptr >= translit_buffer_len)
{
if (fillBuffer() <= 0)
return EOF ;
}
return translit_buffer[translit_buffer_ptr++] ; // advance pointer
}
//----------------------------------------------------------------------
unsigned PreprocessedInputFile::getCodepoint()
{
int byte = getFromBuffer() ;
if (byte == EOF)
{
return byte ;
}
// ensure no sign-extension
unsigned codepoint = byte & 0xFF ;
if ((byte & 0x80) != 0 &&
!(m_bigram_ext == BigramExt_ASCIILittleEndian ||
m_bigram_ext == BigramExt_ASCIIBigEndian))
{
// figure out how many bytes make up the code point, and put the
// highest-order bits stored in the first byte into the
// codepoint variable
unsigned extra = 0 ;
if ((byte & 0xE0) == 0xC0)
{
codepoint = (byte & 0x1F) ;
extra = 1 ;
}
else if ((byte & 0xF0) == 0xE0)
{
codepoint = (byte & 0x0F) ;
extra = 2 ;
}
else if ((byte & 0xF8) == 0xF0)
{
codepoint = (byte & 0x07) ;
extra = 3 ;
}
else if ((byte & 0xFC) == 0xF8)
{
codepoint = (byte & 0x03) ;
extra = 4 ;
}
else if ((byte & 0xFE) == 0xFC)
{
codepoint = (byte & 0x01) ;
extra = 5 ;
}
// read in the additional bytes specified by the high bits of the
// codepoint's first byte
for (size_t i = 1 ; i <= extra ; i++)
{
byte = getFromBuffer() ;
if (byte == EOF)
return byte ;
else if ((byte & 0xC0) != 0x80)
break ; // invalid UTF8
// each extra byte gives us six more bits of the codepoint
codepoint = (codepoint << 6) | (byte & 0x3F) ;
}
}
return codepoint ;
}
//----------------------------------------------------------------------
int PreprocessedInputFile::getByte()
{
if (m_bytes_read >= m_max_sample_bytes)
{
translit_buffer_ptr = translit_buffer_len ;
return EOF ;
}
if (m_convert_Latin1)
{
int cp ;
if (buffered_char >= 0x80)
{
cp = buffered_char ;
buffered_char = 0 ;
}
else
{
do {
cp = getFromBuffer() ;
} while (ignoringWhitespace() && cp == ' ') ;
if (cp != EOF)
{
if ((cp & 0xFF) >= 0x80)
{
buffered_char = (unsigned char)(0x80 | (cp & 0x3F)) ;
cp = 0xC0 | ((cp & 0xFF) >> 6) ;
}
}
}
if (cp != EOF)
++m_bytes_read ;
return cp ;
}
if (m_bigram_ext == BigramExt_None)
{
int cp ;
do {
cp = getFromBuffer() ;
} while (ignoringWhitespace() && cp == ' ') ;
++m_bytes_read ;
return cp ;
}
if ((m_bytes_read & 1) != 0)
{
++m_bytes_read ;
return buffered_char ;
}
int cp = getCodepoint() ;
if (cp == EOF)
return cp ;
++m_bytes_read ;
if (m_bigram_ext == BigramExt_ASCIILittleEndian ||
m_bigram_ext == BigramExt_UTF8LittleEndian)
{
buffered_char = (unsigned char)((cp >> 8) & 0xFF) ;
return (cp & 0xFF) ;
}
else
{
buffered_char = (unsigned char)(cp & 0xFF) ;
return (cp >> 8) & 0xFF ;
}
}
//----------------------------------------------------------------------
bool PreprocessedInputFile::setDefaultTransliteration(const char *from, const char *to)
{
if (!from && !to)
{
s_from_enc = s_to_enc = nullptr ;
return true ;
}
if (!from || !to)
return false ;
#ifdef NO_ICONV
(void)from ; (void)to ;
return false ;
#else
iconv_t conversion = iconv_open(to,from) ;
bool can_translit = (conversion != (iconv_t)-1) ;
if (can_translit)
{
iconv_close(conversion) ;
s_from_enc = Fr::dup_string(from) ;
s_to_enc = Fr::dup_string(to) ;
}
return can_translit ;
#endif
}
/************************************************************************/
/************************************************************************/
// end of file prepfile.C //