summaryrefslogtreecommitdiffstats
path: root/ubuntu/maverick/tdeedu/debian/keduca-shrinker
blob: 188b3e0ef264f591a2dde4762d18c97e7c17fafa (plain)
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
#!/usr/bin/perl
#
# Copyright (c) 2005 Mathieu Roy <yeupou--gnu.org>
# http://yeupou.coleumes.org
#
#   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; either version 2 of the License, or
#   (at your option) any later version.
#
#   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
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
#   USA
#
# $Id: keduca-shrinker.pl,v 1.3 2005/04/23 11:30:50 yeupou Exp $

use Getopt::Long;

my $getopt;
my $debug; 
my $help;

my $input;
my $output;
my $number = "20";


########### Get options, give help

eval {
    $getopt = GetOptions("debug" => \$debug,
			 "help" => \$help,
			 "number=s" => \$number,
			 "input=s" => \$input,
			 "output=s" => \$ouput);
};

if ($help) {
    print STDERR <<EOF;
Usage: $0 [OPTION] --input [FILE] 

A small program that take a kdeduca test file (.edu) as input with say 100
question and output a kdeduca test file with 20 questions selected randomly. 

    -i, --input=FILE          Input keduca file.
    -o, --output=FILE         Output keduca file, shrinked.
                              (By default, the suffix -shrinked will be added)
    -n, --number=NUMBER       Number expected of questions, in the shrinked
                              version.
                              ($number by default)
 
Project Homepage: https://gna.org/projects/keduca-shrinker/
EOF
exit(1);
}

# Test input file existence
unless ($input) {
    print "No input file.\n";
}
unless (-r $input) {
    print "Input file not readable.\n";
    exit;
}
open(INPUT, "< $input");

# Test output writability
unless ($output) {
    $output = $input;
    $output =~ s/.edu$//;
    $output .= "-shrinked.edu";
}
if (-e $output && ! -w $output) {
    print "Output file not writable.\n";
    exit;

}
open(OUTPUT, "> $output");

########### Define subs

sub fisher_yates_shuffle {
    my $table = shift;
    my $i;
    for ($i = @$table; --$i;) {
	my $j = int rand($i+1);
	next if $i == $j;
	@$table[$i,$j] = @$table[$j,$i];
    }
}


########### Grab the file header, store questions in an array.
# I know, it's XML, it may be simple to call an xml parser.
# But in fact, we have nothing to parse here, we do not care about
# the real content, so...
my $structure = "header";
my $header;
my $footer;
my @questions;
my $newquestion;

while (<INPUT>) {
    ## Grab the structure (footer and header)
    # the header last when data begins
    # the footer begin when data ends

    $header .= $_ if $structure eq "header";

    $structure = "content" if /\<Data\>/m;
    $structure = "footer" if /\<\/Data\>/m;

    $footer .= $_ if $structure eq "footer";

    ## Grab the questions
    if ($structure eq "content") {
	$newquestion .= $_;
	
	# If we found the string </question>, that the end of a question
	if (/\<\/question\>/m) {
	    push(@questions, $newquestion);
	    $newquestion = "";
	}
	
    }
}

########### Select the number of questions we want
# warn the user if there's nothing to do
if (scalar(@questions) < $number) {
    print "There are only ".scalar(@questions)." questions in the input file, less than $number.\n";
    # Copy & exit
    system("cp", $input, $output);
    exit;
} else {
    # Shuffle
    fisher_yates_shuffle(\@questions);
    # Keeps only the desired amount (number-1, as 0 is counted)
    $#questions = ($number-1);
}

########### Final output
print OUTPUT $header;
print OUTPUT @questions;
print OUTPUT $footer;

close(INPUT);
close(OUTPUT);