home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume8 / cz / part01 / 78common.h < prev    next >
Encoding:
Text File  |  1989-10-01  |  6.1 KB  |  209 lines

  1. /*
  2.  * 78common.h - common functions for ISO 646 to 8-bit conversion
  3.  *
  4.  * Copyright 1989 Howard Lee Gayle
  5.  *
  6.  * $Header: 78common.h,v 1.2 89/08/24 13:03:41 howard Exp $
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License version 1,
  10.  * as published by the Free Software Foundation.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  *
  21.  * Prerequisites:
  22.  *    limits.h
  23.  *    howard/port.h
  24.  *    howard/a2.h
  25.  *    howard/malf.h
  26.  *    howard/registers.i
  27.  *    cz.h
  28.  *    78.h
  29.  *    78????.h (defines byte2t[])
  30.  */
  31.  
  32. /* dif78 - compute trigram frequency difference for one word */
  33.  
  34. PUBLIC int dif78 (wp, lim, tt)
  35. R1 bStrT    wp;  /* The word.*/
  36. R3 bStrT    lim; /* Don't pass this.*/
  37. R4 triDifT *tt;  /* Difference table.*/
  38.  
  39. /* Function:
  40.  *    Sum the frequency differences of all the trigrams in a word.
  41.  * Algorithm:
  42.  *    Special-case single-letter words.  Otherwise, loop through
  43.  *    the word.
  44.  * Returns:
  45.  *    Sum of the values in the difference table for each trigram in the word.
  46.  * Notes:
  47.  *    
  48.  */
  49. {
  50. R2 int r; /* Return.*/
  51.  
  52. if (NULBSTR == wp)            malf1 ("dif78: word NUL");
  53. if (EOS == B(*wp))            malf1 ("dif78: word empty");
  54. if (((triDifT *) NULL) == tt) malf1 ("dif78: no difference table");
  55. if ((EOS == B(wp[1])) || (lim == (wp + 1)))
  56.     return (tt[TOTRI (TRIBEG, byte2t[B(*wp)], TRIEND)] - TRIBIAS);
  57. r = tt[TOTRI (TRIBEG, byte2t[B(*wp)], byte2t[B(wp[1])])] - TRIBIAS;
  58. for (; (EOS != B(wp[2])) && (lim != (wp + 2)); ++wp)
  59.    r += tt[TOTRI (byte2t[B(*wp)], byte2t[B(wp[1])], byte2t[B(wp[2])])]-TRIBIAS;
  60. r += tt[TOTRI (byte2t[B(*wp)], byte2t[B(wp[1])], TRIEND)] - TRIBIAS;
  61. return (r);
  62. }
  63.  
  64. /* frqmm - find minimum and maximum values in a trigram frequency table */
  65.  
  66. PRIVATE void frqmm (fp, minp, pminp, maxp)
  67. R1 long *fp;    /* Points to frequency table.*/
  68. R7 long *minp;  /* Store min here.*/
  69. R8 long *pminp; /* Store positive min here.*/
  70. R9 long *maxp;  /* Store max here.*/
  71.  
  72. /* Function:
  73.  *    Find the minimum and maximum frequencies in the given table.
  74.  * Algorithm:
  75.  *    Linear search.
  76.  * Returns:
  77.  *    
  78.  * Notes:
  79.  *    
  80.  */
  81. {
  82. R2 long  f;   /* Current frequency.*/
  83. R6 long *fp2; /* End of fp[].*/
  84. R3 long  l;   /* Min.*/
  85. R4 long  h;   /* Max.*/
  86. R5 long  p;   /* Positive min.*/
  87.  
  88. if (NULONGP == fp) malf1 ("frqmm: no frequency table");
  89. l = LONG_MAX;
  90. h = LONG_MIN;
  91. p = LONG_MAX;
  92. for (fp2 = fp + (TRIMAX + 1); fp != fp2;)
  93.    {
  94.    f = *fp++;
  95.    if (f < l) l = f;
  96.    if (f > h) h = f;
  97.    if ((f > 0) && (f < p)) p = f;
  98.    }
  99. if (NULONGP != minp)  *minp = l;
  100. if (NULONGP != pminp) *pminp = p;
  101. if (NULONGP != maxp)  *maxp = h;
  102. }
  103.  
  104. /* mrdfrq - read in a trigram frequency file */
  105.  
  106. PRIVATE void mrdfrq (fn, fp)
  107. R2 bStrT fn; /* File name.*/
  108. R3 long *fp; /* Points to frequency table.*/
  109.  
  110. /* Function:
  111.  *    Read a file representation of a trigram frequency table into
  112.  *    its internal representation.
  113.  * Algorithm:
  114.  *    Read each line in the file.  Convert the frequency to a long.
  115.  *    Index into fp[] and store the value.
  116.  * Returns:
  117.  *    
  118.  * Notes:
  119.  *    1) There is no checking for duplicate trigrams.
  120.  *    2) fp[] is assumed to be zero on entry.
  121.  */
  122. {
  123. R4 int      a;         /* Trigram code for first letter.*/
  124. R5 int      b;         /* Trigram code for second letter.*/
  125. R6 int      c;         /* Trigram code for third letter.*/
  126. R7 long     f;         /* Frequency.*/
  127. R8 streamT  is;        /* Input stream.*/
  128.    unsigned ln = 0;    /* Input line number.*/
  129. R1 bStrT    p;         /* Steps through lb[].*/
  130.    bStrT    p0;        /* End of frequency.*/
  131.    byteT    lb[MLINE]; /* Line buffer.*/
  132.  
  133. if (NULONGP == fp) malf1 ("mrdfrq: no frequency table");
  134. is = mfopen (fn, "r");
  135. while (NULBSTR != getlic (lb, MLINE, is, fn, &ln, 1, COMMENT))
  136.    {
  137.    f = mra2l (lb, NULBSTR, TRUE, S("Frequency"), 0L, (long) LONG_MAX, &p0);
  138.    for (p = p0; ' ' == B(*p); ++p)
  139.       ;
  140.    if ((EOS == B(*p)) || (EOS == B(p[1])) || (EOS == B(p[2])))
  141.       malf1 ("%s %u: bad trigram", fn, ln);
  142.    a = (('(' == B(*p)) ? TRIBEG : byte2t[B(*p)]);
  143.    b = byte2t[B(p[1])];
  144.    c = ((')' == B(p[2])) ? TRIEND : byte2t[B(p[2])]);
  145.    fp[TOTRI (a, b, c)] = f;
  146.    }
  147. mfclose (is, fn);
  148. }
  149.  
  150. /* mrdtri - read trigram table and handle errors */
  151.  
  152. PUBLIC void mrdtri (sim, tt)
  153. bStrT sim; /* Simple part of file name.*/
  154. bStrT tt;  /* Store trigram table here.*/
  155.  
  156. /* Function:
  157.  *    Read in a trigram table.
  158.  * Algorithm:
  159.  *    Call mopenp() to search path and open the file.
  160.  *    Call fread() to do the read.  Close the file
  161.  * Returns:
  162.  *    
  163.  * Notes:
  164.  *    
  165.  */
  166. {
  167. R1 streamT is;         /* Input stream.*/
  168.    byteT   fnb[MFILE]; /* Store full paths here.*/
  169.  
  170. is = mopenp (path, PATHSEP, sim, TTSUF, S("r"), fnb, MFILE);
  171. if ((1 != fread ((cStrT) tt, TRIMAX + 1, 1, is)) || ferror (is))
  172.    malf1 ("%s: Read error", fnb);
  173. mfclose (is, fnb);
  174. }
  175.  
  176. /* word78 - compute trigram index for one word */
  177.  
  178. PUBLIC unsigned word78 (wp, lim, b2t, tt)
  179. R1 bStrT wp;  /* The word.*/
  180. R4 bStrT lim; /* Don't pass this.*/
  181. R2 bStrT b2t; /* Byte to trigraph code map.*/
  182. R5 bStrT tt;  /* Trigram table.*/
  183.  
  184. /* Function:
  185.  *    Sum the frequencies of all the trigrams in a word.
  186.  * Algorithm:
  187.  *    Special-case single-letter words.  Otherwise, loop through
  188.  *    the word.
  189.  * Returns:
  190.  *    Sum of the values in the trigram table for each trigram in the word.
  191.  * Notes:
  192.  *    
  193.  */
  194. {
  195. R3 unsigned r; /* Return.*/
  196.  
  197. if (NULBSTR == wp)  malf1 ("word78: word NUL");
  198. if (EOS == B(*wp))  malf1 ("word78: word empty");
  199. if (NULBSTR == b2t) malf1 ("word78: no code map");
  200. if (NULBSTR == tt)  malf1 ("word78: no trigram table");
  201. if ((EOS == B(wp[1])) || (lim == (wp + 1)))
  202.     return (tt[TOTRI (TRIBEG, b2t[B(*wp)], TRIEND)]);
  203. r = tt[TOTRI (TRIBEG, b2t[B(*wp)], b2t[B(wp[1])])];
  204. for (; (EOS != B(wp[2])) && (lim != (wp + 2)); ++wp)
  205.    r += tt[TOTRI (b2t[B(*wp)], b2t[B(wp[1])], b2t[B(wp[2])])];
  206. r += tt[TOTRI (b2t[B(*wp)], b2t[B(wp[1])], TRIEND)];
  207. return (r);
  208. }
  209.