Debian Codegraph @

loadtris.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /* This program counts trigraphs in all the dictionaries specified
2 and outputs a C++ include file of constants for gpw.C to use.
3
4 Change to a C include file by changing "const" to "static."
5 Output of this program needs postprocessing to eliminate comma closebrace,
6 see the makefile for gpw.
7
8 THVV 6/94 Coded
9 */
10
11 #include "stdio.h"
12 #include "stdlib.h"
13
14 int tris[26][26][26]; /* Trigraph frequencies */
15 int duos[26][26]; /* Bigraph frequencies */
16 int sing[26]; /* Letter frequencies */
17 long max = 0; /* largest triraph count */
18 int m1, m2, m3; /* coords of largest count */
19 long sigma = 0; /* Total letters */
20
21 FILE *fp;
22
•23 void checktris() {
24 int c1, c2, c3;
25 for(c1=0; c1<26; c1++) {
26 for(c2=0; c2<26; c2++) {
27 int hastris = 0;
28 for(c3=0; c3<26; c3++) {
29 if(tris[c1][c2][c3]) {
30 hastris = 1;
31 break;
32 }
33 }
34 if(!hastris) { /* if [c1][c2] has no tris... */
35 duos[c1][c2] = 0; /* ... do not use the duo ...*/
36 for(c3=0; c3<26; c3++) { /* ... and don't use tris */
37 sigma-=tris[c3][c1][c2];/* which are ending */
38 tris[c3][c1][c2] = 0; /* on [c1][c2] ...*/
39 }
40 }
41 }
42 }
43 }
44
45 int main (int argc, char ** argv) {
46 char buf[100];
47 int j;
48 int k1, k2, k3;
49 int c1, c2, c3;
50 char s1[2], s2[2], s3[2];
51 int argno, nfiles;
52
53 for (c1=0; c1 < 26; c1++) { /* Initialize arrays to zero */
54 sing[c1] = 0;
55 for (c2=0; c2 < 26; c2++) {
56 duos[c1][c2] = 0;
57 for (c3=0; c3 < 26; c3++) {
58 tris[c1][c2][c3] = 0;
59 }
60 }
61 }
62 s1[1] = '\0';
63 s2[1] = '\0';
64 s3[1] = '\0';
65 nfiles = 0; /* count of files read */
66
67 if (argc < 2) {
68 printf (" USAGE: loadtris /usr/dict/words ...");
69 exit (1);
70 }
71 for (argno = 1; argno < argc; argno++) {
72 if ((fp = fopen (argv[argno], "r")) == NULL) {
73 printf ("** file %s not found\n", argv[argno]);
74 break;
75 }
76 nfiles++;
77 while (fgets (buf, sizeof (buf), fp)) {
78 j = 0; /* j indexes the input */
79 k2 = -1; /* k1, k2 are coords of previous letter */
80 k1 = -1;
81 while (buf[j]) { /* until we find the null char.. */
82 k3 = buf[j]; /* Pick out a letter from the input */
83 if (k3 > 'Z') {
84 k3 = k3 - 'a'; /* map from a-z to 0-25 */
85 }
86 else {
87 k3 = k3 - 'A'; /* map from A-Z to 0-25 */
88 }
89 if (k3 >= 0 && k3 <= 25) { /* valid subscript? */
90 if (k1 >= 0) { /* do we have 3 letters? */
91 tris[k1][k2][k3]++; /* count */
92 sigma++; /* grand total */
93 if (tris[k1][k2][k3] > max) {
94 max = tris[k1][k2][k3];
95 m1 = k1; /* note largest cell.. */
96 m2 = k2; /* .. for interest */
97 m3 = k3;
98 }
99 }
100 if (k2 >= 0) {
101 duos[k2][k3]++; /* count 2-letter pairs */
102 }
103 sing[k3]++; /* count single letter frequency */
104 k1 = k2; /* shift over */
105 k2 = k3;
106 }
107 j++;
108 } /* while buf[j] */
109 } /* while fgets */
110 fclose (fp);
111 } /* for argno */
112
113 if (nfiles) { /* find any input? */
•114 checktris();
115 printf ("/* BEGIN INCLUDE FILE .. trigram.h */\n"); /* Multics style */
116 printf ("\n");
117 printf ("const long sigma = %ld;\n", sigma);
118 /* (for my /usr/dict/words it is 125729, fits in a long) */
119
120 /* For interest print out the most frequent entry. */
121 /* (for my /usr/dict/words it is 863 = ATE, showing that a short works OK) */
122 s1[0] = m1 + 'a';
123 s2[0] = m2 + 'a';
124 s3[0] = m3 + 'a';
125 printf ("const short maxcell = %ld; /* %s%s%s */\n", max, s1, s2, s3);
126
127 printf ("const short sing[26] = {");
128 for (c1=0; c1 < 26; c1++) {
129 printf ("%d, ", sing[c1]);
130 }
131 printf ("};\n"); /* oops, ends in comma closebrace */
132
133 printf ("const short duos[26][26] = {");
134 for (c1=0; c1 < 26; c1++) {
135 s1[0] = c1+'A';
136 printf ("\n{/* %s */ ", s1);
137 for (c2=0; c2 < 26; c2++) {
138 printf ("%d, ", duos[c1][c2]);
139 }
140 printf("}");
141 if (c1!=25) printf(",");
142 }
143 printf ("};\n"); /* oops, ends in comma closebrace */
144
145 printf ("const short tris[26][26][26] = {");
146 for (c1=0; c1 < 26; c1++) {
147 printf("{");
148 for (c2=0; c2 < 26; c2++) {
149 s1[0] = c1+'A';
150 s2[0] = c2+'A';
151 printf ("\n{/* %s %s */ ", s1, s2);
152 for (c3=0; c3 < 26; c3++) {
153 printf ("%d, ", tris[c1][c2][c3]);
154 }
155 printf("}");
156 if (c2!=25) printf(",");
157 }
158 printf("}");
159 if (c1!=25) printf(",");
160 }
161 printf ("};\n"); /* comma closebrace again, fix later */
162 printf ("/* END INCLUDE FILE .. trigram.h */\n");
163 }
164 exit (0);
165 }
166