forked from micro222/chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle.c
570 lines (444 loc) · 14.3 KB
/
handle.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#include "handle.h"
void handle_class_statement(char*user_subject, char*user_class) {
/*
example: a cat is an animal(value)
Posibilities:
db action user action
-------------------------- ------------ --------------------------
2 cat is unknown add to db "I'll take note of that"
1 animal is unknown "what's an animal"
3 cat is known, class = animal "I know"
4 cat known, class != animal "no it's a ____"
5 info is unknown. Add to db
// 1 class doesn't exist // i dont know what an animal is
// 2 subject doesnt exist, class exists // ill take note of that
// 3 subject exists, class is the same // "i knew that"
// 4 subject exists, class is different // no its not
*/
char value[20];
char key[20];
int result;
// 1 is "animal" in the database?
if(db_check(user_class) != FOUND) {
printf("I'm not familiar with %s\n", user_class);
return;
}
// preparatiom for 2, 3 & 4
// ex: lookup "cat"
sprintf(key, "%s > class", user_subject);
result = db_lookup(key, value);
// 2)
if(result == NOT_FOUND) {
sprintf(key, "%s > class", user_subject);
db_add_pair(key, user_class);
printf("I'll take a note of that\n");
return;
}
// Note: At this point, "cat" was found in the database.
// We just need to check if the info matches or not.
// 3) states what already is known
if(strcmp(user_class, value) == 0) {
printf("I already know that\n");
return;
}
// 4) contradictory info
else {
printf("no, it's a %s\n", value);
return;
}
}
//--------------------------------------------------
void handle_color_statement(char* user_subject,char* user_color) {
/*
example: grass is green
Posibilities:
1 grass is unknown "I dont know what grass is"
2 grass can't have a color
3 grass is known. no color is listed
4 grass is known and already associated with the given color
5 grass is known, but a different color is listed
*/
char key[20];
char value1[20];
char value2[20];
int subject_result;
char user_class;
int color_result;
if(DEBUG) printf("handle_color ");
// 1 is "grass" in the database?
if(db_check(user_subject) != FOUND) {
printf("I'm not familiar with %s\n", user_subject);
return;
}
// 2 is "grass" something that can have a color?
if(db_root_check(user_subject, "substance")!=MATCH && db_root_check(user_subject, "object") != MATCH) {
printf("I don't think %s can have a color\r\n", user_subject);
return;
}
// 3 is grass already associated with that color? if not, add it
sprintf(key, "%s > color", user_subject);
color_result = db_lookup(key, value1);
if(color_result == NOT_FOUND) {
sprintf(key, "%s > color", user_subject);
db_add_pair(key, user_color);
printf("I'll take a note of that\n");
return;
}
// 4 if already associated with the given color
if(strcmp(user_color, value1) == 0) {
printf("I already know that\n");
return;
}
// 5 if associated with a different color
else printf("no, it's %s\n", value1);
}
//--------------------------------------------------
void handle_class_question(char* subject) {
int result;
char value[20];
char key[20];
sprintf(key, "%s > class", subject);
result = db_lookup(key, value);
if(result==FOUND) printf("It's a %s\n", value);
else printf("I've never heard of ""%s""\n", subject);
}
//------------------------------------------------------
void handle_color_question(char* subject) {
int result;
char value[20];
char key[60];
sprintf(key, "%s > color", subject);
result = db_lookup(key, value);
if(result == FOUND) {
printf("%s\n", value);
return;
} else {
printf("I don't know\n");
return;
}
//else if(result==3)printf("I've never heard of %s\n",key);
// else printf(" I don't know (result %d)\n", result);
}
//----------------------------------------------------
void handle_color_confirmation_question(char* subject, char*value1) {
int result;
char value[20];
char key[60];
sprintf(key, "%s > color", subject);
db_lookup(key, value);
result = strcmp(value, value1);
if (result == 0) {
printf("yes\n");
} else printf("no\n");
}
//----------------------------------------------------
void handle_rating_statement(char* key, char* s2, char* rating) {
//
// in: firstname, firstname or object or substance, rating:0-9
//
// examples:
// i like beer
// 1) i'll take note of that
// 2) what is beer?
// 3) i already knew that
// 4) but I thought you hated beer
// i like jane
// 1) rating added
// 2) i know nothing of this "jane" you speak of
// jane likes beer
// 1) rating added
// 2) i know nothing of this "jane" you speak of
// 3) i know nothing of this "beer" you speak of
// 4)
//
// Problem: will make multiple entries
// second parameter
//
// known person? add info
// unknown person? dont know this person
// known object? add info
// known substance? add info
// unknown
char pn[20]="rating-";
// char value[20];
int result, result2;
char id1[20],id2[20];
int person = FALSE;
// So, take example 2. At this point we know that neither of the words have been checked.
// We'll start by seeing if the first word is the first name of someone we know.
// We can use the get id function for that
// If known, take note that this is a person and continue on, if not, send a message and exit
result = db_get_id(key);
if(result != FOUND) {
result = db_root_check(key,"firstname");
if(result==FOUND) printf("I don't know%s\n", key);
else printf("%s?\n", key);
return;
}
// Next we'll look at the 2nd parameter and see if it's a known person
//
result = db_get_id(s2); // a known entity?
result2 = db_root_check(s2,"firstname"); // a person?
if(result==FOUND && result2==FOUND) {
person = TRUE;
goto add_info;
}
if(result!=FOUND && result2==FOUND) {
printf("I don't know %s\n", s2);
return;
}
// Check if the 2nd parameter a object or substance?
result = db_root_check(s2, "object");
result2 = db_root_check(s2, "substance");
if(result == NO_MATCH) {
printf("I don't know what %s is\n", s2);
return;
}
printf("*%d*\n", result);
if(result != FOUND && result2 != FOUND) {
printf("I dont see how %s makes sense\n", s2);
return;
}
//---
// If we've made if this far we have a valid sentence and we can store
// the info
add_info:
if(person==TRUE)strcat(pn, id2);
else strcat(pn, s2);
// result = db_lookup(id1, pn, value);
//if(result == SUBJECT_NOT_FOUND){
// db_add_subject_and_property(id1,pn,rating);
// db_add_property(id1,"firstname",key);
// printf("name and rating added\n");
// }
// else
if(result==5) {
// db_add_property(id1, pn, rating); // change this
printf("rating added\n");
} else if(result == FOUND) printf("I already know that\n");
else printf("error, result code:%d\n",result);
}
//--------------------------------------------------
void handle_location_question(char* subject) {
int result;
char value[20];
char key[60];
// DO WE KNOW THE ANSWER?
// strcpy(key, subject);
// strcat(key, " > location");
sprintf(key, "%s > location", subject);
result = db_lookup(key, value);
if(result==FOUND) {
printf("%s\n", value);
return;
}
// DO WE KNOW THE SUBJECT BUT NOT THE ANSWER?
//strcpy(key, subject);
//strcat(key, " > class");
sprintf(key, "%s > class", subject);
result = db_lookup(key, value);
if(result==FOUND) {
printf(" I don't know where %s is\n", subject);
return;
}
// WE DON'T KNOW THE SUBJECT
printf(" I never heard of %s\n", key);
return;
}
//-------------------------------------------------------
void handle_ability_question(char* key, char* value) {
int result;
char* temp[40];
char value2[20];
result = db_root_check(key,"object");
if(result == NOT_FOUND) {
printf(" I never heard of %s\n", key);
return;
}
if(result != MATCH) {
printf("%s is not an object\n", key);
return;
}
if(db_root_check(value,"action")!= MATCH) {
printf("%s is not an action\n",key);
return;
}
sprintf(temp,"%s > ability > %s", key, value);
result = db_lookup(temp, value2);
printf("RESULT: %d\r\n", result);
}
//------------------------------------------------------
// key is someones first name
// p is what the rating is of
//
void handle_rating_question(char* key, char* p) {
int result;
char value[20];
char id[20];
char temp[20]="rating-";
result = db_get_id(key);
//printf("result %d %s %s %s ", result, key, p, id);
//return;
if(result !=0) {
printf("I dont know %s\n", key);
return;
}
//printf("result %d ", result);
// return;
//check if the subject of rating is known
// result = db_subject_search(p);
if(result!=0) {
printf("What is %s?\n", key);
return;
}
// get rating if any
strcat(temp, p);
// result = db_search(key,temp,value);
if(result==0) printf("%s\n", value);
else printf("I dont know (code:%d)\n", result);
}
//----------------------------------------
void handle_list_question(char* subject) {
}
//--------------------------------------------------
void handle_attribute_statement(char* user_subject,char* user_attribute) {
/*
example: grass is green
Posibilities:
1 grass is unknown "reply: "i dont know what grass is"
2 grass is not an object or substance
3 green is unknown
4 green is not an attribute
5 info already known
6 info is contradictory
7 ok to add to db
*/
int result;
int result1;
int result2;
char value[20];
char key[60];
int subject_result;
char subject_class[20];
char db_class[20];
char attribute_type[20];
char db_attribute[20];
// 1) ex: is "grass" in database?
sprintf(key, "%s > class", user_subject);
subject_result = db_lookup(key, subject_class);
// If subject_result === UNKNOWN, I don't know what %s is
// 2) Is grass an object or substance?
result1 = db_root_check(user_subject, "object");
result2 = db_root_check(user_subject, "substance");
if ((result1 == NOT_FOUND) && (result2 == NOT_FOUND)) {
printf("I don't think %s can have such an attribute\n", user_subject);
return;
}
// 3) is green in db?
sprintf(key, "%s > class", user_attribute);
if(db_lookup(key, db_class) == NOT_FOUND) {
printf("I'm unfamiliar with %s\n", user_attribute);
return;
}
// 4) is "green" an attribute
if(db_root_check(user_attribute, "attribute") == NOT_FOUND) {
printf("%s is not an attribute\n", user_attribute);
return;
}
// Preparation for 5, 6 and 7
// Get the type of the attribute. ex: "green is a color"
sprintf(key, "%s > class", user_attribute); // assemble key, ex: "green > class"
db_lookup(key, attribute_type); // ex: returns "color"
// Get the attribute from db if any
sprintf(key, "%s > %s", user_subject, attribute_type); // assemble key, ex: "green > color"
result = db_lookup(key, db_attribute); // ex: returns "green"
// 5) Is the attribute already known?
if(strcmp(user_attribute, db_attribute) == 0) { // compare "green"(what the user typed) with green(from db)
printf("I already know that\n");
return;
}
// 6 Check if the attrubute contradicts what's in the db
if(result == FOUND) {
printf("no, it's %s\n", db_attribute);
return;
}
// 7 Since posiblities 1 to 6 have been eliminated, it's ok to add the info to the db
// Determine attribute type
// Add to db
sprintf(key, "%s > %s", user_subject, value); // assemble key, ex: grass > color
db_add_pair(key, user_attribute);
printf("I'll take a note of that\n");
return;
}
//--------------------------------------------------
// WORK IN PROGRESS
//
// handle hi, hello
//
//
void handle_greetings(void) {
// if not logged in
if(current_user_id == 0) {
printf("hi, what is your name?\r\n");
}
// if logged in
else {
printf("hi %s\r\n", current_user_name);
}
}
/*
void handle_is_statement(char*user_subject, char*user_attribute) {
UNDER CONSTRUCTION, will likely be abandoned
example: a cat is an animal(value)
Posibilities:
db action user action
-------------------------- ------------ --------------------------
cat is unknown add to db "I'll take note of that"
animal is unknown "what's an animal"
cat is known, class = animal "I know"
cat known, class != animal "no it's a ____"
// class doesn't exist // i dont know what an animal is
// subject doesnt exist, class exists // ill take note of that
// subject exists, value is the same // "i knew that"
// subject exists, value is different // no its not
At some point we need to figure out if this is a class or an attribute.
The syntax tells us what to expect, then we have to verify it.
class or attribute?
B)if(root_check(p1) == attribute) deal with it, goto label1
A)if(p1 is in db) it's a class so deal with it,
if yes to both,
label1:
cat in db?
char value[20];
char key[20];
int result;
// 1 is "animal" in the database?
if(db_check(user_class) != FOUND) {
printf("I'm not familiar with %s\n", user_class);
return;
}
//2,3&4
// ex: lookup "cat"
sprintf(key, "%s > class", user_subject);
result = db_lookup(key, value);
// 2)
if(result == NOT_FOUND) {
sprintf(key, "%s > class", user_subject);
db_add_pair(key, user_class);
printf("I'll take a note of that\n");
return;
}
// Note: At this point, "cat" was found in the database.
// We just need to check if the info matches or not.
// 3) states what already is known
if(strcmp(user_class, value) == 0) {
printf("I already know that\n");
return;
}
// 4) contradictory info
else {
printf("no, it's a %s\n", value);
return;
}
}
*/