| 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 |
int main (int argc, char ** argv) { |
| 24 |
char buf[100]; |
| 25 |
int j; |
| 26 |
int k1, k2, k3; |
| 27 |
int c1, c2, c3; |
| 28 |
char s1[2], s2[2], s3[2]; |
| 29 |
int argno, nfiles; |
| 30 |
|
| 31 |
for (c1=0; c1 < 26; c1++) { /* Initialize arrays to zero */ |
| 32 |
sing[c1] = 0; |
| 33 |
for (c2=0; c2 < 26; c2++) { |
| 34 |
duos[c1][c2] = 0; |
| 35 |
for (c3=0; c3 < 26; c3++) { |
| 36 |
tris[c1][c2][c3] = 0; |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
s1[1] = '\0'; |
| 41 |
s2[1] = '\0'; |
| 42 |
s3[1] = '\0'; |
| 43 |
nfiles = 0; /* count of files read */ |
| 44 |
|
| 45 |
if (argc < 2) { |
| 46 |
printf (" USAGE: loadtris /usr/dict/words ..."); |
| 47 |
exit (1); |
| 48 |
} |
| 49 |
for (argno = 1; argno < argc; argno++) { |
| 50 |
if ((fp = fopen (argv[argno], "r")) == NULL) { |
| 51 |
printf ("** file %s not found\n", argv[argno]); |
| 52 |
break; |
| 53 |
} |
| 54 |
nfiles++; |
| 55 |
while (fgets (buf, sizeof (buf), fp)) { |
| 56 |
j = 0; /* j indexes the input */ |
| 57 |
k2 = -1; /* k1, k2 are coords of previous letter */ |
| 58 |
k1 = -1; |
| 59 |
while (buf[j]) { /* until we find the null char.. */ |
| 60 |
k3 = buf[j]; /* Pick out a letter from the input */ |
| 61 |
if (k3 > 'Z') { |
| 62 |
k3 = k3 - 'a'; /* map from a-z to 0-25 */ |
| 63 |
} |
| 64 |
else { |
| 65 |
k3 = k3 - 'A'; /* map from A-Z to 0-25 */ |
| 66 |
} |
| 67 |
if (k3 >= 0 && k3 <= 25) { /* valid subscript? */ |
| 68 |
if (k1 >= 0) { /* do we have 3 letters? */ |
| 69 |
tris[k1][k2][k3]++; /* count */ |
| 70 |
sigma++; /* grand total */ |
| 71 |
if (tris[k1][k2][k3] > max) { |
| 72 |
max = tris[k1][k2][k3]; |
| 73 |
m1 = k1; /* note largest cell.. */ |
| 74 |
m2 = k2; /* .. for interest */ |
| 75 |
m3 = k3; |
| 76 |
} |
| 77 |
} |
| 78 |
if (k2 >= 0) { |
| 79 |
duos[k2][k3]++; /* count 2-letter pairs */ |
| 80 |
} |
| 81 |
sing[k3]++; /* count single letter frequency */ |
| 82 |
k1 = k2; /* shift over */ |
| 83 |
k2 = k3; |
| 84 |
} |
| 85 |
j++; |
| 86 |
} /* while buf[j] */ |
| 87 |
} /* while fgets */ |
| 88 |
fclose (fp); |
| 89 |
} /* for argno */ |
| 90 |
|
| 91 |
if (nfiles) { /* find any input? */ |
| 92 |
printf ("/* BEGIN INCLUDE FILE .. trigram.h */\n"); /* Multics style */ |
| 93 |
printf ("\n"); |
| 94 |
printf ("const long sigma = %ld;\n", sigma); |
| 95 |
/* (for my /usr/dict/words it is 125729, fits in a long) */ |
| 96 |
|
| 97 |
/* For interest print out the most frequent entry. */ |
| 98 |
/* (for my /usr/dict/words it is 863 = ATE, showing that a short works OK) */ |
| 99 |
s1[0] = m1 + 'a'; |
| 100 |
s2[0] = m2 + 'a'; |
| 101 |
s3[0] = m3 + 'a'; |
| 102 |
printf ("const short maxcell = %ld; /* %s%s%s */\n", max, s1, s2, s3); |
| 103 |
|
| 104 |
printf ("const short sing[26] = {"); |
| 105 |
for (c1=0; c1 < 26; c1++) { |
| 106 |
printf ("%d, ", sing[c1]); |
| 107 |
} |
| 108 |
printf ("};\n"); /* oops, ends in comma closebrace */ |
| 109 |
|
| 110 |
printf ("const short duos[26][26] = {"); |
| 111 |
for (c1=0; c1 < 26; c1++) { |
| 112 |
s1[0] = c1+'A'; |
| 113 |
printf ("\n{/* %s */ ", s1); |
| 114 |
for (c2=0; c2 < 26; c2++) { |
| 115 |
printf ("%d, ", duos[c1][c2]); |
| 116 |
} |
| 117 |
printf("}"); |
| 118 |
if (c1!=25) printf(","); |
| 119 |
} |
| 120 |
printf ("};\n"); /* oops, ends in comma closebrace */ |
| 121 |
|
| 122 |
printf ("const short tris[26][26][26] = {"); |
| 123 |
for (c1=0; c1 < 26; c1++) { |
| 124 |
printf("{"); |
| 125 |
for (c2=0; c2 < 26; c2++) { |
| 126 |
s1[0] = c1+'A'; |
| 127 |
s2[0] = c2+'A'; |
| 128 |
printf ("\n{/* %s %s */ ", s1, s2); |
| 129 |
for (c3=0; c3 < 26; c3++) { |
| 130 |
printf ("%d, ", tris[c1][c2][c3]); |
| 131 |
} |
| 132 |
printf("}"); |
| 133 |
if (c2!=25) printf(","); |
| 134 |
} |
| 135 |
printf("}"); |
| 136 |
if (c1!=25) printf(","); |
| 137 |
} |
| 138 |
printf ("};\n"); /* comma closebrace again, fix later */ |
| 139 |
printf ("/* END INCLUDE FILE .. trigram.h */\n"); |
| 140 |
} |
| 141 |
exit (0); |
| 142 |
} |
| 143 |
|