-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkov_chains.c
105 lines (89 loc) · 2.19 KB
/
markov_chains.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <locale.h>
#define MAX_WORDS 1000
#define MAX_WORD_LEN 50
#define MAX_CHAIN_LEN 100
typedef struct Node {
char word[MAX_WORD_LEN];
struct Node* next[MAX_WORDS];
int next_count;
} Node;
Node* nodes[MAX_WORDS];
int node_count = 0;
Node* find_node(const char* word)
{
for (int i = 0; i < node_count; i++)
{
if (strcmp(nodes[i]->word, word) == 0)
{
return nodes[i];
}
}
return NULL;
}
Node* create_node(const char* word)
{
Node* node = (Node*)malloc(sizeof(Node));
strcpy(node->word, word);
node->next_count = 0;
nodes[node_count++] = node;
return node;
}
void add_transition(const char* word1, const char* word2)
{
Node* node1 = find_node(word1);
if (!node1) node1 = create_node(word1);
Node* node2 = find_node(word2);
if (!node2) node2 = create_node(word2);
node1->next[node1->next_count++] = node2;
}
void generate_text(const char* start_word)
{
Node* current = find_node(start_word);
if (!current) {
printf("Word %s is not exist", start_word);
return;
}
printf("%s", start_word);
for (int i = 0; i < MAX_CHAIN_LEN; i++)
{
if (current -> next_count == 0) break;
int next_index = rand() % current->next_count;
current = current->next[next_index];
printf(" %s", current->word);
}
printf("\n");
}
void build_chain(char* input_text)
{
char* word1 = strtok(input_text, " ");
char* word2;
while ((word2 = strtok(NULL, " ")) != NULL)
{
add_transition(word1, word2);
word1 = word2;
}
}
int main()
{
srand(time(NULL));
printf("Paste text\n> ");
char text[MAX_WORD_LEN * MAX_WORDS];
char inputChar;
for(int i = 0; (inputChar = getchar()) != EOF; i++)
{
text[i] = inputChar;
}
build_chain(text);
char word[MAX_WORD_LEN];
printf("Start word: ");
for (int i = 0; (inputChar = getchar()) != '\n'; i++)
{
word[i] = inputChar;
}
generate_text(word);
return 0;
}