-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubsample.C
342 lines (311 loc) · 9.77 KB
/
subsample.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
/****************************** -*- C++ -*- *****************************/
/* */
/* LangIdent: long n-gram-based language identification */
/* by Ralf Brown / Carnegie Mellon University */
/* */
/* File: subsample.C utility program to sample lines of text */
/* Version: 1.30 */
/* LastEdit: 2019-07-15 */
/* */
/* (c) Copyright 2012,2013,2014,2019 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 <cstring>
#include <iostream>
#include "framepac/file.h"
#include "framepac/random.h"
using namespace std ;
using namespace Fr ;
/************************************************************************/
/* Manifest Constants */
/************************************************************************/
#define MAX_LINE 32768U
/************************************************************************/
/* Types */
/************************************************************************/
class StringList
{
public:
StringList(CharPtr strng) ;
~StringList() = default ;
// accessors
StringList* next() const { return m_next ; }
const char* string() const { return m_string ; }
unsigned length() const { return m_length ; }
unsigned listlength() const ;
// modifiers
void next(StringList* nxt) { m_next = nxt ; }
// factory
static void append(CharPtr strng, StringList**& last_string) ;
private:
StringList* m_next { nullptr } ;
CharPtr m_string ;
unsigned m_length ;
} ;
/************************************************************************/
/* Methods for class StringList */
/************************************************************************/
StringList::StringList(CharPtr strng) : m_string(strng)
{
m_length = m_string ? strlen(m_string) : 0 ;
return ;
}
//----------------------------------------------------------------------
unsigned StringList::listlength() const
{
unsigned count = 0 ;
for (const StringList *s = this ; s ; s = s->next())
{
count++ ;
}
return count ;
}
//----------------------------------------------------------------------
void StringList::append(CharPtr strng, StringList**& last_string)
{
if (!last_string)
return ; // no list, so we can't do anything
auto str = new StringList(strng) ;
if (!str)
return ; // Out of memory! Ignore line
*last_string = str ;
StringList** end_ptr = &str->m_next ;
last_string = end_ptr ;
return ;
}
/************************************************************************/
/************************************************************************/
static void usage(const char *argv0)
{
cerr << "Usage: " << argv0 << " [options] count <inputfile" << endl ;
cerr << "Extract 'count' lines from the input file and write them to "
"standard output.\n"
"Options:\n"
"\t-b\tdeterministically sample approx. 'count' bytes in total\n"
"\t-i\tsample every 'count'th line ('count' is interval, not total)\n"
"\t-lX\tsample lines more than 'X' bytes in length ('count' ignored)\n"
"\t-LX\tsample lines less than 'X' bytes in length\n"
"\t-u\tsample uniformly-spaced lines\n"
"\t-oF\twrite sampled lines to file F (default is standard output)\n"
"\t-rF\twrite non-sampled (rejected) lines to file F\n"
"\t-R\tgenerate the same 'random' sample every time\n"
<< endl ;
exit(1) ;
}
//----------------------------------------------------------------------
static void write_line(CFile& f, const char* line)
{
if (f)
{
f.puts(line) ;
f.putc('\n') ;
}
return ;
}
//----------------------------------------------------------------------
static bool select_line(bool selected, CFile& selectfp, CFile& rejectfp, const char* line)
{
if (selected)
write_line(selectfp,line) ;
else
write_line(rejectfp,line) ;
return selected ;
}
//----------------------------------------------------------------------
static void take_uniform_bytes(StringList* lines, size_t sample_size, CFile& selectfp, CFile& rejectfp)
{
if (!lines)
return ;
size_t total_bytes = 0 ;
for (const StringList *l = lines ; l ; l = l->next())
{
total_bytes += l->length() ;
}
double sample_rate = (sample_size + 1.0) / (double)total_bytes ;
size_t sampled_bytes = 0 ;
total_bytes = 0 ;
while (lines)
{
Owned<StringList> line = lines ;
lines = lines->next() ;
size_t len = line->length() ;
if (select_line(sampled_bytes <= (total_bytes * sample_rate),selectfp,rejectfp,line->string()))
sampled_bytes += len ;
total_bytes += len ;
}
return ;
}
//----------------------------------------------------------------------
static void take_uniform_sample(StringList* lines, size_t sample_size, CFile& selectfp, CFile& rejectfp)
{
if (!lines)
return ;
size_t numlines = lines->listlength() ;
double interval = sample_size / (double)numlines ;
double count = interval/2.0 ;
while (lines)
{
Owned<StringList> line = lines ;
lines = lines->next() ;
select_line((size_t)(count + interval) > (size_t)count,selectfp,rejectfp,line->string()) ;
count += interval ;
}
return ;
}
//----------------------------------------------------------------------
static void take_random_sample(StringList* lines, size_t sample_size, CFile& selectfp, CFile& rejectfp)
{
size_t numlines = lines->listlength() ;
if (sample_size >= numlines)
{
while (lines)
{
Owned<StringList> line = lines ;
lines = lines->next() ;
write_line(selectfp,line->string()) ;
}
}
else
{
auto selected = RandomSample(numlines,sample_size) ;
for (unsigned i = 0 ; i < numlines ; i++)
{
Owned<StringList> line = lines ;
lines = lines->next() ;
select_line(selected[i],selectfp,rejectfp,line->string()) ;
}
}
return ;
}
//----------------------------------------------------------------------
int main(int argc, char **argv)
{
bool uniform_sample = false ;
bool use_bytes = false ;
bool use_interval = false ;
bool use_length = false ;
bool randomized = true ;
unsigned min_length = 0 ;
unsigned max_length = (unsigned)~0 ;
const char* output_file = "-" ;
const char* reject_file = 0 ;
const char* argv0 = argv[0] ;
while (argc > 1 && argv[1][0] == '-')
{
switch (argv[1][1])
{
case 'b':
use_bytes = true ;
use_interval = false ;
use_length = false ;
uniform_sample = false ;
break ;
case 'i':
use_interval = true ;
use_bytes = false ;
use_length = false ;
uniform_sample = false ;
break ;
case 'l':
min_length=atoi(argv[1]+2) ;
use_length = true ;
use_bytes = false ;
use_interval = false ;
uniform_sample = false ;
break ;
case 'L':
max_length=atoi(argv[1]+2) ;
use_length = true ;
use_bytes = false ;
use_interval = false ;
uniform_sample = false ;
break ;
case 'o':
output_file = argv[1]+2 ;
break ;
case 'r':
reject_file = argv[1]+2 ;
break ;
case 'R':
randomized = false ;
break ;
case 'u':
uniform_sample = true ;
use_bytes = false ;
use_interval = false ;
use_length = false ;
break ;
default:
cerr << "Unrecognized option " << argv[1] << endl << endl ;
usage(argv0) ;
break ;
}
argc-- ;
argv++ ;
}
size_t sample_size = 0 ;
if (use_length)
{
// ignore the rest of the commandline
}
else if (argc < 2)
{
usage(argv0) ;
}
else
{
sample_size = atoi(argv[1]) ;
}
StringList* lines = nullptr ;
StringList** lastline = &lines ;
COutputFile rejectfp(reject_file) ;
size_t numlines = 0 ;
CFile f(stdin) ;
COutputFile selectfp(output_file) ;
while (CharPtr line = f.getCLine())
{
if (use_length)
{
unsigned len = strlen(line) ;
select_line(len >= min_length && len <= max_length,selectfp,rejectfp,line) ;
}
else if (use_interval)
{
select_line(numlines % sample_size == 0,selectfp,rejectfp,line) ;
}
else
{
lines->append(line,lastline) ;
}
numlines++ ;
}
*lastline = nullptr ; // terminate list of lines
if (use_bytes)
{
take_uniform_bytes(lines,sample_size,selectfp,rejectfp) ;
}
else if (uniform_sample)
{
take_uniform_sample(lines,sample_size,selectfp,rejectfp) ;
}
else
{
if (randomized)
Randomize() ;
take_random_sample(lines,sample_size,selectfp,rejectfp) ;
}
return 0 ;
}