Updated Auto Grade Script

Not long after I posted the original autograding script, I figured out a way to colorize the output. My solution was html. Here’s the script in it’s final (for now) form.

EDIT – For some reason, WordPress really doesn’t like this script, even within the pre tags. If you want to use it, you’ll just have to do a search and replace to get rid of the &lt and &gt to replace them with the appropriate characters.

#!/bin/bash
WORKDIR=$1
KEYFILE=$2
GRADEDDIR=$WORKDIR/graded

# first check if there are two arguments:
if [ "$#" -ne 2 ]; then
    echo "usage: $0 &lt/path/to/files&gt &lt/path/to/answerkey.txt&gt"
    exit
fi

#checks to see if $GRADEDDIR exists and creates it if it does not
if [ ! -d "$GRADEDDIR" ]; then
        mkdir -p "$GRADEDDIR"
fi

#converts MS Word format to plain text
for UNGRADED in $WORKDIR/*.docx; do
docx2txt.sh $UNGRADED;
done

#compares text files against key and outputs to .GRADED.txt file
for GRADED in $WORKDIR/*.txt; do
dwdiff -s $KEYFILE $GRADED &gt "$WORKDIR/$(basename $GRADED .txt).GRADED"
done

#renames .GRADED file with information dwdiff writes to end of file
for RENAMED in $WORKDIR/*.GRADED; do
mv $RENAMED "$WORKDIR/$(basename $RENAMED .GRADED).$(grep "old:" $RENAMED).RENAMED"
done

#moves graded files to graded directory
for MOVED in $WORKDIR/*.RENAMED; do
mv "$MOVED" $GRADEDDIR/
done

#begin converting .RENAMED to .html
for COLORIZED in $GRADEDDIR/*.RENAMED; do
cat "$COLORIZED" | sed -e 's/\[-/&ltspan style=\"background-color:red\"&gt/g' -e 's/-\]/&lt\/span&gt/g' -e 's/{+/&ltspan style=\"background-color:yellow\"&gt/g' -e 's/+}/&lt\/span&gt/g' -e '1i&lthtml&gt\n&ltbody&gt\n&ltpre&gt' -e 's/old:/Answer Key:/' -e 's/new:/Your Document:/' &gt "$COLORIZED.html"
done

#finish converting .txt to .html
for HTMLEND in $GRADEDDIR/*.html; do
echo "&lt/pre&gt
&lt/body&gt
&lt/html&gt" &gt&gt '$HTMLEND'
done

#clean up the filenames a bit
rename 's/.old: /\-/' $GRADEDDIR/*.html
rename 's/.RENAMED././' $GRADEDDIR/*.html

#removes .txt files from $WORKDIR and .RENAMED files from $GRADEDDIR
rm $WORKDIR/*.txt
rm $GRADEDDIR/*.RENAMED

Leave a Reply

Your email address will not be published. Required fields are marked *