[LON-CAPA-cvs] cvs: modules /felicia radioprobs.pl
albertel
lon-capa-cvs@mail.lon-capa.org
Wed, 11 Sep 2002 21:47:15 -0000
albertel Wed Sep 11 17:47:15 2002 EDT
Added files:
/modules/felicia radioprobs.pl
Log:
- Felicia's script to generate LON-CAPA radio response problems from a question file and
answer key file.
- Might be nice to be able to run this in CSTR on an uploaded file to create many new radiobutton problems
Index: modules/felicia/radioprobs.pl
+++ modules/felicia/radioprobs.pl
#!/usr/bin/perl
###############################################################################
#
# Script to generate LON-CAPA radio response problems from a question file and
# answer key file. The script was originally written for ADV 2?? course and
# MSU. The script is ran with:
# perl makeradio.pl [PROBLEM_FILE] [ANSWER_FILE] [PROB_DIR]
# [PROBLEM_FILE] and [ANSWER_FILE] are formatted with the following
# characteristics:
# -[PROBLEM_FILE] was tab dileminated with the format:
# [tab]43.[tab]question-text[newline]
# [tab]A.[tab]foil-text[newline]
# [tab]B.[tab]foil-text[newline]
# ...
# [tab]44.[tab]question-text[newline]
# -[ANSWER_FILE] was tab dileminated with the format:
# [tab]43.[tab]A
# [tab]44.[tab]B
# Where A is the answer to 43 and B is the answer to 44.
# A file is created into [PROB-DIR] with the name prob-0043.problem (using the
# example above).
# Created by: Felicia Berryman
# felicia@lon-capa.org
# LITE Lab; Michigan State University
# Originally completed: Sept 7, 2002
# Last Modified: -----
#
# Crappiness that can be improved:
# -written explicity for the format given above - more flexibilty such as:
# *ability to replace [tab] delimination with user specified one.
# -foils are recoginized by line beginning with [tab] then A or B or ... J.
# -answer key file must be in the same order as the problem file
# -does not check if prob-0043 exists, just automatically overwrites.
# *should prompt user if files should be overwritten or ability to
# specify this option on the command line, for example --safe
#
###############################################################################
use strict;
# Check for proper command line specifications:
unless ($#ARGV == 2) {
die( "\nCorrect command usage:
perl makeradio.pl [PROBLEM_FILE] [ANSWER_FILE] [PROB_DIR]\n\n" );
}
my $probfile = $ARGV[0]; # user supplied [PROBLEM_FILE]
my $ansfile = $ARGV[1]; # user supplied [ANSWER_FILE]
my $probdir = $ARGV[2]; # user supplied directory where new radio response
# questions are to be created
my @question=(); # Holds input from $probfile and $ansfile.
# @question is an array of monster hashes that stores all
# elements necessary to create the radio response problems
my $index = -1; # indexes question while reading $probfile and $ansfile
# Read input from $probfile and input data into @question.
open( READ_PROB, "<$probfile")
or die( "Cannot open $probfile for reading: $!" );
while( <READ_PROB> ) {
my @dummy_string = split( /\t/, $_ );
next if (@dummy_string < 3); # skip blank line
if ( $dummy_string[1] =~ /\d/ ) {
$index++; # increment $index for each question number recognized.
# reason why $index begins at -1.
my $temp = $dummy_string[1];
chop($temp);
$question[$index]->{'number'}=$temp; #save question number for filename
$question[$index]->{'text'} = $dummy_string[2]; #save question text
}
elsif ( $dummy_string[1] =~ /A|B|C|D|E|F|G|H|I|J/ ) {
my $temp = $dummy_string[1];
chop($temp); # foil letter
# save foil text with foil letter as hash key.
$question[$index]->{'foils'}->{$temp}->{'ftext'} = $dummy_string[2];
# specify all foils as "false" for now.
$question[$index]->{'foils'}->{$temp}->{'answer'} = "false";
}
}
close( READ_PROB ) or die( "Cannot close $probfile: $!" );
# Read input from $ansfile and input data into @question.
$index=0; # reset index to begin at 0;
open( READ_ANS, "<$ansfile")
or die( "Cannot open $ansfile for reading: $!" );
while( <READ_ANS> ) {
my @dummy_string = split( /\t/, $_ );
next if (@dummy_string < 3); # skip blank lines
if ( $dummy_string[1] =~ /\d/ ) {
my $temp = $dummy_string[1];
chop($temp);
# check if answer file is listed in the same order as the question file
unless ( $temp == $question[$index]->{'number'} ) {
die( "$ansfile does not correspond in order to $probfile" );
}
}
if ( $dummy_string[2] =~ /A|B|C|D|E|F|G|H|I|J/ ) {
my $temp = $dummy_string[2]; # letter corresponding to correct foil
# specify the appropriate foil as "true".
$question[$index]->{'foils'}->{$temp}->{'answer'} = "true";
}
$index++;
}
close( READ_ANS ) or die( "Cannot close $ansfile: !" );
# Create .problem files with data from @question
for( my $i = 0; $i<=$#question; $i++ ) {
my $prob = sprintf("%04u",$question[$i]->{'number'});
$prob = "prob-".$prob.".problem\n";
open( PROB, ">$probdir/$prob" )
or die ( "Cannot create $prob. Check if $probdir exists or is
writable.\n Error: $!" );
print( PROB "<problem>\n\n\n" );
print( PROB "<!-- Autogenerated LON-CAPA problem -->\n" );
print( PROB "<!-- Autogeneration script written by: -->\n" );
print( PROB "<!-- F. Berryman, felicia\@lon-capa.org -->\n\n\n");
print( PROB "<startouttext />".($question[$i]->{'text'})."<endouttext />" );
print( PROB "\n\n<radiobuttonresponse max=\"10\">\n\n\t<foilgroup>\n\n" );
foreach my $key (keys( %{$question[$i]->{'foils'}} )) {
print( PROB "<foil name=\"foil".($key)."\" value=\"" );
print( PROB ($question[$i]->{'foils'}->{$key}->{'answer'})."\">" );
print( PROB "<startouttext />" );
print( PROB "$question[$i]->{'foils'}->{$key}->{'ftext'}" );
print( PROB "<endouttext />\n</foil>\n\n" );
}
print( PROB "\t</foilgroup>\n\n</radiobuttonresponse>\n\n</problem>" );
close( PROB ) or die( "Cannot close $probdir/$prob" );
}