home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d191 / ispell.lha / ISpell / unix.zoo / munchlist.bug < prev    next >
Text File  |  1989-02-22  |  7KB  |  220 lines

  1. #!/bin/sh
  2. #
  3. # Works correctly (where foo has these four words, one per line):
  4. # ---------------
  5. # % args "conformer" "conformers" "conformer/S" "test" | munchlist
  6. #
  7. # % cat foo | munchlist
  8. #
  9. # Doesn't work correctly:
  10. # -----------------------
  11. # % munchlist
  12. # conformer
  13. # conformers
  14. # conformer/S
  15. # test
  16. # *** EOF ***
  17. #
  18. # % munchlist foo
  19. #
  20. # % munchlist <foo
  21. #
  22. #
  23. #
  24. # Here's the munchlist file, "traced":
  25.  
  26. #
  27. #    Given a list of words for ispell, generate a reduced list
  28. #    in which all possible suffixes have been collapsed.  The reduced
  29. #    list will match the same list as the original.
  30. #
  31. #    Usage:
  32. #
  33. #    munchlist [ -d hashfile ] [ -e ] [ -w chars ] [ file ] ...
  34. #
  35. #    Options:
  36. #
  37. #    -d hashfile
  38. #        Remove any words that are covered by 'hashfile'.  The
  39. #        default is the default ispell dictionary.  The words
  40. #        will be removed only if all suffixes are covered by
  41. #        the hash file.  A hashfile of /dev/null should be
  42. #        specified when the main dictionary is being munched.
  43. #    -e    Economical algorithm.  This will use much less temporary
  44. #        disk space, at the expense of time.  Useful with large files
  45. #        (such as complete dictionaries).
  46. #    -w    Passed on to ispell (specify chars that are part of a word)
  47. #
  48. #    The given input files are merged, then processed by 'ispell -c'
  49. #    to generate possible suffix lists;  these are then combined
  50. #    and reduced.  The final result is written to standard output.
  51. #
  52. #    For portability to older systems, I have avoided getopt.
  53. #
  54. #        Geoff Kuenning
  55. #        2/28/87
  56. #
  57. LIBDIR=//leo/yale/ram/emacs/ispell
  58. COMBINE=${LIBDIR}/icombine
  59. EXPAND1=${LIBDIR}/isexp1.sed
  60. EXPAND2=${LIBDIR}/isexp2.sed
  61. EXPAND3=${LIBDIR}/isexp3.sed
  62. EXPAND4=${LIBDIR}/isexp4.sed
  63.  
  64. # TDIR=${TMPDIR:-/usr/tmp}
  65. TDIR=/tmp
  66. TMP=${TDIR}/munch$$
  67.  
  68. cheap=no
  69. dictopt=
  70. wchars=
  71. while [ $# != 0 ]
  72. do
  73.     case "$1" in
  74.     -d)
  75.         case "$2" in
  76.         /dev/null)
  77.             dictopt=NONE
  78.             ;;
  79.         *)
  80.             dictopt="-d $2"
  81.             ;;
  82.         esac
  83.         shift
  84.         ;;
  85.     -e)
  86.         cheap=yes
  87.         ;;
  88.     -w)
  89.         wchars="-w $2"
  90.         shift
  91.         ;;
  92.     *)
  93.         break
  94.     esac
  95.     shift
  96. done
  97. trap "/bin/rm -f ${TMP}*; exit 1" 1 2 15
  98. #
  99. # Collect all the input and expand all the suffix options (four sed's),
  100. # and preserve (sorted) for later joining in ${TMP}a.
  101. #
  102. if [ $# -eq 0 ]
  103. then
  104.     sed -f $EXPAND1 | sed -f $EXPAND2 \
  105.       | sed -f $EXPAND3 | sed -f $EXPAND4 | sort -u > ${TMP}a
  106. else
  107.     sed -f $EXPAND1 "$@" | sed -f $EXPAND2 \
  108.       | sed -f $EXPAND3 | sed -f $EXPAND4 | sort -u > ${TMP}a
  109. fi
  110.  
  111. args "" "TMPa" "--"; cat ${TMP}a; args "--"
  112.  
  113. #
  114. # Unless an explicitly null dictionary was specified, remove all
  115. # expanded words that are covered by the dictionary.  This produces
  116. # the final list of expanded words that this dictionary must cover.
  117. # Leave the list in ${TMP}b.
  118. #
  119. if [ "X$dictopt" = "XNONE" ]
  120. then
  121.     ln ${TMP}a ${TMP}b
  122. else
  123.     ispell -l $dictopt -p /dev/null < ${TMP}a > ${TMP}b
  124. fi
  125.  
  126. args "" "TMPb" "--"; cat ${TMP}b; args "--"
  127.  
  128. #
  129. # Munch the input to generate roots and suffixes (ispell -c).  We are
  130. # only interested in words that have at least one suffix (egrep /);  the
  131. # next step will pick up the rest.  Some of the roots are illegal.  We
  132. # use join to restrict the output to those root words that are found
  133. # in the original dictionary.  In cheap mode, we re-sort this for
  134. # icombine's benefit, and then use icombine to scrunch them together.
  135. #
  136. # Note:  one disadvantage of this pipeline is that for a large file,
  137. # the join and icombine may be sitting around for a long time while ispell
  138. # and sorts run.  You can get rid of this by splitting the pipe, at
  139. # the expense of more temp file space.
  140. #
  141. if [ $cheap = yes ]
  142. then
  143.     ispell $wchars -c -d /dev/null -p /dev/null < ${TMP}b \
  144.       | egrep / | sort -u -t/ +0 -1 +1 \
  145.       | join -t/ - ${TMP}a \
  146.       | sort -u -t/ +0f -1 +0 -1 +1 | $COMBINE > ${TMP}c
  147. else
  148.     ispell $wchars -c -d /dev/null -p /dev/null < ${TMP}b \
  149.       | egrep / | sort -u -t/ +0 -1 +1 \
  150.       | join -t/ - ${TMP}a > ${TMP}c
  151. fi
  152.  
  153. args "" "TMPc" "--"; cat ${TMP}c; args "--"
  154.  
  155. #
  156. # There is now one slight problem:  the suffix flags X, J, and Z
  157. # are simply the addition of an "S" to the suffixes N, G, and R,
  158. # respectively.  This produces redundant entries in the output file;
  159. # for example, ABBREVIATE/N/X and ABBREVIATION/S.  We must get rid
  160. # of the unnecessary duplicates.  The candidates are those words that
  161. # have only an "S" flag (egrep).  We strip off the "S" (sed), and
  162. # generate a list of roots that might have made these words (ispell -c).
  163. # Of these roots, we select those that have the N, G, or R flags,
  164. # replacing each with the plural equivalent X, J, or Z (sed -n).
  165. # Using join once again, we select those that have legal roots
  166. # and put them in ${TMP}d.
  167. #
  168. if [ $cheap = yes ]
  169. then
  170.     egrep '^[^/]*/S$' ${TMP}c | sed 's@/S$@@' \
  171.       | ispell $wchars -c -d /dev/null -p /dev/null \
  172.       | sed -n -e '/\/N/s/N$/X/p' -e '/\/G/s/G$/J/p' -e '/\/R/s/R$/Z/p' \
  173.       | sort -u -t/ +0 -1 +1 \
  174.       | join -t/ - ${TMP}a \
  175.       | sort -u -t/ +0f -1 +0 -1 +1 \
  176.       | $COMBINE > ${TMP}d
  177. else
  178.     egrep '^[^/]*/S$' ${TMP}c | sed 's@/S$@@' \
  179.       | ispell $wchars -c -d /dev/null -p /dev/null \
  180.       | sed -n -e '/\/N/s/N$/X/p' -e '/\/G/s/G$/J/p' -e '/\/R/s/R$/Z/p' \
  181.       | sort -u -t/ +0 -1 +1 \
  182.       | join -t/ - ${TMP}a > ${TMP}d
  183. fi
  184. # /bin/rm -f ${TMP}a
  185.  
  186. args "" "TMPd" "--"; cat ${TMP}d; args "--"
  187.  
  188. #
  189. # Now we have to eliminate the stuff covered by ${TMP}d from ${TMP}c.
  190. # First, we re-expand the suffixes we just made (four sed's), and let
  191. # ispell re-create the /S version (ispell -c).  We select the /S versions
  192. # only (egrep), sort them (sort) for comm, and use comm to delete these
  193. # from ${TMP}c.  The output of comm (i.e., the trimmed version of
  194. # ${TMP}c) is combined with our special-suffixes file ${TMP}d (sort again)
  195. # and reduced in size (icombine) to produce a final list of all words
  196. # that have at least one suffix.
  197. #
  198. sed -f $EXPAND1 ${TMP}d | sed -f $EXPAND2 | sed -f $EXPAND3 | sed -f $EXPAND4 \
  199.   | ispell $wchars -c -d /dev/null -p /dev/null \
  200.   | egrep '\/S$' | sort -u -t/ +0 -1 +1 | tee ${TMP}test1 | comm -13 - ${TMP}c \
  201.   | tee ${TMP}test2 \
  202.   | sort -u -t/ +0f -1 +0 -1 +1 - ${TMP}d \
  203.   | $COMBINE > ${TMP}e
  204. # /bin/rm -f ${TMP}[cd]
  205.  
  206. args "" "TMPtest1" "--"; cat ${TMP}test1; args "--"
  207. args "" "TMPtest2" "--"; cat ${TMP}test2; args "--"
  208. args "" "TMPe" "--"; cat ${TMP}e; args "--"
  209.  
  210. #
  211. # Now a slick trick.  Use ispell to select those (root) words from the original
  212. # list (${TMP}b) that are not covered by the suffix list (${TMP}e).  Then we
  213. # merge these with the suffix list, sort it, and use icombine to strip out
  214. # unnecessary capitalizations and produce the final output.
  215. #
  216. ispell $wchars -d /dev/null -p ${TMP}e -l < ${TMP}b \
  217.   | sort -t/ +0f -1 +0 -1 +1 - ${TMP}e \
  218.   | $COMBINE
  219. # /bin/rm -f ${TMP}*
  220.