home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume12 / cake / part09 / Script / fix < prev    next >
Encoding:
Text File  |  1987-10-15  |  5.6 KB  |  293 lines

  1. #! /bin/sh
  2. # fix    Ver. 2.0, 20/1/86
  3. # interactive spelling checker and fixer
  4. #   Rex Sanders, USGS Pacific Marine Geology
  5. #   Modifications by Bill Silvert, MEL
  6. #   Modifications by Ed Reeder, Intel
  7. #   Rewritten by Bill Ross and Zoltan Somogyi, University of Melbourne
  8. #
  9. # usage:
  10. #     fix [-d dict] [-l local] [-p pnouns] [-s] [-a] [-b] [-o out] [-i in] files
  11. #        -d use dict as your private dictionary
  12. #            this is the only dictionary that gets uppercase
  13. #            folded to lower (default $HOME/lib/dict/words)
  14. #            the directory $HOME/lib/dict MUST exist
  15. #        -p use pnouns as the dictionary of proper nouns
  16. #                    (default=$HOME/lib/dict/proper-nouns)
  17. #        -l use local as the dictionary specific to this paper
  18. #                    (default=./.words)
  19. #        -a use american spelling
  20. #        -b use british spelling (default)
  21. #        -s don't assume the dictionaries are sorted
  22. #           by default we assume dict, local and pnouns
  23. #           are all sorted, and dict is all lower case,
  24. #           because sorting takes time.
  25. #        -o write the mistakes to a file, and exit
  26. #        -i read mistakes in from a file instead of running spell
  27. #
  28. # to come:
  29. #        preservation of case for beginning of sentence
  30. #        allow >1 word to be typed for a correction
  31. #            (at the moment it creates a file of the name of
  32. #             the second word---weird!)
  33. # bugs:
  34. #        painfully slow on substitutions
  35.  
  36. mis=/tmp/spf$$.mis
  37. sub=/tmp/spf$$.sub
  38. mrk=/tmp/spf$$.mrk
  39. tmp=/tmp/spf$$.tmp
  40. FILES=
  41. SORTED=
  42. SPELLOPT="-b"
  43. OUTFILE=
  44. INFILE=
  45. WORDS=$HOME/lib/dict/words
  46. PNOUNS=$HOME/lib/dict/proper-nouns
  47. LWORDS=./.words
  48. SPELLEDITOR=${SPELLEDITOR-'vi +/###/'}
  49. WJRBIN=/u/hons/wjr/bin
  50.  
  51. trap "/bin/rm -f $mis $tmp $sub $mrk ; exit" 0 1 2 15
  52.  
  53. while test -n "$1"
  54. do
  55.     case $1 in
  56.     -d)    shift
  57.         WORDS=$1
  58.         ;;
  59.     -l)    shift
  60.         LWORDS=$1
  61.         ;;
  62.     -p)    shift
  63.         PNOUNS=$1
  64.         ;;
  65.     -a)    SPELLOPT=
  66.         ;;
  67.     -b)    SPELLOPT="-b"
  68.         ;;
  69.     -s)    SORTED="n"
  70.         ;;
  71.     -i)    shift
  72.         INFILE=$1
  73.         ;;
  74.     -o)    shift
  75.         OUTFILE=$1
  76.         ;;
  77.     *)    FILES="$FILES $1"
  78.         ;;
  79.     esac
  80.     shift
  81. done
  82.  
  83. if test -n "$INFILE"
  84. then
  85.     echo "Reading mistakes from $INFILE for corrections to $FILES"
  86.     cp $INFILE $mis
  87. else
  88.     echo "Looking for spelling errors in $FILES ..."
  89.  
  90.     touch $LWORDS        # we remove this at the end if it wasn't used
  91.  
  92.     if test "$SORTED"
  93.     then
  94.         # -s option - force a sort of the dictionaries
  95.         if test ! -s $WORDS
  96.         then
  97.             touch $WORDS
  98.         else
  99.             tr A-Z a-z < $WORDS > $tmp
  100.             sort -u -f $tmp -o $WORDS
  101.         fi
  102.  
  103.         if test ! -s $LWORDS
  104.         then
  105.             touch $LWORDS
  106.         else
  107.             sort -u  $LWORDS -o $LWORDS
  108.         fi
  109.  
  110.         if test ! -s $PNOUNS
  111.         then
  112.             touch $PNOUNS
  113.         else
  114.             sort -u  $PNOUNS -o $PNOUNS
  115.         fi
  116.     fi
  117.  
  118.     # ignore numbers, but keep alpha-numerics
  119.     /usr/bin/spell -b $FILES | egrep -v '^[0-9]+$' > $tmp
  120.     # remove the possessive apostrophe
  121.     if egrep \'s\$ $tmp > /dev/null
  122.     then
  123.         $WJRBIN/gres \'s\$ "" $tmp
  124.     fi
  125.     
  126.     sort -u $tmp -o $mis    # must be sorted for comm
  127.  
  128.     # remove the words we know about
  129.     comm -23 $mis $PNOUNS | comm -23 - $LWORDS | comm -23 - $WORDS > $tmp
  130.     # now get rid of words in with (some) upper case that appear in words
  131.     egrep -v [A-Z] $tmp > $mis
  132.     for word in `egrep [A-Z] $tmp`
  133.     do
  134.         if grep -i -w $word $WORDS > /dev/null
  135.         then
  136.             echo -n    # a nop ! (continue does the wrong thing)
  137.         else
  138.             echo $word >> $mis
  139.         fi
  140.     done
  141.     sort -u $mis -o $mis
  142. fi
  143.  
  144. if test ! -s $mis
  145. then
  146.     echo "No spelling errors found in $FILES"
  147.     /bin/rm -f $mis $sub $mrk $tmp
  148.     if test ! -s $LWORDS
  149.     then
  150.         /bin/rm -f $LWORDS
  151.     fi
  152.     exit 0
  153. fi
  154.  
  155. if test -n "$OUTFILE"
  156. then
  157.     mv $mis $OUTFILE
  158.     /bin/rm -f $mis $sub $mrk $tmp
  159.     if test ! -s $LWORDS
  160.     then
  161.         /bin/rm -f $LWORDS
  162.     fi
  163.     exit 0
  164. fi
  165.  
  166. echo "Found `/usr/ucb/wc -l < $mis` misspellings"
  167. echo "Responses:"
  168. echo "g=add to user (global) dictionary"
  169. echo "p=add to user (global) dictionary of proper-nouns"
  170. echo "l=add to user (local) dictionary"
  171. echo "f=use a regular expression to look for correct spelling"
  172. echo "m=mark for correction, b=bypass"
  173. echo "Anything else is the correction"
  174.  
  175. for word in `cat $mis`
  176. do
  177.     egrep $word $FILES
  178.     while true
  179.     do
  180.         echo -n "${word}: [correction or bglpfm] "
  181.         read response
  182.         case $response in
  183.         G|g)
  184.             echo $word >> $WORDS
  185.             ##echo adding $word to global dictionary
  186.             addedg=y
  187.             break 
  188.             ;;
  189.         P|p)
  190.             echo $word >> $PNOUNS
  191.             ##echo adding $word to proper-noun dictionary
  192.             addedp=y
  193.             break 
  194.             ;;
  195.         L|l)
  196.             echo $word >> $LWORDS
  197.             ##echo adding $word to local dictionary
  198.             addedl=y
  199.             break 
  200.             ;;
  201.         ""|B|b)
  202.             ##echo bypass on $word
  203.             break
  204.             ;;
  205.         F|f)
  206.             echo -n "Search pattern for word: "
  207.             read response
  208.             if test "$response"
  209.             then
  210.                 cat $PNOUNS $LWORDS $WORDS | egrep "$response" > $tmp
  211.                 egrep "$response" /usr/dict/words | sort -f - $tmp | more
  212.             fi
  213.             ;;
  214.         M|m)
  215.             echo "$WJRBIN/grei \"\\<${word}\\>\" \"### spell: ${word} %%%\" $FILES" >> $mrk
  216.             ##echo marking $word
  217.             break
  218.             ;;
  219.         *)    
  220.             echo "$WJRBIN/gres \"\\<${word}\\>\" ${response} $FILES" >> $sub
  221.             ##echo changing $word to $response
  222.             break
  223.             ;;
  224.         esac
  225.     done
  226. done
  227.  
  228. if test ${addedg-n} = y
  229. then
  230.     tr A-Z a-z < $WORDS > $tmp
  231.     sort -u -f $tmp -o $WORDS
  232. fi
  233. if test ${addedl-n} = y
  234. then
  235.     sort -u $LWORDS -o $LWORDS
  236. elif test ! -s $LWORDS
  237. then
  238.     /bin/rm -f $LWORDS
  239. fi
  240. if test ${addedp-n} = y
  241. then
  242.     sort -u $PNOUNS -o $PNOUNS
  243. fi
  244.  
  245. if test ! -s $sub -a ! -s $mrk
  246. then
  247.     /bin/rm -f $mis $sub $mrk $tmp
  248.     exit 0
  249. fi
  250.  
  251. while true
  252. do
  253.     echo -n "Do you want to overwrite $FILES [yn] "
  254.     read reply
  255.     case "$reply" in
  256.     y*|Y*)
  257.         break
  258.         ;;
  259.     n*|N*)
  260.         /bin/rm -f $mis $sub $mrk $tmp
  261.         exit 0
  262.         ;;
  263.     *)
  264.         continue
  265.         ;;
  266.     esac
  267. done
  268.  
  269. if test -s $sub
  270. then
  271.     echo -n "Substituting.... "
  272.     . $sub
  273.     echo "done."
  274. fi
  275.  
  276. if test -s $mrk
  277. then
  278.     echo -n "Marking.... "
  279.     . $mrk
  280.     echo "done."
  281.     mod=""
  282.     for file in $FILES
  283.     do
  284.         if fgrep '###' $file > /dev/null
  285.         then
  286.             mod="$mod $file"
  287.         fi
  288.     done
  289.     $SPELLEDITOR $mod
  290. fi
  291.  
  292. /bin/rm -f $mis $sub $mrk $tmp
  293.