-
Notifications
You must be signed in to change notification settings - Fork 7
/
Exercise-3-4.markov-chain-without-sentinel.c
161 lines (143 loc) · 3.82 KB
/
Exercise-3-4.markov-chain-without-sentinel.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
enum
{
NPREF = 1, // number of preifx words
NHASH = 4093, // size of state hash table array
MAXGEN = 10000 // maximum words generated
};
enum
{
MULTIPLIER = 31
};
typedef struct State State;
typedef struct Suffix Suffix;
struct State // prefix + suffix list
{
char *pref[NPREF]; // prefix words
Suffix *suf; // list of suffixes
State *next; // next in hash table
};
struct Suffix // list of suffixes
{
char *word; // suffix
Suffix *next; // next in list of suffixes
};
State *statetab[NHASH]; // hash table of states
// hash: compute hash value for array of NPREF strings
unsigned int hash(char *s[NPREF])
{
unsigned int h;
unsigned char *p;
int i;
h = 0;
for (i = 0; i < NPREF; i++)
for (p = (unsigned char *)s[i]; *p != '\0'; p++)
h = MULTIPLIER * h + *p;
return h % NHASH;
}
// lookup: search for prefix; create if requested.
// returns pointer if present or created; NULL if not.
// creation doesn't strdup so strings mustn't change later.
State *lookup(char *prefix[NPREF], int create)
{
int i, h;
State *sp = NULL;
h = hash(prefix);
for (sp = statetab[h]; sp != NULL; sp = sp->next)
{
for (i = 0; i < NPREF; i++)
if (strcmp(prefix[i], sp->pref[i]) != 0)
break;
if (i == NPREF) // found it
return sp;
}
if (create)
{
sp = (State *)malloc(sizeof(State));
for (i = 0; i < NPREF; i++)
sp->pref[i] = prefix[i];
sp->suf = NULL;
sp->next = statetab[h];
statetab[h] = sp;
}
return sp;
}
// addsuffix: add to state. suffix must not change later
void addsuffix(State *sp, char *suffix)
{
Suffix *suf;
suf = (Suffix *)malloc(sizeof(Suffix));
suf->word = suffix;
suf->next = sp->suf;
sp->suf = suf;
}
// add: add word to suffix list, update prefix
void add(char *prefix[NPREF], char *suffix)
{
State *sp;
sp = lookup(prefix, 1); // create if not found
addsuffix(sp, suffix);
// move the words down the prefix
memmove(prefix, prefix + 1, (NPREF - 1) * sizeof(prefix[0]));
prefix[NPREF - 1] = suffix;
}
// build: read input, build prefix table
void build(char *prefix[NPREF], FILE *f, char *start[NPREF], char *end[NPREF])
{
char buf[100], fmt[10];
// create a format string; %s could overflow buf
sprintf(fmt, "%%%ds", sizeof(buf) - 1);
int i = 0;
while (fscanf(f, fmt, buf) != EOF)
{
if (i < NPREF)
{
prefix[i] = start[i] = strdup(buf);
i++;
continue;
}
add(prefix, strdup(buf));
}
}
char NOWWORD[] = "\n"; // cannot appear as real word
// generate: produce output
void generate(int nwords, char *start[NPREF], char *end[NPREF])
{
State *sp;
Suffix *suf;
char *prefix[NPREF], *w;
int i, nmatch;
for (i = 0; i < NPREF; i++) // reset initial prefix
{
prefix[i] = start[i];
printf("%s ", start[i]);
}
for (i = 0; i < nwords; i++)
{
sp = lookup(prefix, 0);
if (sp == NULL)
break;
nmatch = 0;
for (suf = sp->suf; suf != NULL; suf = suf->next)
if (rand() % ++nmatch == 0) // probability = 1/nmatch
w = suf->word;
if (strcmp(w, NOWWORD) == 0)
break;
printf("%s ", w);
memmove(prefix, prefix + 1, (NPREF - 1) * sizeof(prefix[0]));
prefix[NPREF - 1] = w;
}
printf("\nnwords: %d\n", nwords);
}
// markov main: markov-chain random text generation
int main(int argc, char const *argv[])
{
int nwords = MAXGEN;
char *prefix[NPREF]; // current input prefix
char *start[NPREF], *end[NPREF];
build(prefix, stdin, start, end);
generate(nwords, start, end);
return 0;
}