-
Notifications
You must be signed in to change notification settings - Fork 0
/
hmmm.c
166 lines (146 loc) · 3.59 KB
/
hmmm.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* hmmm.c
*
* A simpleminded word generator based on hidden markov models
* by Jonathan H N Chin <[email protected]>, 22 July 2000
*
* Given a list, one word per line, it will output "similar" words.
*
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#define SIZE 28 /* [a-z] + space + terminator */
/* declare these globally; they don't fit on the stack... */
unsigned short A[SIZE][SIZE][SIZE][SIZE][SIZE];
unsigned max[SIZE][SIZE][SIZE][SIZE];
int verbose = 0;
void chatter(char *stuff){
if (verbose){
fprintf(stderr, stuff);
fflush(stderr);
}
}
int main(int argc, char *argv[]){
char *spinner[] = {"\b-", "\b\\", "\b|", "\b/"};
int s = 0, words = 0, input, count, depth, seed;
int gdepth = SIZE, hdepth = SIZE, idepth = SIZE;
int g, h, i, j, k;
if (argc==5 && !strcmp("-v", argv[1])){
verbose = 1;
}
chatter("\n A simpleminded random wordalike generator.\n");
if (argc<4 || argc>5 || (argc==5 && !verbose)){
fprintf(stderr,
"\n Usage:"
"\n hmmm [-v] COUNT DEPTH SEED <WORDS\n"
"\n where:"
"\n -v = be verbose"
"\n COUNT = how many words to generate"
"\n DEPTH = integer from 1..4 inclusive"
"\n SEED = a random integer"
"\n WORDS = a list, one item per line, "
"containing [A-Za-z ]\n"
"\n Warning:"
"\n needs nearly 70Mb of RAM to run.\n\n");
fflush(stderr);
exit(1);
}
count = atoi(argv[argc==4 ? 1 : 2]);
if (count<1){
chatter("\n bogus count\n");
count = 1;
}
depth = atoi(argv[argc==4 ? 2 : 3]);
if (depth<1 || depth>4){
chatter("\n bogus depth\n");
depth = 3;
}
switch (depth){
/* fallthrough */
case 1: idepth = 1;
case 2: hdepth = 1;
case 3: gdepth = 1;
}
seed = atoi(argv[argc==4 ? 3 : 4]);
if (seed<0){
chatter("\n bogus seed\n");
seed = 0;
}
/*-------------------------------------------------------------------------*/
if (verbose){
fprintf(stderr, " count=%d, depth=%d, seed=%d\n",
count, depth, seed);
fflush(stderr);
}
/*=========================================================================*/
chatter(" reading data ");
g = h = i = j = k = 0;
while ((input = getchar())!=EOF){
input = tolower(input);
if (isalpha(input)){
max[g][h][i][j] += 1;
A[g][h][i][j][1+input-'a'] += 1;
if (depth>3) g = h;
if (depth>2) h = i;
if (depth>1) i = j;
j = 1+input-'a';
} else if (input==' ') {
max[g][h][i][j] += 1;
A[g][h][i][j][SIZE-1] += 1;
if (depth>3) g = h;
if (depth>2) h = i;
if (depth>1) i = j;
j = SIZE-1;
} else if (input=='\n') {
max[g][h][i][j] += 1;
A[g][h][i][j][0] += 1;
g = h = i = j = 0;
chatter(spinner[s]);
s = (s+1)%4;
words++;
}
}
A[0][0][0][0][0] = 0; /* don't care about empty lines */
/*-------------------------------------------------------------------------*/
if (verbose){
fprintf(stderr, "\b\b- got %d line%s of input",
words,
words==1 ? "" : "s");
fflush(stderr);
}
/*=========================================================================*/
chatter("\n generating words\n\n");
srand(seed);
while (count-->0){
float n, where;
g = h = i = j = k = 0;
loop:
if (depth>3) g = h;
if (depth>2) h = i;
if (depth>1) i = j;
j = k;
n = ((float)rand())/((float)RAND_MAX);
for (k = where = 0; k<SIZE; k++){
where += ((float)A[g][h][i][j][k]/max[g][h][i][j]);
if (where>=n)
break;
}
if (k==0){
putchar('\n');
} else {
int letter = k+'a'-1;
if (islower(letter)){
putchar(letter);
goto loop;
} else if (k==(SIZE-1)){
putchar(' ');
goto loop;
} else {
chatter("Internal Error!\n");
exit(2);
}
}
}
exit(0);
}