-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquestionwriter.pl
334 lines (274 loc) · 7.09 KB
/
questionwriter.pl
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
#!/usr/local/bin/perl -w
#use strict;
use Getopt::Long;
## Written by Max Bileschi, Spring 2011
## mlbileschi@gmail.com
## creates questions
#TODO optional character after regex, like comma or period. some answers not appearing
#update Feb 25: takes one OR two CL args, to write questions about a specific thing
#update Feb 26: takes CL option --qword, --numbers
#update Feb 27: looks at every number match in a sentence, not just the first. also changed --numbers to --years
die "wrong number of parameters from comand line \n
usage: executable <input text file> [options] \n OPTIONS:
--qword=<word you want a question about> (will be overriden by --years)\n
--years (will target years instead of text. Will override the qword option)\n"
unless ($#ARGV>=0);
my $infile=$ARGV[0];
open(INFILE, "<$infile") or die "Can't open infile $infile\n";
shift(@ARGV);
open(DICTIONARY, "<index.txt") or die "Can't open dicitonary file index.txt\n";
open(TIMEPREPS, "<time_preps.txt") or die "Can't find time preposition dictionary time_preps.txt\n";
my $qword; my $years;
GetOptions ("qword=s" => \$qword, "years" => \$years) or die "Whups, got options we don't recognize!";
$qword=lc($qword);
my @dict = <DICTIONARY>;
my %hdict=();
my %localfreq=();
my @file = <INFILE>;
my $total=0;
my @line = ();
foreach (@dict)
{
chomp;
@line = split(/ /, $_);
$hdict{$line[0]}=$line[1]/8382231; #key is word, value is freq
$total+=$line[1];
}
$total=0;
foreach(@file)
{
chomp;
@line = split(/ /, $_);
$total+=$#line+1;
foreach my $token (@line)
{
if($token =~ /^[A-Za-z]+[\.,]?$/)
{
chop($token) if ($token =~ /[\.,]+$/);
$token = lc($token);
if(exists($localfreq{$token}))
{
$localfreq{$token}++;
}
else
{
$localfreq{$token} = 1;
}
}
}
}
#compute relative frequencies
foreach my $key ( keys(%localfreq) )
{
if(!exists($hdict{$key})){ print "word $key is not in hdict\n"; } #possibly add it with a really high value?
else { $localfreq{$key}= ($localfreq{$key}/$total)/($hdict{$key}); }
}
#now read by sentence and create questions
my $wholefile = "";
#read file into sentences
foreach (@file)
{
chomp;
$wholefile.=$_;
}
my @topwords=();
foreach my $key (sort {$localfreq{$b} <=> $localfreq{$a}} keys(%localfreq)) {
# print "$key $localfreq{$key}\n";
push(@topwords, $key);
}
my $timeprepregex="";
if($years) #read in time prepositions
{
foreach(<TIMEPREPS>)
{
chomp;
$timeprepregex.="(".$_.")\|";
}
}
chop($timeprepregex); #take last "|" off
sub years
{
my @matches = ();
#POSSIBLE SPACE PROBLEMS HERE
if(($_ =~ $timeprepregex) && (@matches = $_=~m/[\s+,\(\-](\d+)[\.,\s+\-\)]?/g))# || $_=~/^[0-9]+\s/)
{
#foreach (@matches) { print $_; }
foreach $match (@matches)
{
print "correct answer: $match\n";
my @tokens = split(/\s+/, $_);
foreach my $word (@tokens)
{
if($word =~ $match)
{
#print " ".$` unless $` eq " "; #in case the number has brackets around it or something stupid
print "_______________";
print $'." " unless $' eq " "; #in case the number was followed by puncutation
}
else
{
print $word." ";
}
}
print "\n";
my %numberchoice=(); #hash of randoms chosen
my $correct = int(rand(5));
$numberchoice{$match}=0;
#make 4 random candidate numbers
#first
my $one=$match; my $two=$match; my $three=$match; my $four=$match;
my $MOST_TRIES=25;
if( 1)#$match>10 )
{
my $posneg = (-1)**int(rand(2));
while(exists($numberchoice{$one})) { $one = $match + $posneg*int(rand(sqrt($match*10))); $MOST_TRIES--; if($MOST_TRIES<=0) { $one = int(rand(100));} }
$numberchoice{$one}=0;
$posneg = (-1)**int(rand(2));
while(exists($numberchoice{$two})) { $two = $match + $posneg*int(rand(sqrt($match*5))); $MOST_TRIES--; if($MOST_TRIES<=0) { $two = int(rand(100));}}
$numberchoice{$two}=0;
$posneg = (-1)**int(rand(2));
while(exists($numberchoice{$three})) { $three = $match + $posneg*int(rand(sqrt($match*2))); $MOST_TRIES--; if($MOST_TRIES<=0) { $three = int(rand(100));}}
$numberchoice{$three}=0;
$posneg = (-1)**int(rand(2));
while(exists($numberchoice{$four})) { $four = $match + $posneg*int(rand(sqrt($match))); $MOST_TRIES--; if($MOST_TRIES<=0) { $four = int(rand(100));}}
$numberchoice{$four}=0;
}
my @pre = ( $match, $one, $two, $three, $four );
my @post = ();
for (my $i=5; $i>=1; $i--)
{
my $choice = int(rand($i));
push(@post, $pre[$choice]);
splice(@pre,$choice,1);
}
for my $j (1..5)
{
print "$j \. $post[$j-1]\n"; #print answers
}
}
}
}
sub qword
{
if(! exists( $localfreq{$qword} ) )
{
print "\n$qword is not in $infile\n\n";
last;
}
if(! exists( $hdict{$qword} ) )
{
print "\n$qword is not in index.txt\n\n";
last;
}
#if it's the first time, we have problems if it's not preceded by a space
if($_=~/\s+$qword[\.,\s+]?/i || $_=~/^$qword\s/i)
{
print "correct answer: $qword\n";
my @tokens = split(/\s+/, $_);
foreach my $word (@tokens)
{
if (!($word =~ /^$qword/i))
{
print $word." ";
}
else
{
print "___________________ ";
print $'." " unless $' eq " "; #in case the word was followed by puncutation
}
}
print "\n";
my %numberchoice=(); #hash of randoms chosen
my $correct = int(rand(5))+1;
my $index_in_topwords= grep { lc($topwords[$_]) eq $qword } 0..$#topwords;
$numberchoice{ $index_in_topwords }=0;
for my $j (1..5)
{
if($j==$correct)
{
print "$j \. $qword\n";
}
else
{
my $random=$correct;
while ( exists($numberchoice{$random}) || $topwords[$random] eq lc($qword) )
{
$random=int(rand(20)); #how far into @topwords i want to look for wrong answers
}
$numberchoice{$random}=0;
print "$j \. $topwords[$random]\n"; #print correct output
}
}
}
}
#default, i.e. if no command line parameters
sub default
{
#ten of top words
for my $i (0..10)
{
if(! exists( $hdict{$topwords[$i]} ) )
{
print "\n$topwords[$i] is not in index.txt\n\n";
next;
}
if($_=~/\s+$topwords[$i]\s/i)
{
print "correct answer: $topwords[$i]\n";
my @tokens = split(/\s+/, $_);
foreach my $word (@tokens)
{
if (!($word =~ /^$topwords[$i]/i))
{
print $word." ";
}
else
{
print "___________________ ";
print $'." " unless $' eq " "; #in case the word was followed by puncutation
}
}
print "\n";
my %numberchoice=(); #hash of randoms chosen
my $correct = int(rand(5))+1;
$numberchoice{$i}=0;
for my $j (1..5)
{
if($j==$correct)
{
print "$j \. $topwords[$i]\n";
}
else
{
my $random=$correct;
while ( exists($numberchoice{$random} ) )
{
$random=int(rand(20)); #how far into @topwords i want to look for wrong answers
}
$numberchoice{$random}=0;
print "$j \. $topwords[$random]\n"; #print correct output
}
}
}
}
}
my @sentences = split(/\./, $wholefile);
foreach(@sentences)
{
if($years)
{
&years
}
#find only specific questions
elsif($qword)
{
&qword;
}
#print questions about each of the top words
else
{
&default;
}
}
close(INFILE);
close(DICTIONARY);