#!/bin/bash #foma_exec=@FOMACLI@ foma_exec=/tmp/v15/bin/hfst_foma if [ ! -x $foma_exec ] ; then echo "$0 could not find executable foma from $foma_exec" exit 1 fi function print_usage() { echo "Usage: $0 [-h|-V] [-v|-s|-q] [-f FORMAT] [-F STACK_FILES] [SCRIPTS]" } function print_help() { echo "Uses foma to parse xfst scripts converts results to HFST3 format" echo echo " -h print this help text" echo " -V print script version" echo " -v print verbosely while processing" echo " -q do not print while processing" echo " -s alias of -q" echo " -f select output format as HFST3 supported format names" echo " -F list files which xfst script saves as stacks that can be converted to HFST3 format" echo " -X pass option string to foma verbatim" echo echo "if SCRIPTS and input parameters from X are missing, stdin is used." echo "Output is written to stdout." echo "If -f is omitted, HFST3 default is used" echo "If -F is omitted, we try to guess files to convert automatically" } function print_version() { echo "hfst-foma-wrapper 0.1" echo "Copyright (c) 2010 University of Helsinki" echo "Licence GPLv3: GNU GPL version 3 " echo "This is free software: you are free to change and redistribute it." echo "There is NO WARRANTY, to the extent permitted by law." } while getopts "vqshVf:F:X:" flag ; do case $flag in h) print_help; exit 0;; V) print_version; exit 0;; v) verbose="-v";; s|q) quiet="-q";; f) format="-f $OPTARG";; F) files="$OPTARG";; X) fomaopts="$OPTARG";; \?) echo "Unknown command line switch $OPTARG, try -h for help"; print_usage; exit 1;; esac done hfstopts="$quiet $verbose $format" if [ x$verbose != x-v -a x$quiet != x-q ] ; then fomaopts="$fomaopts -p" hfstopts=-v fi shift $(($OPTIND-1)) if [ -z $@ ] ; then if [ "x$verbose" == "x-v" ] ; then echo "running $foma_exec $fomaopts" fi if ! $foma_exec $fomaopts; then echo "$foma_exec failed" exit 1 fi fi for f in $@ ; do if [ "x$verbose" == "x-v" ] ; then echo "running $foma_exec $fomaopts -f $f" fi if ! $foma_exec $fomaopts -f "$f" ; then echo "$foma_exec -f $f failed" exit 1 fi done if [ -z $files ] ; then scripts="$(echo $fomaopts | sed 's/ -f //') $scripts" for f in $@ $scripts; do if test -f ./"$f" ; then if egrep -q '^(save stack|ss)' ./"$f" ; then files="$(egrep '^(save stack|ss)' < "$f" | sed -e 's/^save stack //' -e 's/^ss //') $files" fi fi done files=$(echo $files | tr -s ' ' '\n' | sort | uniq) if [ "x$verbose" == "x-v" ] ; then echo "discovered stack files: $files" fi fi if [ -z "$files" ] ; then echo "Unable to find saved stack files to convert..." echo "HFST-xfst can only use -f scripts with save stack command or" echo "-l scripts with save stack before interactive part" exit 1 fi for f in $files ; do if test -f "$f" ; then if [ "x$verbose" == "x-v" ] ; then echo "converting $f" fi printf "load stack $f\nwatt $f.att\nquit" > $f.tmpscript if ! $foma_exec -q -f $f.tmpscript ; then echo "converting $f from foma to text failed" exit 1 fi sed -e 's/ / @_TAB_@/g' -e 's/ / @_SPACE_@/g' < "$f".att > "$f".att2 mv "$f".att2 "$f".att if ! hfst-txt2fst -e '@0@' $verbose $hfstopts $format -i "$f.att" -o "$f" ; then echo "converting $f from text to HFST3 failed" exit 1 fi rm $f.tmpscript $f.att fi done # vim: set ft=bash