home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume10 / cbw / part01 / tritab.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-16  |  2.9 KB  |  146 lines

  1. /*
  2.  * Operations to load and print the table of trigrams.
  3.  */
  4.  
  5. #include    <math.h>
  6. #include    <stdio.h>
  7. #include    "window.h"
  8. #include    "specs.h"
  9. #include    "cipher.h"
  10. #include    "autotri.h"
  11.  
  12.  
  13.  
  14. #define    DEFAULTTRIGLIST    "trigrams.txt"
  15. int        trig_loaded = FALSE;
  16.  
  17. /* This number is the probability that a randomly selected trigram
  18.  * is not in the table of trigrams.
  19.  */
  20. float    trig_other_prob;
  21.  
  22.  
  23. /* Table of trigrams with their probabilities.
  24.  * The last entry has trig_ent.trigram == NULL.
  25.  * This table is also be used as a priority queue.
  26.  */
  27. int            trig_tab_next;            /* For priority queue. */
  28. trig_ent    trig_tab[TRIGTABSZ];
  29.  
  30.  
  31.  
  32. /* The following buffer is used to store all the trigram
  33.  * strings read from the file.
  34.  * trig_buf_next points to the net free character.
  35.  */
  36. char    *trig_buf_next;
  37. char    trig_buf[TRIGBUFSZ];
  38.  
  39.  
  40.  
  41. /* Load the trigram table from the named file.
  42.  */
  43. load_tri_from(filename)
  44. char    *filename;
  45. {
  46.     FILE    *inp;
  47.  
  48.     if ((inp = fopen(filename, "r")) == NULL)  {
  49.         printf("\nCannot open %s to read trigram stats.\n", filename);
  50.         exit(0);
  51.         }
  52.     load_tri(inp);
  53.     fclose(inp);
  54. }
  55.  
  56.  
  57. /* Load trigram table from the given stream.
  58.  * Input format:
  59.  * <Total number of trigrams>
  60.  * <blank line>
  61.  * <Count for a particular trigram><space><Chars in the particular trigram>
  62.  * ...
  63.  * <Count for a particular trigram><space><Chars in the particular trigram>
  64.  * <blank line>
  65.  * <End of file>
  66.  */
  67. load_tri(inp)
  68. FILE    *inp;
  69. {
  70.     int        i,n;
  71.     int        tmp;
  72.     int        c;
  73.     float    v, trigram_prob;
  74.     float    etotal, ctotal;
  75.     char    *trigram_start;
  76.  
  77.     trig_loaded = TRUE;
  78.     trig_tab_next = 0;
  79.     trig_buf_next = trig_buf;
  80.     trig_other_prob = 1.0;
  81.     trig_tab[0].trigram = NULL;
  82.  
  83.     if (fscanf(inp, "%d", &tmp) != 1)  {
  84.         printf("\nError while getting total trigram count.\n");
  85.         exit(0);
  86.         }
  87.     etotal = tmp;
  88.     ctotal = 0.0;
  89.  
  90.     if (fscanf(inp, "\n") != 0)  {
  91.         printf("\nError while skipping blank line in trigram file.\n");
  92.         return;
  93.         }
  94.  
  95.     while (TRUE) {
  96.         if ((n = fscanf(inp, "%d", &tmp)) != 1)  {
  97.             if (n == 0) break;
  98.             if (n == EOF) break;
  99.             printf("\nError getting character count from trigram file.\n ");
  100.             return;
  101.             }
  102.         v = tmp;
  103.         ctotal += v;
  104.         trigram_prob = v/etotal;
  105.         trig_other_prob -= trigram_prob;
  106.  
  107.         c = read_char(inp);        /* Skip the space. */
  108.         trigram_start = trig_buf_next;
  109.         while (TRUE) {
  110.             c = read_char(inp);
  111.             if (c == EOL)  break;
  112.             if (trig_buf_next >= &trig_buf[TRIGBUFSZ-1]) {
  113.                 printf("\nOverflowed Trigram buffer.\n");
  114.                 exit(0);
  115.                 }
  116.             *trig_buf_next++ = c & CHARMASK;
  117.             }
  118.         if (trig_buf_next >= &trig_buf[TRIGBUFSZ-1]) {
  119.             printf("\nOverflowed Trigram buffer.\n");
  120.             exit(0);
  121.             }
  122.         *trig_buf_next++ = NULL;
  123.  
  124.         trig_tab[trig_tab_next].prob = trigram_prob;
  125.         trig_tab[trig_tab_next].trigram = trigram_start;
  126.         trig_tab[trig_tab_next].notused = 0;
  127.         trig_tab_next++;
  128.         }
  129.  
  130. }
  131.  
  132.  
  133. /* Print the trigram table onto a stream.
  134.  */
  135. print_tri(out)
  136. FILE    *out;
  137. {
  138.     int        i;
  139.  
  140.     fprintf(out, "\n");
  141.     for (i = 0 ; trig_tab[i].trigram != NULL ; i++)  {
  142.         fprintf(out, "'%s'\t%7.4f\n", trig_tab[i].trigram, trig_tab[i].prob);
  143.         }
  144.     fprintf(out, "\n");
  145. }
  146.