-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeComparisonFromLists
More file actions
executable file
·547 lines (441 loc) · 13 KB
/
makeComparisonFromLists
File metadata and controls
executable file
·547 lines (441 loc) · 13 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
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
#!/usr/bin/env perl -w
#
# Make a table comparing several sorted files, record by record.
#
# Written 2007-?? by Steven J. DeRose.
# 2007-08-29 sjd: Hack from maketagtable.
# 2007-12-17 sjd: strict, -d, -b, get closer to working.
# 2009-08-16 sjd: Debug.
# 2009-08-20 sjd: Finish -fieldNum, -show. Better headings.
# 2012-10-05, 2013-12-16: Clean up.
#
# To do:
# Only show words that occur in one key version.
#
use strict;
use Getopt::Long;
use sjdUtils;
use alogging;
our $VERSION_DATE = "2013-12-16";
my $break = 0;
my $color = 1;
my $correlate = 0;
my $delim = "\\s+";
my $fieldNum = 0;
my $ignoreCase = 0;
my $listonly = 0;
my $max = 0;
my $min = 0;
my $normws = 0;
my $quiet = 0;
my $shorten = 1;
my $show = "x";
my $sort = 0;
my $sortOptions = "";
my $split = 0;
my $table = 1;
my $tickInterval= 1000;
my $verbose = 0;
# Process options
#
my %getoptHash = (
"break!" => \$break,
"color!" => \$color,
"correlate!" => \$correlate,
"delim=s" => \$delim,
"fieldnum=i" => \$fieldNum,
"h|help|?" => sub { system "perldoc $0"; exit; },
"i" => \$ignoreCase,
"b|normws!" => \$normws,
"listonly" => \$listonly,
"max=i" => \$max,
"min=i" => \$min,
"q|quiet!" => \$quiet,
"shorten!" => \$shorten,
"show=s" => \$show,
"sort!" => \$sort,
"split!" => \$split,
"table!" => \$table,
"tickInterval=o" => \$tickInterval,
"v|verbose+" => \$verbose,
"version" => sub {
die "Version of $VERSION_DATE, by Steven J. DeRose.\n";
},
);
Getopt::Long::Configure ("ignore_case");
GetOptions(%getoptHash) || die("Bad options.\n");
###############################################################################
# Validate and default options, set up files
#
my $newline = ($break) ? "\n":"";
my $showIsNumeric = 0;
if ($show =~ m/^\s*\d+\s*$/) {
$showIsNumeric = $show - 0;
($showIsNumeric > 0) || die "-show value invalid.\n";
}
else {
$show = lc(substr("$show ",0,1));
($show =~ m/[xr]/) || die "-show must be r, x, or a field number.\n";
}
(scalar(@ARGV) > 1) ||
die "Must specify at least 2 files.\n";
my @fhlist = ();
foreach my $f (@ARGV) {
($verbose) && warn "Setting up file number ".(scalar @fhlist) . ": '$f'\n";
my $ff = $f;
if ($sort) {
(my $fesc = $f) =~ s/(['" \\])/\\$1/g; # Protect shell
my $fout = $f;
$fout =~ s/^.*\///;
$fout =~ s/['"\\ ]/_/g;
$fout = "/tmp/$fout.sorted";
my $cmd = "sort $sortOptions <$fesc >$fout";
($verbose) && warn "Running: $cmd\n";
system "$cmd" || die "Sort failed.\n";
$ff = $fout;
}
local *F;
open(F, "<$ff") || die "Unable to open file '$ff'.\n";
my $t = *F;
push @fhlist, $t;
}
my $nfiles = scalar @fhlist;
($verbose) && warn "Opened $nfiles files.\n";
###############################################################################
###############################################################################
# Main
#
# Load up the first record from each file
#
my @pendingRecords = ();
my @recnum = ();
my @hasHitEOF = ();
my $buf;
for (my $i=0; $i<scalar @fhlist; $i++) {
my $curfh = $fhlist[$i];
if ($buf = <$curfh>) {
chomp $buf;
($verbose>1) && warn "First record of file $i is '$buf'.\n";
$pendingRecords[$i] = $buf;
$recnum[$i] = 1;
}
else {
($verbose>1) && warn "Hit EOF on file $i.\n";
$pendingRecords[$i] = "";
$hasHitEOF[$i]++;
}
}
# Run an n-way merge: Find the file with the smallest next item,
# then print a row showing that for all the files that have it.
writeHeader();
print "<tbody>\n";
my @corr = (); # index by $nfiles*f1 + f2.
my @uncorr = (); # index by $nfiles*f1 + f2.
my $rownum = 1;
my $prev = "";
my $small = smallest();
while ($small ne "") { # Watch out for "0" which might show up....
if ($tickInterval>0 && ($rownum%$tickInterval) == 0) {
warn "At word number $rownum\n";
}
($verbose>1) && warn "\nSmallest found to be '$small'\n";
if ($split && $prev ne "" && substr($prev,0,1) ne substr($small,0,1)) {
my $initial = substr($small,0,1);
print "</tbody></table>\n" .
"<h2 id=\"$initial\">$initial</h2>\n" .
"<table>\n<tbody>\n";
}
issueSmallest($small, $rownum);
$rownum++;
$prev = $small;
$small = smallest();
}
print "</tbody>\n</table>";
reportCorr();
($quiet) || warn "Done, $rownum rows.\n";
exit;
###############################################################################
###############################################################################
# Print the accumulated correlation table.
#
sub reportCorr {
if ($correlate) {
print "\n\n<h1>Pairwise vocabulary correlation:</h1>\n";
print "<table>\n";
print "<thead>\n";
print "<tr>\n";
print "<th>-</th>\n";
doFileNameHeaders();
print "</tr>\n";
print "</thead>\n\n";
print "<tbody>\n";
my $m = "";
for (my $i=0; $i<$nfiles; $i++) {
$m = sprintf("<tr><td>File %3d</td> ",$i);
for (my $j=0; $j<$nfiles; $j++) {
my $index = $i*$nfiles + $j;
my $tot = $corr[$index]+$uncorr[$index];
my $c = ($tot) ? ($corr[$index]*100.0/$tot) : 0.0;
my $attrs = ($color) ? colorAttr($c):"";
$m .= sprintf("<td$attrs>%6.3f</td>", $c);
} # j
print "$m</tr>\n";
} # i
print "</tbody>\n</table>\n";
} # corr
print "</body>\n</html>";
} # reportCorr
# Parameter comes in as a percentage; map to color value.
#
sub colorAttr {
my $c;
if ($_[0] < 33) {
my $n = 155 + 3 * int($_[0]);
$c = sprintf(" style=\"color:#%02x0000\"", 255);
}
elsif ($_[0] < 67) {
my $n = 155 + 3 * (int($_[0]-0.33));
$c = sprintf(" style=\"color:#%02x%02x00\"", 255, 255);
}
else {
my $n = 155 + 3 * int($_[0]-0.67);
$c = sprintf(" style=\"color:#00%02x00\"", 255);
}
return($c);
}
###############################################################################
# Write a full table row, with the record included for files for which it
# matches the smallest key, and nbsp elsewhere. Delete the issued records.
#
sub issueSmallest {
my $s = $_[0];
my $rownum = $_[1];
# For each file, show/advance the record if it matches $smallest.
my @appearsIn = ();
my $appearCount = 0;
my $buf = "";
for (my $i=0; $i<scalar @fhlist; $i++) {
if ($hasHitEOF[$i]) {
$buf .= "<td class=\"EOF\"> </td>$newline";
}
elsif (getKey($pendingRecords[$i],$i) eq $small) {
$appearsIn[$i]++; $appearCount++;
if ($showIsNumeric) {
my @tokens = split($delim,$pendingRecords[$i]);
$buf .= ((defined $tokens[$showIsNumeric])
? "<td>$tokens[$showIsNumeric]</td>"
: "<td>+++</td>") . $newline;
}
elsif ($show eq "x") {
$buf .= "<td>X</td>$newline";
}
else {
$buf .= "<td>$pendingRecords[$i]</td>$newline";
}
advanceRecord($i);
}
else {
$buf .= "<td> </td>$newline";
}
} # for
if ($max>0 && $appearCount>$max) { $buf=""; return; }
if ($min>0 && $appearCount<$min) { $buf=""; return; }
if ($correlate) {
for (my $i=0; $i<$nfiles; $i++) {
for (my $j=0; $j<$nfiles; $j++) {
my $index = $i*$nfiles + $j;
if ($appearsIn[$i]) {
if ($appearsIn[$j]) { $corr[$index]++; }
else { $uncorr[$index]++; }
}
else {
if ($appearsIn[$j]) { $uncorr[$index]++; }
else { } # neither -- irrelevant
}
} # j
} # i
} # corr
if ($table) {
print "<tr>$newline";
# print "<td>$rownum</td>$newline";
print "<td>'$small'</td>$newline";
print "<td>($appearCount)</td>$newline";
print $buf;
print "</tr>\n$newline";
}
} # issueSmallest
###############################################################################
# Find the smallest key in any of the files. We're always buffering in the
# pending line of each file.
#
sub smallest {
my $s = "~~~~~~~~~~~~";
for (my $i=0; $i<scalar @fhlist; $i++) {
my $key = getKey($pendingRecords[$i],$i);
($verbose>1) &&
warn "File $i has: '$pendingRecords[$i]', key '$key'\n";
if ($key lt $s) { $s = $key; }
}
return ($s);
} # smallest
###############################################################################
# Move file #n forward to next record.
#
sub advanceRecord {
my $i = $_[0];
my $buf;
my $curfh = $fhlist[$i];
if ($buf = <$curfh>) {
chomp $buf;
$pendingRecords[$i] = $buf;
$recnum[$i]++;
}
else {
($verbose>1) && warn "Hit EOF on file $i.\n";
$pendingRecords[$i] = "";
$hasHitEOF[$i]++;
}
} # advanceRecord
###############################################################################
# Extract the key from a string (generally a record)
#
sub getKey {
my $rec = $_[0];
my $filenum = $_[1];
my $rc = $rec;
if ($rc eq "") { return(""); }
$rc =~ s/^\s+//; # Prevent spurious 1st 'field'.
if ($fieldNum>0) {
my @fields = split($delim,$rc);
if (scalar @fields >= $fieldNum) {
$rc = $fields[$fieldNum-1];
}
else {
warn "Not enough fields in record $recnum[$filenum] "
. " of file $filenum ($ARGV[$filenum]). Need $fieldNum, got "
. scalar(@fields) . "):\n '$rec'\n";
for my $f (@fields) { warn " $fields[$f]\n"; }
die "Exit.\n";
return("");
}
}
if ($ignoreCase) {
$rc = lc($rc);
}
if ($normws) {
$rc =~ s/\s+/ /g;
$rc =~ s/^\s+//;
$rc =~ s/\s+$//;
}
return($rc);
} # getKey
###############################################################################
#
sub writeHeader {
print "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<title>Comprison</title>
<meta name=\"generator\" content=\"makeComparisonFromLists\" />
<style type=\"text/css\">
table { border:thin black solid; font-size:smallest;
border-collapse:collapse; }
tr { border:thin black solid; }
td { border:thin black solid; width:0.9in; }
th { border:thin black solid; width:0.9in; }
</style>
</head>
<body>
<table>
";
($table) || return;
print "
<thead>
<tr>
<th>Word</th>
<th>Occ. in</th>
";
doFileNameHeaders();
print "</tr>\n";
print "</thead>\n\n";
} # writeHeader
sub doFileNameHeaders {
my $i = 0;
foreach my $f (@ARGV) {
$i++;
my $short = $f;
if ($shorten) {
$short =~ s/\..*$//;
$short =~ s/ ?Translat(ion|ed)//;
$short =~ s/New Testament/NT/;
$short =~ s/Old Testament/OT/;
$short =~ s/ ?Version//;
$short =~ s/The //;
}
print "<th>$short<br />($i)</th>\n";
}
}
###############################################################################
###############################################################################
###############################################################################
#
=pod
=head1 Usage
makeComparisonFromLists [options] [files]
Merges all the files, so each becomes a column in an HTML table.
The files must be sorted already. Matching entries go in same row.
You can merge only on a certain field, so for example, you can use
files with frequency counts, such as produced by 'uniq -c'.
=head1 Options
(negate option with 'no' prefix when applicable):
=over
=item * B<--break>
Put each C<tr> and C<td> on a separate line.
=item * B<--color>
Colorize the correlation table (default).
=item * B<--correlate>
Calculate pairwise vocabulary correlations (slow).
=item * B<--delim> I<expr>
Field-delimiter regex (default '$dft_delim').
=item * B<-i>
Ignore case (must sort that way first).
=item * B<-b>
Normalize whitespace before comparing.
=item * B<-f> I<n>
Use field number I<n>.
=item * B<--max> I<n>
Only show words that appear in at least this many sources.
=item * B<--min> I<n>
Only show words that appear in at most this many sources.
=item * B<--quiet> OR B<-q>
Suppresses most messages.
=item * B<--shorten>
Try to abbreviate column headers.
=item * B<--show> I<s>
What to put in each cell:
x (default): just a check for version that have it.
n (number): the specified field from the input record,
as for 'uniq -c' files: '-f 2 -show 1'
r: The whole record.
=item * B<--sort>
Sort each file before merge (only for Latin-1 charset).
=item * B<--split>
Make each initial letter a separate table.
=item * B<--tick> I<n>
Issue progress message after every I<n> words.
=item * B<--verbose> OR B<-v>
Add extra messages (repeatable).
=item * B<--version>
Show version info and exit.
=back
=head1 Known bugs and limitations
Comparison is always string, not numeric.
=head1 Related commands
C<maketagtable> -- does similar job, but on element lists in DTDs.
=head1 Ownership
This work by Steven J. DeRose is licensed under a Creative Commons
Attribution-Share Alike 3.0 Unported License. For further information on
this license, see L<http://creativecommons.org/licenses/by-sa/3.0/>.
For the most recent version, see L<http://www.derose.net/steve/utilities/>.
=cut