-
Notifications
You must be signed in to change notification settings - Fork 0
/
markov_chain.c
349 lines (334 loc) · 9.83 KB
/
markov_chain.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "linked_list.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "markov_chain.h"
#define ALLOCATION_ERROR_MARKOVNODE \
"Allocation failure: fail to allocate memory for markovnode\n"
#define ADD_FAILURE 1
#define TRUE true
#define FALSE false
/**
* Get random number between 0 and max_number [0, max_number).
* @param max_number maximal number to return (not including)
* @return Random number
*/
int get_random_number (int max_number)
{
return rand () % max_number;
}
/**
* The function gets the word as data of the current MarkovNode
* returns a pointer to the current new markov node
*/
MarkovNode *new_markov_node (void *data, CpyFunc *copy_func)
{
MarkovNode *new_markov_node = (MarkovNode *) malloc (sizeof (MarkovNode));
if (new_markov_node == NULL)
{
printf (ALLOCATION_ERROR_MARKOVNODE);
return NULL;
}
//?????? CHANGE THIS and i deleted the malloc because copy malooc by itself
//strcpy (str, data);
void *cpy_data = (*copy_func) (data);
new_markov_node->data = cpy_data;
new_markov_node->possible_words_arr = NULL;
new_markov_node->length = 0;
new_markov_node->total_words = 0;
return new_markov_node;
}
/**
* Get a number-i and return the i'th number in the database list
* @param markov_chain and k which is the kth node we want to return
* @return the MarkovNode
*/
MarkovNode *get_ith_node (MarkovChain *markov_chain, int k)
{
if (markov_chain != NULL)
{
Node *curr_node = markov_chain->database->first;
for (int i = 0; i < k; i++)
{
curr_node = curr_node->next;
}
return curr_node->data;
}
return NULL;
}
MarkovNode *get_first_random_node (MarkovChain *markov_chain)
{
IsLast is_last_node = markov_chain->is_last;
int random_num = get_random_number (markov_chain->database->size);
MarkovNode *curr_markov_node = get_ith_node (markov_chain, random_num);
while (is_last_node (curr_markov_node->data))
{
random_num = get_random_number (markov_chain->database->size);
curr_markov_node = get_ith_node (markov_chain, random_num);
}
return curr_markov_node;
}
MarkovNode *get_next_random_node (MarkovNode *state_struct_ptr)
{
int rnd_num = get_random_number (state_struct_ptr->total_words);
if (state_struct_ptr->possible_words_arr == NULL)
{
return NULL;
}
int i = -1;
NextNodeCounter curr_nxt_node;
while (rnd_num >= 0)
{
i++;
curr_nxt_node = state_struct_ptr->possible_words_arr[i];
rnd_num = rnd_num - (curr_nxt_node.frequency);
}
return curr_nxt_node.markov_node;
}
void generate_random_sequence (MarkovChain *markov_chain, MarkovNode *
first_node, int max_length)
{
IsLast is_last_node = markov_chain->is_last;
VoidPointer print_func = markov_chain->print_func;
while (first_node == NULL || is_last_node (first_node->data) == TRUE)
{
first_node = get_first_random_node (markov_chain);
}
MarkovNode *curr_node = first_node;
while (max_length != 0 && is_last_node (curr_node->data) == FALSE)
{
if (curr_node == first_node)
{
print_func (curr_node->data);
}
else
{
print_func (curr_node->data);
}
max_length--;
curr_node = get_next_random_node (curr_node);
if (curr_node == NULL)
{
break;
}
}
if (is_last_node (curr_node->data) != FALSE)
{
print_func (curr_node->data);
}
}
/**
* Free markov_node and all of it's content from memory
* @param free_markov_node to free
* @param free_func the function to use to free the data
*/
void free_markov_node (MarkovNode *markov_node, VoidPointer *free_func)
{
if (markov_node != NULL)
{
(*free_func) (markov_node->data);
//free the counter_list
NextNodeCounter *counter_list_arr = markov_node->possible_words_arr;
if (counter_list_arr != NULL)
{
free (counter_list_arr);
}
free (markov_node);
markov_node = NULL;
}
}
/**
* Free all of node's content from memory
* @param node to free it's content
*/
void free_content_node (Node *node, VoidPointer *free_func)
{
if (node != NULL)
{
//free the Markov node that inside of the node
free_markov_node (node->data, free_func);
node->data = NULL;
}
}
/**
* Free the linked list's content from memory
* @param LinkedList linked_list to free
*/
void free_linked_list (LinkedList *linked_list, VoidPointer *free_func)
{
if (linked_list != NULL)
{
if ((linked_list->size) >= 1)
{
Node *curr_node = linked_list->first;
Node *nxt_node = curr_node->next;
for (int i = 0; i < linked_list->size; i++)
{
free_content_node (curr_node, free_func);
free (curr_node);
curr_node = nxt_node;
if (nxt_node != NULL)
{
nxt_node = nxt_node->next;
}
}
free_content_node (curr_node, free_func);
free (curr_node);
}
//It is a single node
else
{
free_content_node (linked_list->first, free_func);
free (linked_list->first);
}
free (linked_list);
}
}
/**
* Free markov_chain and all of it's content from memory
* @param markov_chain markov_chain to free
*/
void free_markov_chain (MarkovChain **ptr_chain)
{
free_linked_list ((*ptr_chain)->database, &((*ptr_chain)->free_data));
free (*ptr_chain);
*ptr_chain = NULL;
}
/**
* Add the second markov_node to the counter list of the first markov_node.
* If already in list, update it's counter value.
* @param first_node
* @param second_node
* @param markov_chain
* @return success/failure: true if the process was successful, false if in
* case of allocation error.
*/
bool
add_node_to_counter_list (MarkovNode *first_node, MarkovNode *second_node,
MarkovChain *markov_chain)
{
CmpFunc comp_func = markov_chain->comp_func;
NextNodeCounter *curr_counter_arr;
int runner;
if (first_node->possible_words_arr != NULL)
{
curr_counter_arr = first_node->possible_words_arr;
for (int i = 0; i < first_node->length; i++)
{
//we find the second node and increase it frequency +1
if (comp_func (curr_counter_arr[i].markov_node->data, second_node->data)
== 0)
{
first_node->possible_words_arr[i].frequency++;
first_node->total_words++;
return true;
}
}
}
NextNodeCounter *curr_arr = first_node->possible_words_arr;
NextNodeCounter *new_counter_arr =
realloc (curr_arr, (first_node->length + 1)
* sizeof (NextNodeCounter));
if (new_counter_arr == NULL)
{
return false;
}
first_node->possible_words_arr = new_counter_arr;
runner = (first_node->length);
(first_node->possible_words_arr)[runner].markov_node = second_node;
(first_node->possible_words_arr)[runner].frequency = 1;
first_node->total_words++;
//our counter list of the first node grow by 1
first_node->length++;
return true;
}
/**
* Check if data_ptr is in database.If so,return the markov_node wrapping
* it in the markov_chain, otherwise return NULL.
* @param markov_chain the chain to look in its database
* @param data_ptr the state to look for
* @return Pointer to the Node wrapping given state, NULL if state not in
* database.
*/
Node *get_node_from_database (MarkovChain *markov_chain, void *data_ptr)
{
CmpFunc compare_func = markov_chain->comp_func;
Node *runner = markov_chain->database->first;
if (runner == NULL)
{
return runner;
}
while ((runner) != (markov_chain->database->last))
{
if (compare_func (runner->data->data, data_ptr) == 0)
{
return runner;
}
runner = runner->next;
}
//Comparing the last node with the data_ptr
if (compare_func (runner->data->data, data_ptr) == 0)
{
return runner;
}
//data_ptr is not in markov chain database
return NULL;
}
/**
* If data_ptr in markov_chain, return it's markov_node. Otherwise, create new
* markov_node, add to end of markov_chain's database and return it.
* @param markov_chain the chain to look in its database
* @param data_ptr the state to look for
* @return node wrapping given data_ptr in given chain's database,
* returns NULL in case of memory allocation failure.
*/
Node *add_to_database (MarkovChain *markov_chain, void *data_ptr)
{
Node *search_node = get_node_from_database (markov_chain, data_ptr);
if (search_node != NULL)
{
return search_node;
}
//Add the MarkovNode to the end and update the last node in the MarkovChain
MarkovNode *my_markov_node = new_markov_node
(data_ptr, &(markov_chain->copy_func));
if (my_markov_node == NULL)
{
return NULL;
}
//if the markov chain is empty
int add_succes = add (markov_chain->database, my_markov_node);
if (add_succes == ADD_FAILURE)
{
return NULL;
}
return markov_chain->database->last;
}
MarkovChain *chain_initialize (VoidPointer print_func, CmpFunc comp_func,
VoidPointer free_func, CpyFunc copy_func,
IsLast is_last_func)
{
MarkovChain *my_markov_chain = malloc (sizeof (MarkovChain));
if (my_markov_chain == NULL)
{
printf(ALLOCATION_ERROR_MASSAGE);
return NULL;
}
my_markov_chain->is_last = is_last_func;
my_markov_chain->print_func = print_func;
my_markov_chain->copy_func = copy_func;
my_markov_chain->comp_func = comp_func;
my_markov_chain->free_data = free_func;
LinkedList *linked_list = malloc (sizeof (LinkedList));
if (linked_list == NULL)
{
printf(ALLOCATION_ERROR_MASSAGE);
free (my_markov_chain);
return NULL;
}
my_markov_chain->database = linked_list;
my_markov_chain->database->first = NULL;
my_markov_chain->database->last = NULL;
my_markov_chain->database->size = 0;
return my_markov_chain;
}