-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheliza.c
218 lines (176 loc) · 5.84 KB
/
eliza.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
// Found on hackchina.com
// Tidied up a little by David Bolton, http://cplus.about.com
// Tidied up even more by Timm Knape, http://www.knp.de
// This is in the public domain under MIT license
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
/* CONFIGURATION
****************/
#define MAX_INPUT_BUFFER_SIZE 80
#define MAX_REMEMBERED_LINES_COUNT 100
#define MAX_TOKEN_SIZE 80
const int SHORT_ANSWER_LENGTH = 11;
const int VERY_SHORT_ANSWER_LENGTH = 3;
/* GENERIC RESPONSES
********************/
void print_generic_response() {
static const char *responses[] = {
"How are you this beautiful day?",
"Did you have a happy childhood?",
"Did you hate your father?",
"Did you have a good friend?",
"Did you like your friend?",
"What do you think about your friendship?",
"I'm not sure I understand.",
NULL
};
static int next = 0;
if (!responses[next]) next = 0;
puts(responses[next++]);
}
/* FABRICATED ANSWERS
*********************/
const char *lookup_fabricated_answer(const char *token) {
static const char *answers[] = {
"no", "Tell me more about the detail.",
"yes", "Tell me more about the detail.",
"fuck", "Don't be so rude again!",
"shit", "Don't be so rude again!",
"you", "Let's not talk about me.",
"think", "Why do you think that?",
"hate", "So you hate something -- tell me more.",
"what", "Why do you ask?",
"want", "Why do you want that?",
"need", "We all need many things -- is this special?",
"why", "Remember, therapy is good for you.",
"know", "How do you know that?",
"murder", "I don't like killing.",
"kill", "It is wrong to kill.",
"jerk", "Don't ever call me a jerk!",
"can't", "Don't be negative -- be positive.",
"failure", "Strive for success.",
"never", "Don't be negative -- be positive.",
"unhappy", "Why are you unhappy?",
"beautiful", "I'm sure you like her, don't you?",
"like", "Why do you like that?",
"love", "Remember, love everything what you love.",
NULL
};
int index = 0;
while (answers[index]) {
if (!strcmp(answers[index], token)) return answers[index + 1];
index += 2;
}
return NULL;
}
/* REMEMBERED LINES
*******************/
char remembered_lines[MAX_REMEMBERED_LINES_COUNT][MAX_INPUT_BUFFER_SIZE];
int next_remembered_line_enter_index = 0;
int next_remembered_line_return_index = 0;
void remember_line(const char *line) {
if (next_remembered_line_enter_index == MAX_REMEMBERED_LINES_COUNT) {
next_remembered_line_enter_index = 0;
}
strncpy(remembered_lines[next_remembered_line_enter_index], line, MAX_INPUT_BUFFER_SIZE);
++next_remembered_line_enter_index;
}
const char *get_old_remembered_line() {
if (next_remembered_line_enter_index != next_remembered_line_return_index) {
int result = next_remembered_line_return_index;
++next_remembered_line_return_index;
if (next_remembered_line_return_index == MAX_REMEMBERED_LINES_COUNT) {
next_remembered_line_return_index = 0;
}
return remembered_lines[result];
}
return NULL;
}
bool is_line_remebered(const char *line) {
for (int index = 0; index < MAX_REMEMBERED_LINES_COUNT; ++index) {
if (!strcmp(line, remembered_lines[index])) return true;
}
return false;
}
/*
* TOKENIZER
************/
const char *position_in_input;
const char *next_token() {
static char token[MAX_TOKEN_SIZE];
char *position_in_token = token;
/* skip spaces */
while (*position_in_input && *position_in_input <= ' ') ++position_in_input;
if (!*position_in_input) { // end of input
return NULL;
}
if (strchr(".,;!?", *position_in_input)) { // return punctuation
*position_in_token++ = *position_in_input++;
*position_in_token = 0;
return token;
}
// read word
while (*position_in_input && !strchr(" \n\r.,;?!", *position_in_input)) {
if (position_in_token != token + MAX_TOKEN_SIZE - 2) {
*position_in_token++ = tolower(*position_in_input++);
} else {
++position_in_input;
}
}
*position_in_token = 0;
return token;
}
/*
* CREATE THE DOCTOR'S RESPONE
******************************/
void respond(char *input) {
if (strlen(input) < VERY_SHORT_ANSWER_LENGTH) { // short answer
const char *remembered = get_old_remembered_line();
if (remembered) {
printf("You just said: %s", remembered);
puts("Tell me more.");
}
else {
print_generic_response();
}
return;
}
if (is_line_remebered(input)) {
puts("Stop repeating yourself!");
return;
}
if (strlen(input) > SHORT_ANSWER_LENGTH) { // remember line
remember_line(input);
}
position_in_input = input;
for (;;) {
const char *token = next_token();
if (!token) break;
const char *fabricated_answer = lookup_fabricated_answer(token);
if (fabricated_answer) {
puts(fabricated_answer);
return;
}
}
// comment of last resort
if (strlen(input) > SHORT_ANSWER_LENGTH) {
puts("It's seem intersting, tell me more ...");
} else {
puts("Tell me more ...");
}
}
int main(int argc, char **argv) {
char input_buffer[MAX_INPUT_BUFFER_SIZE];
print_generic_response();
for (;;) {
printf(": ");
fgets(input_buffer, sizeof(input_buffer), stdin);
if (strnstr(input_buffer, "bye", MAX_INPUT_BUFFER_SIZE) == input_buffer) {
puts("Your bill will be mailed to you.");
break;
}
respond(input_buffer);
}
}