-
Notifications
You must be signed in to change notification settings - Fork 72
/
scigen.pm
433 lines (370 loc) · 9.75 KB
/
scigen.pm
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
package scigen;
# scigen.pl: SCIgen grammar engine
#
# Copyright (C) 2012 Nathaniel Eldredge
# This file is part of Mathgen.
#
# Adapted from scigen.pl from SCIgen
# (http://pdos.csail.mit.edu/scigen/). Portions may be copyright
# (C) the authors of SCIgen.
#
# Mathgen 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.
#
# Mathgen 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 Mathgen; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
use strict;
use IO::File;
use Data::Dumper;
#use Autoformat;
use vars qw($SCIGEND_PORT);
#### daemon settings ####
$SCIGEND_PORT = 4724;
sub dup_name {
my $name = shift;
return $name . "!!!";
}
sub file_name {
my $name = shift;
return $name . ".file";
}
sub read_rules {
my ($fh, $rules, $RE, $debug) = @_;
my $line;
while ($line = <$fh>) {
next if $line =~ /^#/ ;
next if $line !~ /\S/ ;
my @words = split /\s+/, $line;
my $name = shift @words;
my $rule = "";
# non-duplicate rule
if( $name =~ /([^\+]*)\!$/ ) {
$name = $1;
push @{$rules->{dup_name("$name")}}, "";
next;
}
# include rule
if( $name =~ /\.include$/ ) {
my $file = $words[0];
# make sure we haven't already included this file
# NOTE: this allows the main file to be included at most twice
if( defined $rules->{&file_name($file)} ) {
if( $debug > 0 ) {
print "Skipping duplicate included file $file\n";
}
next;
} else {
$rules->{&file_name($file)} = 1;
}
if( $debug > 0 ) {
print "Opening included file $file\n";
}
my $inc_fh = new IO::File ("<$file");
if( !defined $inc_fh ) {
die( "Couldn't open included file $file" );
}
&read_rules( $inc_fh, $rules, undef, $debug );
next; # we don't want to have .include itself be a rule
}
if ($#words == 0 && $words[0] eq '{') {
my $end = 0;
while ($line = <$fh>) {
if ($line =~ /^}[\r\n]+$/) {
$end = 1;
last;
} else {
$rule .= $line;
}
}
if (! $end) {
die "$name: EOF found before close rule\n";
}
} else {
$line =~ s/^\S+\s+//;
chomp ($line);
$rule = $line;
}
# look for the weight
my $weight = 1;
if( $name =~ /([^\+]*)\+(\d+)$/ ) {
$name = $1;
$weight = $2;
if( $debug > 10 ) {
warn "weighting rule by $weight: $name -> $rule\n";
}
}
do {
push @{$rules->{$name}}, $rule;
} while( --$weight > 0 );
}
if( defined $RE ) {
compute_re( $rules, $RE );
}
}
sub compute_re {
# must sort; order matters, and we want to make sure that we get
# the longest matches first
my ($rules, $RE) = @_;
my $in = join "|", sort { length ($b) <=> length ($a) } keys %$rules;
$$RE = qr/^(.*?)(${in})/s ;
}
sub generate {
my ($rules, $start, $RE, $debug, $pretty) = @_;
my $s = expand ($rules, $start, $RE, $debug);
if($pretty =~ /latex/) {
$s = pretty_print_latex($s, $pretty, $debug);
} elsif ($pretty eq 'bibtex') {
$s = pretty_print_bibtex($s);
}
return $s;
}
sub pick_rand {
my ($set) = @_;
my $n = $#$set + 1;
my $v = @$set[int (rand () * $n)];
return $v;
}
sub pop_first_rule {
my ($rules, $preamble, $input, $rule, $RE) = @_;
$$preamble = undef;
$$rule = undef;
my $ret = undef;
if ($$input =~ s/$RE//s ) {
$$preamble = $1;
$$rule = $2;
return 1;
}
return 0;
}
sub pretty_print_latex {
my ($s, $pretty, $debug) = @_;
my $news = "";
my @lines = split( /\n/, $s );
foreach my $line (@lines) {
$news .= "% $line\n" if ($debug);
my $newline = "";
# $line =~ s/(\s+)([\.\,\?\;\:])/$2/g;
$line =~ s/(\b)(a)((?:\s+)(?:\\[^\s\{]+\{)?(?:[aeiou]))/$1$2n$3/gi; # fix "a object"
if( $line =~ /(\\
(?:sci)?
(?:
(?:
(?:sub)?section\*?
)
|
(?:chapter)
|
(?:title)
)
)
\{(.*)\}/x ) {
my ($command, $title) = ($1, $2);
chomp $title;
chomp $title;
$title = my_entitle($title);
if ($pretty eq 'latexbook') {
#my $short_title = ($title =~ /^(.{35}\S*)/) ? "$1\\dots" : $title;
my $short_title = "\\truncate{0.75\\textwidth}{${title}}";
if ($command =~ /title/) {
$newline = "${command}{${title}}";
} elsif ($command =~ /chapter/) {
$newline = "${command}{${title}}\n\\chaptermark{${short_title}}";
} else {
$newline = "${command}[${short_title}]{${title}}"
}
} else {
$newline = "${command}{${title}}";
}
} elsif( $line =~ /\\index/ ) {
$newline = $line; # don't mangle index entries
} elsif( $line =~ /\S/ ) {
$newline = my_ensentence($line);
}
$newline =~ s/\\Em/\\em/g;
$newline =~ s/\s+([,.\-!?\';:])/$1/g; # fix "foo , bar"
$newline =~ s/-\s+/-/g; # fix "foo- bar"
# Replace "so then $$x=y$$." with "so then $$x=y.$$"
$newline =~ s/(\$\$|\\end\{align\*\})\s*([,.;:!?])/$2$1/g;
if( $newline !~ /\n$/ ) {
$newline .= "\n";
}
$news .= $newline;
}
return $news;
}
# Note that for names like "d'Alembert" the d' is not really part of the last name.
sub get_lastname {
my ($name) = @_;
$name =~ /([[:upper:]][^\s]*)\s*$/;
return $1;
}
# consider Lingua::EN::NameParse to do this properly
sub sort_authors {
my ($s) = @_;
# A few papers might have non-alphabetical authors
return $s if (rand() < 0.03);
my @authors = split(' and ', $s);
my @asort = sort {
get_lastname($a) cmp get_lastname($b)
||
$a cmp $b
} @authors;
return join(' and ', @asort);
}
sub pretty_print_bibtex {
my @lines = split( /\n/, $_[0] );
my $out;
foreach my $line (@lines) {
if ($line =~ /^(.*(?:title|journal)\s*=\s*\{)(.*)(\},\s*)$/) {
my ($first, $title, $last) = ($1,$2,$3);
$title =~ s/(\b)(a)\s+([aeiou])/$1$2n $3/gi; #fix "a object"
$title =~ s/\s+([,.\-!?\';:])/$1/g; # fix "foo , bar"
$title =~ s/-\s+/-/g; # fix "foo- bar"
$title = enbracket($title);
$title = my_entitle($title);
$line = $first . $title . $last;
}
# sort author lists
if ($line =~ /\s*author\s*=\s*\{(.*)\}/) {
my $authors = sort_authors($1);
$line = "author = { $authors },";
}
# Protect all math with brackets
$line =~ s/(\$.*?\$)/\{$1\}/g;
$out .= $line . "\n";
}
return $out;
}
# Protect caps, etc, with brackets
sub enbracket {
my $s = $_[0];
$s =~ s/(^|[\s\-])([[:upper:]])/$1\{$2\}/g;
$s =~ s/(\\[[:alpha:]]+(?:\{[^\s\}]*\})?)/\{$1\}/g;
return $s;
}
my %smallwords = map {$_=>1} qw {
a an at as and are
but by
ere
for from
in into is
of on or over
per
the to that than
until unto upon
via
with while whilst within without
};
sub my_entitle {
my ($s) = @_;
my $out;
my @words = split(/([\s\-]+)/, $s);
foreach my $i (0..@words-1) {
my $w = $words[$i];
if ($w =~ /^[[:lower:]]/ &&
($i == 0 || $i == @words-1 || !$smallwords{$w})) {
$out .= ucfirst($w);
} else {
$out .= $w;
}
}
return $out;
}
sub my_ensentence {
my ($s) = @_;
my $out;
my @words = split(/([\s\-]+)/, $s);
my $start = 1;
foreach my $i (0..@words-1) {
my $w = $words[$i];
# allow $ because a sentence could start with math
if ($w =~ /^[\$[:alpha:]]/ && $start) {
$out .= ucfirst($w);
$start = 0;
} else {
$out .= $w;
}
$start = 1 if ($w =~ /[.?!]$/);
}
return $out;
}
sub break_latex($$$) {
my ($text, $reqlen, $fldlen) = @_;
if( !defined $text ) {
$text = "";
}
return { $text, "" };
}
sub expand {
no warnings 'recursion';
my ($rules, $start, $RE, $debug) = @_;
# check for special rules ending in + and #
# Rules ending in + generate a sequential integer
# The same rule ending in # chooses a random # from among preiously
# generated integers
if( $start =~ /(.*)\+$/ ) {
my $rule = $1;
my $i = $rules->{$rule};
if( !defined $i ) {
$i = 0;
$rules->{$rule} = 1;
} else {
$rules->{$rule} = $i+1;
}
return $i;
} elsif( $start =~ /(.*)\#$/ ) {
my $rule = $1;
my $i = $rules->{$rule};
if( !defined $i ) {
$i = 0;
} else {
$i = int rand $i;
}
return $i;
}
my $full_token;
my $repeat = 0;
my $count = 0;
do {
my $input = pick_rand ($rules->{$start});
$count++;
if ($debug >= 5) {
warn "$start -> $input\n";
}
my ($pre, $rule);
my @components;
$repeat = 0;
while (pop_first_rule ($rules, \$pre, \$input, \$rule, $RE)) {
my $ex = expand ($rules, $rule, $RE, $debug);
push @components, $pre if length ($pre);
push @components, $ex if length ($ex);
}
push @components, $input if length ($input);
$full_token = join "", @components ;
my $ref = $rules->{dup_name("$start")};
if( defined $ref ) {
my @dups = @{$ref};
# make sure we haven't generated this exact token yet
foreach my $d (@dups) {
if( $d eq $full_token ) {
$repeat = 1;
}
}
if( !$repeat ) {
push @{$rules->{dup_name("$start")}}, $full_token;
} elsif( $count > 50 ) {
$repeat = 0;
}
}
} while( $repeat );
return $full_token;
}
1;