| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#include "stdio.h" |
| 12 |
#include "stdlib.h" |
| 13 |
|
| 14 |
int tris[26][26][26]; |
| 15 |
int duos[26][26]; |
| 16 |
int sing[26]; |
| 17 |
long max = 0; |
| 18 |
int m1, m2, m3; |
| 19 |
long sigma = 0; |
| 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) { |
| 35 |
duos[c1][c2] = 0; |
| 36 |
for(c3=0; c3<26; c3++) { |
| 37 |
sigma-=tris[c3][c1][c2]; |
| 38 |
tris[c3][c1][c2] = 0; |
| 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++) { |
| 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; |
| 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; |
| 79 |
k2 = -1; |
| 80 |
k1 = -1; |
| 81 |
while (buf[j]) { |
| 82 |
k3 = buf[j]; |
| 83 |
if (k3 > 'Z') { |
| 84 |
k3 = k3 - 'a'; |
| 85 |
} |
| 86 |
else { |
| 87 |
k3 = k3 - 'A'; |
| 88 |
} |
| 89 |
if (k3 >= 0 && k3 <= 25) { |
| 90 |
if (k1 >= 0) { |
| 91 |
tris[k1][k2][k3]++; |
| 92 |
sigma++; |
| 93 |
if (tris[k1][k2][k3] > max) { |
| 94 |
max = tris[k1][k2][k3]; |
| 95 |
m1 = k1; |
| 96 |
m2 = k2; |
| 97 |
m3 = k3; |
| 98 |
} |
| 99 |
} |
| 100 |
if (k2 >= 0) { |
| 101 |
duos[k2][k3]++; |
| 102 |
} |
| 103 |
sing[k3]++; |
| 104 |
k1 = k2; |
| 105 |
k2 = k3; |
| 106 |
} |
| 107 |
j++; |
| 108 |
} |
| 109 |
} |
| 110 |
fclose (fp); |
| 111 |
} |
| 112 |
|
| 113 |
if (nfiles) { |
| •114 |
checktris(); |
| 115 |
printf ("/* BEGIN INCLUDE FILE .. trigram.h */\n"); |
| 116 |
printf ("\n"); |
| 117 |
printf ("const long sigma = %ld;\n", sigma); |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 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"); |
| 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"); |
| 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"); |
| 162 |
printf ("/* END INCLUDE FILE .. trigram.h */\n"); |
| 163 |
} |
| 164 |
exit (0); |
| 165 |
} |
| 166 |
|