#! /bin/sh
# ========================================
# 08 may 1996.  small changes in help added
# ========================================

naam=$1
drempel=1
field=0
count=0
only=0

if [ -z $naam ] ; then naam="-h"; fi

if [ $naam = '-h' ] ; then
   echo "Usage       : word_sel [-h] | [[-c | -n [num]] | -f [field]] filename]"
   echo "   filename : Crosstab of index-file in the format"
   echo "              concept weight_file1 weight_file2 ... weight_filen"
   echo "              (can be produced by the 'matrix'-script)"
   echo "  -c        : count number of files in which word occurs"
   echo "  -n [num]  : number of files in which word occurs (default=1)"
   echo "  -f [file#]: number of file to select words from (default=1)"
   echo "  -o [file#]: words that occur only in that file and no other files"
   echo "  -h        : this message"
   echo "Copyright Hans Paijmans 1995"
   exit 1
fi


if [ $naam = '-c' ] ; then count=1; naam=$2 ; drempel=0;
fi

if [ $naam = '-n' ] ; then
   if [ $2 -gt 0 ] ; then drempel=$2; naam=$3 ; else drempel=1; naam=$2
   fi
fi

if [ $naam = '-f' ] ; then
   if [ $2 -gt 0 ] ; then field=$2; naam=$3 ; else field=1; naam=$2 ;
   fi
drempel=0;
fi

if [ $naam = '-o' ] ; then
   if [ $2 -gt 0 ] ; then only=$2; naam=$3 ; else only=1; naam=$2 ;
   fi
drempel=0;
fi

awk '{
     x=0;
     for (n=2;n<=NF;n++) 
             {
             if ($n!="0") x++;
             if (f) if (n==f+1) print $1, $n;
             if (o) if (n==o+1) effe=$n;
             }
     
     if (d) if (x==d) print $0;
     if (c) print $1, x
     if (o) if (x==1) if (effe!=0) print $1, effe; 
     }' d=$drempel f=$field c=$count o=$only $naam






