home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Product / Product.zip / ISPSRC.ZIP / subset.X < prev    next >
Encoding:
Text File  |  1992-08-14  |  5.4 KB  |  194 lines

  1. : Use /bin/sh
  2. #
  3. # $Id: subset.X,v 1.9 1992/01/09 09:29:53 geoff Exp $
  4. #
  5. # Copyright 1987, 1988, 1989, by Geoff Kuenning, Manhattan Beach, CA
  6. # Permission for non-profit use is hereby granted.
  7. # All other rights reserved.
  8. # See "version.h" for a more complete copyright notice.
  9. #
  10. # $Log: subset.X,v $
  11. # Revision 1.9  1992/01/09  09:29:53  geoff
  12. # Fix a typo and a tiny documentation error.
  13. #
  14. # Revision 1.8  91/07/27  20:48:35  geoff
  15. # Improve the commentary and the usage message.  Just for neatness,
  16. # generate dict.0 first rather than last.  Remove intermediate
  17. # dictionaries from the temp directory as they are used up.
  18. # Revision 1.7  91/06/12  19:15:20  geoff
  19. # Remember to exit after issuing a usage message.  Add some warnings about
  20. # anomalous behaviors of the script under semi-error conditions.
  21. # Revision 1.6  89/04/28  01:17:08  geoff
  22. # Change Header to Id;  nobody cares about my pathnames.
  23. # Revision 1.5  89/04/27  23:34:16  geoff
  24. # Add (untested) support for a selectable flag marker.
  25. # Revision 1.4  88/12/26  02:32:22  geoff
  26. # Update the copyright notice.
  27. # Revision 1.3  88/03/30  00:15:01  geoff
  28. # Replace the -d switch with the -l switch.
  29. # Revision 1.2  88/02/20  23:14:34  geoff
  30. # Get rid of icombine.  Fix some typos.  Use a better temp file name.
  31. # Run munchlist on the output dictionaries.
  32. # Revision 1.1  87/09/25  00:28:20  geoff
  33. # Initial revision
  34. #
  35. #    Combine and resolve various dictionaries so they are proper
  36. #    subsets of one another, and so that maximal use is made of
  37. #    flags in the smaller ones.
  38. #
  39. #    Usage:
  40. #
  41. #    subset [-b base] [-l langfile] small-dict bigger-dict ... biggest-dict
  42. #
  43. #    The output is a an equal number of successively-larger
  44. #    dictionaries.  The smallest is written to "dict.0".  Successive
  45. #    files are named "dict.1", "dict.2", and so forth, and each contains
  46. #    a list of words which should be added to the previous files to
  47. #    generate a dictionary.  Words which are in smaller dictionaries are
  48. #    effectively propagated to the larger ones, so that the smaller ones
  49. #    are proper subsets of their siblings.  If dictionaries are
  50. #    completely disjoint, this may result in an empty output dictionary.
  51. #    Affix flags are propagated to the smallest dictionary containing
  52. #    the root word;  this expands the effectiveness of small dictionaries
  53. #    at no cost in hash table space.
  54. #
  55. #    The -b switch is used to specify a different base name for the
  56. #    output files than "dict".  (In other words, "-b english" would
  57. #    produce output in english.0, english.1, etc.).
  58. #
  59. #    If the -l switch is specified, the language tables are gotten
  60. #    from the specified file;  otherwise they come from $LIBDIR/!!DEFLANG!!.
  61. #
  62. #    Input dictionaries should be "clean";  if non-word characters
  63. #    appear in the dictionaries, the script may produce incorrect output.
  64. #
  65. LIBDIR=!!LIBDIR!!
  66. TDIR=${TMPDIR-/usr/tmp}
  67. TMP=${TDIR}/sset$$.
  68. SORTTMP="-T ${TDIR}"            # !!SORTTMP!!
  69. USAGE="Usage:  subset [-b base] [-l langfile] dict-0 dict-1 ..."
  70.  
  71. langtabs=${LIBDIR}/!!DEFLANG!!
  72. outbase=dict
  73. while :
  74. do
  75.     case "$1" in
  76.     -b)
  77.         outbase="$2"
  78.         shift; shift
  79.         ;;
  80.     -l)
  81.         langtabs="$2"
  82.         shift; shift
  83.         ;;
  84.     -*)
  85.         echo "$USAGE" 1>&2
  86.         exit 1
  87.         ;;
  88.     *)
  89.         break
  90.         ;;
  91.     esac
  92. done
  93.  
  94. if [ $# -lt 2 ]
  95. then
  96.     echo "$USAGE" 1>&2
  97.     exit 1
  98. fi
  99.  
  100. # Temp files
  101. MUNCHOUTPUT=${TMP}a
  102. MISSINGWORDS=${TMP}b
  103. TEMPDICT=${TMP}c
  104. FAKEDICT=${TMP}d
  105. FAKEHASH=${TMP}e.hash
  106.  
  107. trap "/bin/rm -f ${TMP}*; exit 1" 1 2 15
  108. trap "/bin/rm -f ${TMP}*; exit 0" 13
  109.  
  110. #
  111. # Create a dummy dictionary to hold a compiled copy of the language
  112. # tables.
  113. #
  114. echo 'QQQQQQQQ' > $FAKEDICT
  115. buildhash -s $FAKEDICT $langtabs $FAKEHASH \
  116.   ||  (echo "Couldn't create fake hash file" 1>&2; /bin/rm -f ${TMP}*; exit 1) \
  117.   ||  exit 1
  118. /bin/rm -f ${FAKEDICT}*
  119. #
  120. # Figure out what the flag-marking character is.
  121. #
  122. flagmarker=`ispell -D -d $FAKEHASH \
  123.   | sed -n '/^flagmarker/s/flagmarker //p'`
  124. case "$flagmarker" in
  125.     \\*)
  126.     flagmarker=`expr "$flagmarker" : '.\(.\)'`
  127.     ;;
  128. esac    
  129. #
  130. #    (1) Use munchlist to create a list of roots and maximal suffixes.
  131. #
  132. munchlist -l $langtabs "$@" | sort $SORTTMP > $MUNCHOUTPUT
  133. #
  134. #    (2) Use join to add the maximal suffixes to each dictionary's roots.
  135. #        Re-expand this, combine with the original, and save for later.
  136. #
  137. newline='
  138. '
  139. dictno=0
  140. for dictfile
  141. do
  142.     ispell -e -d $FAKEHASH < $dictfile | tr ' ' "$newline" \
  143.       | sort -u $SORTTMP | join "-t$flagmarker" -a1 - $MUNCHOUTPUT \
  144.       | ispell -e -d $FAKEHASH | tr ' ' "$newline" \
  145.       | sort -u $SORTTMP > ${TEMPDICT}.$dictno
  146.     dictno=`expr $dictno + 1`
  147. done
  148. /bin/rm -f $MUNCHOUTPUT
  149. #
  150. #    (3) For each adjacent pair of dictionaries, use comm to find words
  151. #        in the smaller that are missing from the larger, and add them
  152. #        to the larger.
  153. #
  154. firstdict="$1"
  155. shift
  156. lastdict="${TEMPDICT}.0"
  157. dictno=1
  158. for dictfile
  159. do
  160.     comm -23 $lastdict ${TEMPDICT}.$dictno > $MISSINGWORDS.$dictno
  161.     if [ -s $MISSINGWORDS.$dictno ]
  162.     then
  163.     sort $SORTTMP -o ${TEMPDICT}.$dictno \
  164.       ${TEMPDICT}.$dictno $MISSINGWORDS.$dictno
  165.     fi
  166.     lastdict="${TEMPDICT}.$dictno"
  167.     dictno=`expr $dictno + 1`
  168. done
  169. /bin/rm -f $MISSINGWORDS.*
  170. #
  171. #    (4) For each pair of dictionaries, use comm to eliminate words in
  172. #        the smaller from the larger, and shrink the result with munchlist.
  173. #
  174. munchlist ${TEMPDICT}.0 > $outbase.0
  175. lastdict="${TEMPDICT}.0"
  176. dictno=1
  177. for dictfile
  178. do
  179.     comm -13 $lastdict ${TEMPDICT}.$dictno \
  180.       | munchlist -l $langtabs > $outbase.$dictno
  181.     /bin/rm -f $lastdict
  182.     lastdict="${TEMPDICT}.$dictno"
  183.     dictno=`expr $dictno + 1`
  184. done
  185. /bin/rm -f ${TMP}*
  186.