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