File:  [LON-CAPA] / loncom / debugging_tools / parse_scantron.pl
Revision 1.1: download - view: text, annotated - select for diffs
Sat Apr 24 08:36:03 2004 UTC (20 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: version_2_9_X, version_2_9_99_0, version_2_9_1, version_2_9_0, version_2_8_X, version_2_8_99_1, version_2_8_99_0, version_2_8_2, version_2_8_1, version_2_8_0, version_2_7_X, version_2_7_99_1, version_2_7_99_0, version_2_7_1, version_2_7_0, version_2_6_X, version_2_6_99_1, version_2_6_99_0, version_2_6_3, version_2_6_2, version_2_6_1, version_2_6_0, version_2_5_X, version_2_5_99_1, version_2_5_99_0, version_2_5_2, version_2_5_1, version_2_5_0, version_2_4_X, version_2_4_99_0, version_2_4_2, version_2_4_1, version_2_4_0, version_2_3_X, version_2_3_99_0, version_2_3_2, version_2_3_1, version_2_3_0, version_2_2_X, version_2_2_99_1, version_2_2_99_0, version_2_2_2, version_2_2_1, version_2_2_0, version_2_1_X, version_2_1_99_3, version_2_1_99_2, version_2_1_99_1, version_2_1_99_0, version_2_1_3, version_2_1_2, version_2_1_1, version_2_1_0, version_2_12_X, version_2_11_X, version_2_11_5_msu, version_2_11_5, version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4, version_2_11_3_uiuc, version_2_11_3_msu, version_2_11_3, version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2, version_2_11_1, version_2_11_0_RC3, version_2_11_0_RC2, version_2_11_0_RC1, version_2_11_0, version_2_10_X, version_2_10_1, version_2_10_0_RC2, version_2_10_0_RC1, version_2_10_0, version_2_0_X, version_2_0_99_1, version_2_0_2, version_2_0_1, version_2_0_0, version_1_99_3, version_1_99_2, version_1_99_1_tmcc, version_1_99_1, version_1_99_0_tmcc, version_1_99_0, version_1_3_X, version_1_3_3, version_1_3_2, version_1_3_1, version_1_3_0, version_1_2_X, version_1_2_99_1, version_1_2_99_0, version_1_2_1, version_1_2_0, version_1_1_99_5, version_1_1_99_4, version_1_1_99_3, version_1_1_99_2, version_1_1_99_1, version_1_1_99_0, loncapaMITrelate_1, language_hyphenation_merge, language_hyphenation, bz6209-base, bz6209, bz5969, bz2851, PRINT_INCOMPLETE_base, PRINT_INCOMPLETE, HEAD, GCI_3, GCI_2, GCI_1, BZ5971-printing-apage, BZ5434-fox, BZ4492-merge, BZ4492-feature_horizontal_radioresponse, BZ4492-feature_Support_horizontal_radioresponse, BZ4492-Support_horizontal_radioresponse
- makes figuring out what is in a scantron file possible

use strict;
use IO::File;

sub get_scantron_config {
    my ($which) = @_;
    my $fh=IO::File->new('scantronformat.tab');
    my %config;
    #FIXME probably should move to XML it has already gotten a bit much now
    foreach my $line (<$fh>) {
	my ($name,$descrip)=split(/:/,$line);
	print ($name ."\n".$which."\n");
	if ($name ne $which ) { next; }
	chomp($line);
	my @config=split(/:/,$line);
	$config{'name'}=$config[0];
	$config{'description'}=$config[1];
	$config{'CODElocation'}=$config[2];
	$config{'CODEstart'}=$config[3];
	$config{'CODElength'}=$config[4];
	$config{'IDstart'}=$config[5];
	$config{'IDlength'}=$config[6];
	$config{'Qstart'}=$config[7];
	$config{'Qlength'}=$config[8];
	$config{'Qoff'}=$config[9];
	$config{'Qon'}=$config[10];
	$config{'PaperID'}=$config[11];
	$config{'PaperIDlength'}=$config[12];
	$config{'FirstName'}=$config[13];
	$config{'FirstNamelength'}=$config[14];
	$config{'LastName'}=$config[15];
	$config{'LastNamelength'}=$config[16];
	last;
    }
    return %config;
}

sub scantron_parse_scanline {
    my ($line,$whichline,$scantron_config)=@_;
    my %record;
    my $questions=substr($line,$$scantron_config{'Qstart'}-1);
    my $data=substr($line,0,$$scantron_config{'Qstart'}-1);
    if ($$scantron_config{'CODElocation'} ne 0) {
	if ($$scantron_config{'CODElocation'} < 0) {
	    $record{'scantron.CODE'}=substr($data,$$scantron_config{'CODEstart'}-1,
					    $$scantron_config{'CODElength'});
	} else {
	    #FIXME interpret first N questions
	}
    }
    $record{'scantron.ID'}=substr($data,$$scantron_config{'IDstart'}-1,
				  $$scantron_config{'IDlength'});
    $record{'scantron.PaperID'}=
	substr($data,$$scantron_config{'PaperID'}-1,
	       $$scantron_config{'PaperIDlength'});
    $record{'scantron.FirstName'}=
	substr($data,$$scantron_config{'FirstName'}-1,
	       $$scantron_config{'FirstNamelength'});
    $record{'scantron.LastName'}=
	substr($data,$$scantron_config{'LastName'}-1,
	       $$scantron_config{'LastNamelength'});
    my @alphabet=('A'..'Z');
    my $questnum=0;
    while ($questions) {
	$questnum++;
	my $currentquest=substr($questions,0,$$scantron_config{'Qlength'});
	substr($questions,0,$$scantron_config{'Qlength'})='';
	if (length($currentquest) < $$scantron_config{'Qlength'}) { next; }
	my @array=split($$scantron_config{'Qon'},$currentquest,-1);
	if (length($array[0]) eq $$scantron_config{'Qlength'}) {
	    $record{"scantron.$questnum.answer"}='';
	} else {
	    $record{"scantron.$questnum.answer"}=$alphabet[length($array[0])];
	}
 	if (scalar(@array) gt 2) {
 	    push(@{$record{'scantron.doubleerror'}},$questnum);
 	    my @ans=@array;
 	    my $i=length($ans[0]);shift(@ans);
	    while ($#ans) {
 		$i+=length($ans[0])+1;
 		$record{"scantron.$questnum.answer"}.=$alphabet[$i];
 		shift(@ans);
 	    }
 	}
    }
    $record{'scantron.maxquest'}=$questnum;
    return \%record;
}

sub print_record {
    my ($scan_record)=@_;
    print "Name: ".$scan_record->{'scantron.LastName'};
    print "  ID: ".$scan_record->{'scantron.ID'};
    print "  Paper ID: ".$scan_record->{'scantron.PaperID'};
    print "\n";
    for (my $i=1;$i<100;$i++) {
	if (!exists($scan_record->{"scantron.$i.answer"})) {
	    print "\n";
	    return;
	}
	if ($scan_record->{"scantron.$i.answer"} eq '') {
	    print " ";
	} else {
	    print $scan_record->{"scantron.$i.answer"};
	}
    }
}

open(FILE,'scantron.data');
my %scantron_config=&get_scantron_config('msunocode');
print(join(':',%scantron_config)."\n");
my $i=0;
while (my $line=<FILE>) {
    my $scan_record=&scantron_parse_scanline($line,$i,\%scantron_config);
    $i++;
    print_record($scan_record);
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>