forked from strib/scigen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scigen.pm
315 lines (264 loc) · 6.82 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
package scigen;
# This file is part of SCIgen.
#
# SCIgen 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.
#
# SCIgen 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 SCIgen; 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 ) {
$s = pretty_print($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 {
my ($s) = shift;
my $news = "";
my @lines = split( /\n/, $s );
foreach my $line (@lines) {
my $newline = "";
$line =~ s/(\s+)([\.\,\?\;\:])/$2/g;
$line =~ s/(\b)(a)\s+([aeiou])/$1$2n $3/gi;
if( $line =~ /\\section(\*?){(.*)}/ ) {
$newline = "\\section${1}{" .
Autoformat::autoformat( $2, { case => 'highlight',
squeeze => 0 } );
chomp $newline;
chomp $newline;
$newline .= "}";
} elsif( $line =~ /(\\subsection){(.*)}/ or
$line =~ /(\\slideheading){(.*)}/ ) {
$newline = $1 . "{" .
Autoformat::autoformat( $2, { case => 'highlight',
squeeze => 0 } );
chomp $newline;
chomp $newline;
$newline .= "}";
} elsif( $line =~ /\\title{(.*)}/ ) {
$newline = "\\title{" .
Autoformat::autoformat( $1, { case => 'highlight',
squeeze => 0 } );
chomp $newline;
chomp $newline;
$newline .= "}";
} elsif( $line =~ /(.*) = {(.*)}\,/ ) {
my $label = $1;
my $curr = $2;
# place brackets around any words containing capital letters
$curr =~ s/\b([^\s]*[A-Z]+[^\s\:]*)\b/\{$1\}/g;
$newline = "$label = {" .
Autoformat::autoformat( $curr, { case => 'highlight',
squeeze => 0 } );
chomp $newline;
chomp $newline;
$newline .= "},";
} elsif( $line =~ /\S/ ) {
$newline =
Autoformat::autoformat( $line, { case => 'sentence',
squeeze => 0,
break => break_latex(),
ignore => qr/^\\/ } );
}
$newline =~ s/\\Em/\\em/g;
if( $newline !~ /\n$/ ) {
$newline .= "\n";
}
$news .= $newline;
}
return $news;
}
sub break_latex($$$) {
my ($text, $reqlen, $fldlen) = @_;
if( !defined $text ) {
$text = "";
}
return { $text, "" };
}
sub expand {
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;