# Description:
#   This script provides a 'gist' translation of a text file, using the
#   i2e.sh utility. Really, the i2e.sh utility is not necessary since all
#   that that program does is look up a plain text dictionary file.
#   This script translates from English into Spanish. The idea is to
#   provide some assistance to a human translator, rather than to provide
#   a 'machine translation' (such as the AltaVista and Google Web Translators)
#
#   A gist translation really just provides suggestions for the translation of 
#   individual words. Generally this has to occur within some kind of 'editing
#   environment' which is jargon-speak for an computer text-editor or 
#   word-processor.
#
# Problems:
#   The script is very slow. It can only translate approximately 1 line of
#   text per second.
# Notes:
#
# Dependencies:
#   Some kind of Bash Shell
#   Unix Utilities
#     echo, cat, sed, tr, head
#   i2e
#    This is a program which includes a spanish/ english dictionary
#    and a C program as well as a BAsh shell script which is called 
#    i2e.sh  The program is available as a Debian package and probably as
#    an RPM
# Author:
#   m.j.bishop
#

 if [ "$1" = "" ]
 then
   echo "usage: $0 text-file-to-translate"
   cat $0 | sed -n "/^[ ]*#/p" 
   exit 1;
 fi

 for line in $(cat $1 | sed "/^[^a-zA-Z]*$/d" | tr ' ' '#');
 do
   s=$(echo $line | tr '#' ' ');
   echo $s;
   for v in $(echo $s | tr '[A-Z]' '[a-z]' | sed "s/[^a-z]/ /g");
   do
     i2e.sh -e $v | sed "/^ *$/d"  \
       | head -1 | sed "s/not found in.*/[???]/gi" \
       | sed "s/^[^:]*://" | sed 's/A (abrev\. de amperio)/uno/i' \
       | sed "s/^ */ /g" | sed "s/ *$/ /g"  \
       | tr '\n' '+';
   done;

   echo "";
   for v in $(echo $s | tr '[A-Z]' '[a-z]' | sed "s/[^a-z]/ /g");
   do
     i2e.sh -e $v | sed "/^ *$/d"  \
       | head -2 | tail -1 | sed "s/not found in.*/[???]/gi" \
       | sed "s/^[^:]*://" | sed 's/A (abrev\. de amperio)/uno/i' \
       | sed "s/^ */ /g" | sed "s/ *$/ /g"  \
       | tr '\n' '+';
   done;

   echo "";
   for v in $(echo $s | tr '[A-Z]' '[a-z]' | sed "s/[^a-z]/ /g");
   do
     i2e.sh -e $v | sed "/^ *$/d"  | head -3 | tail -1 \
       | sed "s/not found in.*/[???]/gi" \
       | sed "s/^[^:]*://" | sed 's/A (abrev\. de amperio)/uno/i' \
       | sed "s/^ */ /g" | sed "s/ *$/ /g"  \
       | tr '\n' '+' ;
   done;

   echo "";
   echo "==============================";
 done