-
Notifications
You must be signed in to change notification settings - Fork 0
/
markov_chain.h
148 lines (127 loc) · 4.59 KB
/
markov_chain.h
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
#ifndef _MARKOV_CHAIN_H
#define _MARKOV_CHAIN_H
#include "linked_list.h"
#include <stdio.h> // For printf(), sscanf()
#include <stdlib.h> // For exit(), malloc()
#include <stdbool.h> // for bool
#define ALLOCATION_ERROR_MASSAGE \
"Allocation failure: Failed to allocate new memory\n"
/***************************/
/* insert typedefs here */
/***************************/
typedef struct NextNodeCounter NextNodeCounter;
typedef struct MarkovNode MarkovNode;
typedef struct MarkovChain MarkovChain;
typedef void (*VoidPointer) (void *pointer);
typedef int (*CmpFunc) (void *p1, void *p2);
typedef void *(*CpyFunc) (void *pointer);
typedef bool (*IsLast) (void *p);
/***************************/
/***************************/
/* STRUCTS */
/***************************/
typedef struct MarkovNode
{
void *data;
NextNodeCounter *possible_words_arr;
int length;
int total_words;
} MarkovNode;
typedef struct NextNodeCounter
{
MarkovNode *markov_node;
int frequency;
} NextNodeCounter;
/* DO NOT ADD or CHANGE variable names in this struct */
typedef struct MarkovChain
{
LinkedList *database;
// pointer to a func that receives data from a generic type and prints it
// returns void.
VoidPointer print_func;
// pointer to a func that gets 2 pointers of generic data type(same one)
// and compare between them */
// returns: - a positive value if the first is bigger
// - a negative value if the second is bigger
// - 0 if equal
CmpFunc comp_func;
// a pointer to a function that gets a pointer of generic data type and
// frees it. returns void.
VoidPointer free_data;
// a pointer to a function that gets a pointer of generic data type and
// returns a newly allocated copy of it returns a generic pointer.
CpyFunc copy_func;
// a pointer to function that gets a pointer of generic
// data type and returns:
// - true if it's the last state.
// - false otherwise.
IsLast is_last;
} MarkovChain;
/**
* Get one random state from the given markov_chain's database.
* @param markov_chain
* @return
*/
MarkovNode *get_first_random_node (MarkovChain *markov_chain);
/**
* Choose randomly the next state, depend on it's occurrence frequency.
* @param state_struct_ptr MarkovNode to choose from
* @return MarkovNode of the chosen state
*/
MarkovNode *get_next_random_node (MarkovNode *state_struct_ptr);
/**
* Receive markov_chain, generate and print random sentence out of it. The
* sentence most have at least 2 words in it.
* @param markov_chain
* @param first_node markov_node to start with, if NULL- choose a random
* markov_node
* @param max_length maximum length of chain to generate
*/
void generate_random_sequence (MarkovChain *markov_chain, MarkovNode *
first_node, int max_length);
/**
* Free markov_chain and all of it's content from memory
* @param markov_chain markov_chain to free
*/
void free_markov_chain (MarkovChain **markov_chain);
/**
* 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);
/**
* 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);
/**
* 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 markov_node wrapping given data_ptr in given chain's database
*/
Node *add_to_database (MarkovChain *markov_chain, void *data_ptr);
/**
* Initiliaze a new markov chain with the generatic parameters as folloows:
* @param print_func
* @param comp_func
* @param free_func
* @param copy_func
* @param is_last_func
* @return a pointer to our new markov chain.
*/
MarkovChain *chain_initialize (VoidPointer print_func, CmpFunc comp_func,
VoidPointer free_func, CpyFunc copy_func,
IsLast is_last_func);
#endif /* markovChain_h */