home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume8 / cz / part01 / 78freq2tt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-01  |  4.6 KB  |  190 lines

  1. /*
  2.  * 78freq2tt - make trigram table
  3.  */
  4.  
  5. #ifndef lint
  6. static char _cpyrgt[] = "Copyright 1989 Howard Lee Gayle";
  7. #endif lint
  8.  
  9. /*
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License version 1,
  12.  * as published by the Free Software Foundation.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <howard/port.h>
  26. #include <howard/version.h>
  27. #include <howard/usage.h>
  28.  
  29. MAINVER ("@(#)$Header: 78freq2tt.c,v 2.4 89/08/28 18:54:50 howard Exp $");
  30. USAGE ("[-d encodings frequency1 frequency2 table] or [-p encodings {frequency table}]");
  31.  
  32. #include <float.h>
  33. #include <limits.h>
  34. #include <math.h>
  35. #include <howard/a2.h>
  36. #include <howard/malf.h>
  37. #include <howard/registers.i>
  38. #include "cz.h"
  39. #include "78.h"
  40. #include "78code.h"
  41. #include "78common.h"
  42.  
  43. #define MTABLE 3    /* Max number of frequency tables.*/
  44.  
  45. PRIVATE long   freq[MTABLE][TRIMAX + 1]; /* Frequency tables.*/
  46. PRIVATE double lfreq[TRIMAX + 1];        /* Log freq.*/
  47.  
  48. PRIVATE char eIntern[] = "Internal error %s";
  49.  
  50. /* dif - make a difference table from two frequency tables */
  51.  
  52. PRIVATE void dif (ffn1, ffn2, dfn)
  53.    bStrT ffn1; /* Name of file containing first frequency table.*/
  54.    bStrT ffn2; /* Name of file containing second frequency table.*/
  55. R9 bStrT dfn;  /* Name of file to hold output difference trigram table.*/
  56.  
  57. /* Function:
  58.  *    Write a trigram difference table.
  59.  * Algorithm:
  60.  *    Read in the two frequency tables.  Compute and apply the cutoff.
  61.  *    Compute ln (f1/f2).  Scale.
  62.  * Returns:
  63.  *    
  64.  * Notes:
  65.  *    
  66.  */
  67. {
  68. R8 long    co;   /* Cutoff frequency.*/
  69.    double  f1;   /* 1 + freq[0][i].*/
  70.    double  f2;   /* 1 + freq[1][i].*/
  71. R4 long   *fp1;  /* Steps through freq[0].*/
  72. R5 long   *fp2;  /* End of freq[0].*/
  73. R6 long   *fp3;  /* Steps through freq[1].*/
  74. R1 int     i;    /* General putpose.*/
  75.    double  l;    /* ln (f1/f2).*/
  76. R2 double *lp1;  /* Steps through lfreq[].*/
  77. R3 double *lp2;  /* End of lfreq[].*/
  78.    double  maxl; /* Max |l|.*/
  79.    long    min1; /* Minimum frequency in first table.*/
  80.    long    min2; /* Minimum frequency in second table.*/
  81.    double  minl; /* Min (l).*/
  82. R7 streamT os;   /* Output stream.*/
  83.    double  s;    /* Scale factor.*/
  84.  
  85. mrdfrq (ffn1, freq[0]);
  86. mrdfrq (ffn2, freq[1]);
  87. os = mfopen (dfn, "w");
  88. frqmm (freq[0], NULONGP, &min1, NULONGP);
  89. frqmm (freq[1], NULONGP, &min2, NULONGP);
  90. if (min1 > min2)
  91.    {
  92.    fp1 = freq[1];
  93.    co = min1;
  94.    }
  95. else
  96.    {
  97.    fp1 = freq[0];
  98.    co = min2;
  99.    }
  100. FPRINTF (stderr, "Cutoff %ld\n", co);
  101. for (fp2 = fp1 + (TRIMAX + 1); fp1 != fp2; ++fp1)
  102.    if (*fp1 < co) *fp1 = 0;
  103. fp1 = freq[0];
  104. fp3 = freq[1];
  105. lp1 = lfreq;
  106. maxl = -DBL_MAX;
  107. minl = DBL_MAX;
  108. for (fp2 = fp1 + (TRIMAX + 1); fp1 != fp2;)
  109.    {
  110.    f1 = *fp1 + 1;
  111.    f2 = *fp3 + 1;
  112.    l = log (f1 / f2);
  113.    *lp1++ = l;
  114.    if (l < minl) minl = l;
  115.    if (l > maxl) maxl = l;
  116.    ++fp1;
  117.    ++fp3;
  118.    }
  119. maxl = MAX (DABS (minl), DABS (maxl));
  120. s = 127.0 / maxl;
  121. lp1 = lfreq;
  122. for (lp2 = lp1 + (TRIMAX + 1); lp1 != lp2; ++lp1)
  123.    {
  124.    i = (int) (*lp1 * s);
  125.    if (ABS (i) > 127) malf1 (eIntern, "dif 1");
  126.    PUTC (i + TRIBIAS, os);
  127.    }
  128. mfflush (os, dfn);
  129. mfclose (os, dfn);
  130. }
  131. /* main - main function                            */
  132.  
  133. PUBLIC int main (argc, argv)
  134. R5 int    argc; /* Number of arguments.*/
  135. R1 bStrT *argv; /* Points to array of argument strings.*/
  136.  
  137. /* Function:
  138.  *    
  139.  * Algorithm:
  140.  *    Decode args and call appropriate function.
  141.  * Notes:
  142.  *    
  143.  */
  144.  
  145. {
  146. R2     rcharT c; /* Option letter.*/
  147. extern int optind; /* See getopt (3).*/
  148. extern cStrT optarg; /* See getopt (3).*/
  149. R3     boolT difFlg = FALSE; /* Made a difference table.*/
  150. R4     boolT pairFlg = FALSE; /* Made trigram tables.*/
  151.  
  152. while (EOF != (c = getopt (argc, (cStrT *) argv, "dp")))
  153.    {
  154.    switch (c)
  155.       {
  156.       case '?':
  157.          usage();
  158.          break;
  159.       case 'd':
  160.          difFlg = TRUE;
  161.          break;
  162.       case 'p':
  163.          pairFlg = TRUE;
  164.          break;
  165.       default:
  166.          malf1 (eIntern, "main 1");
  167.          break;
  168.       }
  169.    }
  170. argc -= optind;
  171. argv += optind;
  172. if (difFlg == pairFlg) usage();
  173. if (difFlg)
  174.    {
  175.    if (4 != argc) usage();
  176.    rdcode (argv[0]);
  177.    dif (argv[1], argv[2], argv[3]);
  178.    }
  179. else
  180.    {
  181.    malf1 ("-p option not implemented");
  182.    }
  183. mfflush (stdout, S("Standard Output"));
  184. exit (SUCCESS);
  185.  
  186. #ifdef lint
  187. return (SUCCESS);
  188. #endif
  189. }
  190.