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

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