home *** CD-ROM | disk | FTP | other *** search
- /*
- * 78freq2tt - make trigram table
- */
-
- #ifndef lint
- static char _cpyrgt[] = "Copyright 1989 Howard Lee Gayle";
- #endif lint
-
- /*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 1,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
- #include <stdio.h>
- #include <howard/port.h>
- #include <howard/version.h>
- #include <howard/usage.h>
-
- MAINVER ("@(#)$Header: 78freq2tt.c,v 2.4 89/08/28 18:54:50 howard Exp $");
- USAGE ("[-d encodings frequency1 frequency2 table] or [-p encodings {frequency table}]");
-
- #include <float.h>
- #include <limits.h>
- #include <math.h>
- #include <howard/a2.h>
- #include <howard/malf.h>
- #include <howard/registers.i>
- #include "cz.h"
- #include "78.h"
- #include "78code.h"
- #include "78common.h"
-
- #define MTABLE 3 /* Max number of frequency tables.*/
-
- PRIVATE long freq[MTABLE][TRIMAX + 1]; /* Frequency tables.*/
- PRIVATE double lfreq[TRIMAX + 1]; /* Log freq.*/
-
- PRIVATE char eIntern[] = "Internal error %s";
-
- /* dif - make a difference table from two frequency tables */
-
- PRIVATE void dif (ffn1, ffn2, dfn)
- bStrT ffn1; /* Name of file containing first frequency table.*/
- bStrT ffn2; /* Name of file containing second frequency table.*/
- R9 bStrT dfn; /* Name of file to hold output difference trigram table.*/
-
- /* Function:
- * Write a trigram difference table.
- * Algorithm:
- * Read in the two frequency tables. Compute and apply the cutoff.
- * Compute ln (f1/f2). Scale.
- * Returns:
- *
- * Notes:
- *
- */
- {
- R8 long co; /* Cutoff frequency.*/
- double f1; /* 1 + freq[0][i].*/
- double f2; /* 1 + freq[1][i].*/
- R4 long *fp1; /* Steps through freq[0].*/
- R5 long *fp2; /* End of freq[0].*/
- R6 long *fp3; /* Steps through freq[1].*/
- R1 int i; /* General putpose.*/
- double l; /* ln (f1/f2).*/
- R2 double *lp1; /* Steps through lfreq[].*/
- R3 double *lp2; /* End of lfreq[].*/
- double maxl; /* Max |l|.*/
- long min1; /* Minimum frequency in first table.*/
- long min2; /* Minimum frequency in second table.*/
- double minl; /* Min (l).*/
- R7 streamT os; /* Output stream.*/
- double s; /* Scale factor.*/
-
- mrdfrq (ffn1, freq[0]);
- mrdfrq (ffn2, freq[1]);
- os = mfopen (dfn, "w");
- frqmm (freq[0], NULONGP, &min1, NULONGP);
- frqmm (freq[1], NULONGP, &min2, NULONGP);
- if (min1 > min2)
- {
- fp1 = freq[1];
- co = min1;
- }
- else
- {
- fp1 = freq[0];
- co = min2;
- }
- FPRINTF (stderr, "Cutoff %ld\n", co);
- for (fp2 = fp1 + (TRIMAX + 1); fp1 != fp2; ++fp1)
- if (*fp1 < co) *fp1 = 0;
- fp1 = freq[0];
- fp3 = freq[1];
- lp1 = lfreq;
- maxl = -DBL_MAX;
- minl = DBL_MAX;
- for (fp2 = fp1 + (TRIMAX + 1); fp1 != fp2;)
- {
- f1 = *fp1 + 1;
- f2 = *fp3 + 1;
- l = log (f1 / f2);
- *lp1++ = l;
- if (l < minl) minl = l;
- if (l > maxl) maxl = l;
- ++fp1;
- ++fp3;
- }
- maxl = MAX (DABS (minl), DABS (maxl));
- s = 127.0 / maxl;
- lp1 = lfreq;
- for (lp2 = lp1 + (TRIMAX + 1); lp1 != lp2; ++lp1)
- {
- i = (int) (*lp1 * s);
- if (ABS (i) > 127) malf1 (eIntern, "dif 1");
- PUTC (i + TRIBIAS, os);
- }
- mfflush (os, dfn);
- mfclose (os, dfn);
- }
- /* main - main function */
-
- PUBLIC int main (argc, argv)
- R5 int argc; /* Number of arguments.*/
- R1 bStrT *argv; /* Points to array of argument strings.*/
-
- /* Function:
- *
- * Algorithm:
- * Decode args and call appropriate function.
- * Notes:
- *
- */
-
- {
- R2 rcharT c; /* Option letter.*/
- extern int optind; /* See getopt (3).*/
- extern cStrT optarg; /* See getopt (3).*/
- R3 boolT difFlg = FALSE; /* Made a difference table.*/
- R4 boolT pairFlg = FALSE; /* Made trigram tables.*/
-
- while (EOF != (c = getopt (argc, (cStrT *) argv, "dp")))
- {
- switch (c)
- {
- case '?':
- usage();
- break;
- case 'd':
- difFlg = TRUE;
- break;
- case 'p':
- pairFlg = TRUE;
- break;
- default:
- malf1 (eIntern, "main 1");
- break;
- }
- }
- argc -= optind;
- argv += optind;
- if (difFlg == pairFlg) usage();
- if (difFlg)
- {
- if (4 != argc) usage();
- rdcode (argv[0]);
- dif (argv[1], argv[2], argv[3]);
- }
- else
- {
- malf1 ("-p option not implemented");
- }
- mfflush (stdout, S("Standard Output"));
- exit (SUCCESS);
-
- #ifdef lint
- return (SUCCESS);
- #endif
- }
-