#!/bin/bash
# Batch generate kaf-files from folia files
# Usage: ./generate_kaf.sh <dir with folia-files> <dir to save kaf-files in>
# 2014-09-10 j.vanderzwaan@esciencecenter.nl

echo ''
echo 'Generating kaf-files for FoLiA XML files in' $1
echo 'Saving kaf-files in' $2
echo ''

# Create output directory if it doesn't exist  
[[ -d "$2" ]] || mkdir "$2"

total=0
invalid=0
valid=0

shopt -s nullglob
for folia in $(find $1 -maxdepth 1 -type f); do
    total=$((total+1))
    
    echo "(${total}) ${folia}"
    
    # extract play id
    # example file name: /home/jvdzwaan/data/test_embem/feit007patr01_01.xml
    # extract 13 characters 20 characters from the end of the string
    play_id=${folia:(-20):13}
   
    # check folia file
    echo "Checking FoLiA XML..."
    # inspect_folia.py writes to stderr
    # 2>&1 redirects stderr to stdout 
    err_report=$(python inspect_folia.py $folia 2>&1)
    folia_ok=$?
    
    if [ $folia_ok -eq 0 ];then
        valid=$((valid+1))
        echo "Generating kaf-files..."
        
        # create directory for play
        play_dir="${2}/${play_id}"
        [[ -d "$play_dir" ]] || mkdir "$play_dir"

        python folia2kaf.py $folia $play_dir > /dev/null
    else
        invalid=$((invalid+1))
        echo "Invalid FoLiA file."
        
        # write id to text file
        error_dir="${2}/invalid"
        [[ -d "$error_dir" ]] || mkdir "$error_dir"

        error_ids_file="${error_dir}/ids.txt"
        touch $error_ids_file
        echo $play_id >> $error_ids_file
        
        # write error report to text file
        error_report_file="${error_dir}/${play_id}.txt"
        echo $err_report > $error_report_file
    fi
    echo ''
done

echo "Total: ${total} Valid: ${valid} Invalid: ${invalid}"

Add bash script to generate kaf files
Add bash script to generate kaf files

Added a bash script that generates kaf-files for each (FoLiA XML) file
in some directory. The script takes two arguments: 1) a path to the
directory containing the FoLiA XML files, and 2) a path to the
directory where the output should be saved. FoLiA XML files are first
checked using the inpect_folia script and, if found valid, kaf files
are created using the folia2kaf script. For invalid files, the id is
saved to a list and the error report from inspect_folia is saved to a
text file named <play_id>.txt.
