forked from ChatScript/ChatScript
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtesting.cpp
9946 lines (9311 loc) · 318 KB
/
testing.cpp
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "common.h"
extern int ignoreRule;
static bool down_is = true;
bool VerifyAuthorization(FILE* in) // is he allowed to use :commands
{
if (overrideAuthorization) return true; // commanded from script
char buffer[MAX_WORD_SIZE];
if ( *authorizations == '1') // command line does not authorize
{
if (in) FClose(in);
return false;
}
// check command line params
char* at = authorizations;
char word[MAX_WORD_SIZE];
if (at) // command line given
{
if (*at == '"') ++at;
while (*at)
{
at = ReadCompiledWord(at,word);
size_t len = strlen(word);
if (word[len-1] == '"') word[len-1] = 0;
if (!stricmp(word,(char*)"all") || !stricmp(word,callerIP) || (*word == 'L' && word[1] == '_' && !stricmp(word+2,loginID))) // allowed by IP or L_loginname
{
if (in) FClose(in);
return true;
}
}
}
if (!in) return (*authorizations) ? false : true; // no restriction file
bool result = false;
while (ReadALine(buffer,in) >= 0 )
{
ReadCompiledWord(buffer,word);
if (!stricmp(word,(char*)"all") || !stricmp(word,callerIP) || (*word == 'L' && word[1] == '_' && !stricmp(word+2,loginID)) || (*word == 'P' && word[1] == '_' && strstr(loginID, word + 2))) // allowed by IP or L_loginname or P_loginname
{
result = true;
break;
}
}
FClose(in);
return result;
}
#ifndef DISCARDTESTING
static void C_Trace(char* input);
static void C_DoInternal(char* input,bool internal);
static int nooob = 0;
static int lineLimit = 0; // in abstract report lines that are longer than this...
static WORDP topLevel = 0;
static unsigned int err = 0;
static int inLine = 0;
static int inDepth = 0;
static unsigned int filesSeen;
static char directory[MAX_WORD_SIZE];
static int itemcount = 0;
static char* abstractBuffer;
static int longLines;
static uint64 verifyToken;
static bool isTracing = false;
static bool isTiming = false;
static WORDP dictUsedG;
static FACT* factUsedG;
static char* textUsedG;
static int trials;
static int nextRule = 0;
static bool stepMatch = false;
static bool keepname = false;
static short int stepOver[MAX_GLOBAL];
static char stepOut[MAX_GLOBAL];
#define ABSTRACT_SPELL 1
#define ABSTRACT_SET_MEMBER 2
#define ABSTRACT_CANONICAL 4
#define ABSTRACT_PRETTY 8
#define ABSTRACT_VP 16
#define ABSTRACT_NOCODE 32
#define ABSTRACT_STORY 64
#define ABSTRACT_RESPONDER 128
#define ABSTRACT_RESTRICTIONS (ABSTRACT_SPELL|ABSTRACT_SET_MEMBER|ABSTRACT_CANONICAL|ABSTRACT_PRETTY|ABSTRACT_VP )
static bool fromScript = false;
static void ShowTrace(unsigned int bits, bool original);
static void ShowTiming(unsigned int bits, bool original);
// prototypes
static bool DumpOne(WORDP S,int all,int depth,bool shown);
static int CountDown(MEANING T,int all,int depth,unsigned int baseStamp);
static void C_Retry(char* input);
static MEANING* meaningList; // list of meanings from :concepts
static MEANING* meaningLimit; // end of meaninglistp
#include <map>
using namespace std;
std::map <const char*, int> statistics; // statistics data
////////////////////////////////////////////////////////
/// UTILITY ROUTINES
////////////////////////////////////////////////////////
int CountSet(WORDP D,unsigned int baseStamp) // full recursive referencing
{
if (!D) return 0;
if (D->inferMark == inferMark) return 0;
D->inferMark = inferMark;
int count = 0;
FACT* F = GetObjectNondeadHead(D);
FACT* G;
while (F) // do all atomic members of it
{
G = F;
F = GetObjectNondeadNext(F);
WORDP S = Meaning2Word(G->subject);
if (!(G->verb == Mmember) || G->flags & FACTDEAD) continue;
if (*S->word == '~' ) continue;
else if (Meaning2Index(G->subject)) count += CountDown(GetMaster(G->subject),-1,-2,baseStamp); // word~2 reference is a synset header -- follow IS_A submembers
else // simple atomic member -- or POS specificiation
{
if (S->inferMark <= baseStamp) // count once
{
S->inferMark = inferMark;
++count;
}
}
}
F = GetObjectNondeadHead(D);
while (F) // do all set members of it
{
G = F;
F = GetObjectNondeadNext(F);
WORDP S = Meaning2Word(G->subject);
if (!(G->verb == Mmember) || G->flags & FACTDEAD) continue;
if (*S->word == '~') count += CountSet(S,baseStamp);
}
return count;
}
static int CountDown(MEANING T,int all,int depth,unsigned int baseStamp)
{ // T is a synset header
T &= -1 ^ SYNSET_MARKER;
if (all == 5) return 0;
int count = 0;
// show each word in synset
WORDP D = Meaning2Word(T);
WORDP baseWord = D;
unsigned int index = Meaning2Index(T);
unsigned int baseIndex = index;
// walk the master list of synonyms at this level
bool shown = false;
while (ALWAYS)
{
MEANING next = GetMeaning(D,index);
if (D->inferMark != inferMark)
{
if (D->inferMark <= baseStamp) ++count;
D->inferMark = inferMark;
if (depth >= 0 ) shown |= DumpOne(D,all,depth,shown); // display it
}
D = Meaning2Word(next);
index = Meaning2Index(next);
if (D == baseWord && index == baseIndex) break; // back at start of loop
}
// down go down to next level synset from this one
FACT* F = GetObjectNondeadHead(T);
while (F)
{
if (F->verb == Mis && F->object == T) count += CountDown(F->subject,all,(depth == -2) ? -2 : (depth+1),baseStamp);
F = GetObjectNondeadNext(F);
}
return count;
}
static void Indent(int count,bool nonumber)
{
if (!nonumber) Log(STDTRACELOG,(char*)"%d.",count);
while (count--) Log(STDTRACELOG,(char*)" ");
}
static bool DumpOne(WORDP S,int all,int depth,bool shown)
{
bool did = false;
if (all)
{
if ( all == 3) return false;
if (itemcount == 0 && all != 2) Indent(depth,shown);
unsigned char* data = GetWhereInSentence(S);
if (all == 1)
{
if (!data)
{
data = (unsigned char*) AllocateWhereInSentence(S);
if (!data) return false;
*data = 0;
data[1] = 0;
}
if (++data[1] == 0) ++data[0];
}
if (all == 1 && *data && (data[0] || data[1] > 1)) Log(STDTRACELOG,(char*)"+%s ",S->word); // multiple occurences
else // first occurence of word
{
if (all == 1 && !(S->systemFlags & VERB_DIRECTOBJECT)) // generate a list of intransitive verbs
{
FILE* out = FopenUTF8WriteAppend((char*)"intransitive.txt");
fprintf(out,(char*)"%s 1\r\n",S->word);
FClose(out);
}
if (all == 1 && (S->systemFlags & VERB_INDIRECTOBJECT)) // generate a list of dual transitive verbs
{
FILE* out = FopenUTF8WriteAppend((char*)"intransitive.txt");
fprintf(out,(char*)"%s 2\r\n",S->word);
FClose(out);
}
Log(STDTRACELOG,(char*)"%s ",S->word);
}
++itemcount;
if (itemcount == 10 && all != 2)
{
Log(STDTRACELOG,(char*)"\r\n");
itemcount = 0;
}
did = true;
}
return did;
}
static void MarkExclude(WORDP D)
{
FACT* F = GetObjectNondeadHead(D);
while (F)
{
if (F->verb == Mexclude) Meaning2Word(F->subject)->inferMark = inferMark;
F = GetObjectNondeadNext(F);
}
}
/////////////////////////////////////////////
/// TESTING
/////////////////////////////////////////////
static void C_AutoReply(char* input)
{
regression = 1;
strcpy(oktest,input);
if (!*oktest) regression = false;
if (*oktest) Log(STDTRACELOG,(char*)"Auto input set to %s\r\n",oktest);
}
static void MarkUp(WORDP D) // mark all that can be seen from here going up as member
{
if (D->inferMark == inferMark) return;
D->inferMark = inferMark;
FACT* F = GetSubjectNondeadHead(D);
while (F)
{
if (F->verb == Mmember)
{
WORDP E = Meaning2Word(F->object);
MarkUp(E);
}
F = GetSubjectNondeadNext(F);
}
}
static void C_Common(char* input)
{
char word[MAX_WORD_SIZE];
char word1[MAX_WORD_SIZE];
WORDP D;
char* ptr = ReadCompiledWord(input,word);
ptr = ReadCompiledWord(ptr,word1);
if (!*word1)
{
Log(STDTRACELOG, "You need to supply at least 2 words.\r\n");
return;
}
while (input)
{
input = ReadCompiledWord(input,word);
D = FindWord(word);
if (!D)
{
Log(STDTRACELOG, "%s is an unknown word\r\n",word);
return;
}
ReadCompiledWord(input,word1); // read ahead 1
if (!*word1) break; //we are on the last word
FACT* F = GetSubjectNondeadHead(D);
NextInferMark();
while (F)
{
TraceFact(F);
if (F->verb == Mmember)
MarkUp(Meaning2Word(F->object)); // mark all on this path as seen
F = GetSubjectNondeadNext(F);
}
}
WORDP words[10000];
WORDP found[10000];
NextInferMark();
unsigned int index = 0;
unsigned int at = 0;
unsigned int foundIndex = 0;
words[index++] = D; // goes onto stack
words[index++] = 0; // end of a level
int level = 0;
FACT* F;
while (at < index)
{
D = words[at++]; // next one from queue
if (D == 0)
{
if (at != index) words[index++] = 0;
++level;
found[foundIndex++] = 0; // mark the level
continue;
}
F = GetSubjectNondeadHead(D);
while (F)
{
if (F->verb == Mmember)
{
D = Meaning2Word(F->object);
if (D->inferMark == (inferMark - 1)) found[foundIndex++] = D;
if (D->inferMark < inferMark)
{
D->inferMark = inferMark;
words[index++] = D;
}
}
F = GetSubjectNondeadNext(F);
}
}
Log(STDTRACELOG,(char*)"Concept intersection:\r\n");
level = 1;
bool header = false;
for (unsigned int i = 0; i < foundIndex; ++i)
{
if (found[i] == 0)
{
Log(STDTRACELOG,(char*)"\r\n");
++level;
header = false;
}
else
{
if (!header) Log(STDTRACELOG,(char*)"%d. ",level);
header = true;
Log(STDTRACELOG,(char*)"%s ",found[i]->word);
}
}
Log(STDTRACELOG,(char*)"\r\n");
}
static void C_NoReact(char* input)
{
noReact = !noReact;
Log(STDTRACELOG,(char*)"Noreact = %d\r\n",noReact);
}
static void C_POS(char* input)
{
if (!*input) prepareMode = (prepareMode == POS_MODE) ? NO_MODE : POS_MODE;
else
{
unsigned int oldtrace = trace;
uint64 oldTokenControl = tokenControl;
char word[MAX_WORD_SIZE];
char* at = ReadCompiledWord(input,word);
if (!stricmp(word,(char*)"PENN"))
{
input = at;
tokenControl = STRICT_CASING | DO_ESSENTIALS| DO_PARSE | DO_CONTRACTIONS| NO_HYPHEN_END | NO_COLON_END | NO_SEMICOLON_END | TOKEN_AS_IS;
}
else
{
char* token = GetUserVariable((char*)"$cs_token");
int64 f;
ReadInt64(token,f);
if (f == 0) f = DO_ESSENTIALS| DO_PARSE | DO_CONTRACTIONS| NO_HYPHEN_END | NO_COLON_END | NO_SEMICOLON_END | TOKEN_AS_IS;
tokenControl = f;
}
trace = (unsigned int) -1;
tmpPrepareMode = POS_MODE;
quotationInProgress = 0;
PrepareSentence(input,true,true);
tmpPrepareMode = NO_MODE;
tokenControl = oldTokenControl;
trace = (modifiedTrace) ? modifiedTraceVal : oldtrace;
}
}
static void C_Prepare(char* input)
{
uint64 oldToken = tokenControl;
input = SkipWhitespace(input);
static bool prepass = true;
char word[MAX_WORD_SIZE];
if (*input == USERVAR_PREFIX) // set token control to this
{
char* ptr = ReadCompiledWord(input,word);
char* value = GetUserVariable(word);
if (value && *value)
{
input = ptr;
int64 val64 = 0;
ReadInt64(value,val64);
tokenControl = val64;
}
}
input = SkipWhitespace(input);
if (!strnicmp(input,(char*)"NOPREPASS",9) || !strnicmp(input,(char*)"PREPASS",7))
{
prepass = strnicmp(input,(char*)"NOPREPASS",9) ? true : false;
input = ReadCompiledWord(input,word);
}
if (!*input) prepareMode = (prepareMode == PREPARE_MODE) ? NO_MODE : PREPARE_MODE;
else
{
char prepassTopic[MAX_WORD_SIZE];
strcpy(prepassTopic, GetUserVariable((char*)"$cs_prepass"));
unsigned int oldtrace = trace;
nextInput = input;
bool oobstart = (*nextInput == '[');
while (*nextInput)
{
prepareMode = PREPARE_MODE;
if (*prepassTopic) Log(STDTRACELOG,(char*)"Prepass: %s\r\n", prepass ? (char*)"ON" : (char*)"OFF");
PrepareSentence(nextInput,true,true,false,oobstart);
oobstart = false;
prepareMode = NO_MODE;
if (prepass && PrepassSentence(prepassTopic)) continue;
}
trace = (modifiedTrace) ? modifiedTraceVal : oldtrace;
}
tokenControl = oldToken;
}
static void C_Spellit(char* input)
{
echo = true;
spellTrace = true;
input = SkipWhitespace(input);
nextInput = input;
prepareMode = TOKENIZE_MODE;
while (*nextInput) PrepareSentence(nextInput, true, true, false, false);
prepareMode = NO_MODE;
echo = false;
spellTrace = false;
}
static void C_Tokenize(char* input)
{
uint64 oldToken = tokenControl;
input = SkipWhitespace(input);
static bool prepass = true;
char word[MAX_WORD_SIZE];
if (*input == USERVAR_PREFIX) // set token control to this
{
char* ptr = ReadCompiledWord(input, word);
char* value = GetUserVariable(word);
if (value && *value)
{
input = ptr;
int64 val64 = 0;
ReadInt64(value, val64);
tokenControl = val64;
}
}
input = SkipWhitespace(input);
if (!strnicmp(input, (char*)"NOPREPASS", 9) || !strnicmp(input, (char*)"PREPASS", 7))
{
prepass = strnicmp(input, (char*)"NOPREPASS", 9) ? true : false;
input = ReadCompiledWord(input, word);
}
if (!*input) prepareMode = (prepareMode == TOKENIZE_MODE) ? NO_MODE : TOKENIZE_MODE;
else
{
char prepassTopic[MAX_WORD_SIZE];
strcpy(prepassTopic, GetUserVariable((char*)"$cs_prepass"));
unsigned int oldtrace = trace;
nextInput = input;
bool oobstart = (*nextInput == '[');
while (*nextInput)
{
prepareMode = TOKENIZE_MODE;
if (*prepassTopic) Log(STDTRACELOG, (char*)"Prepass: %s\r\n", prepass ? (char*)"ON" : (char*)"OFF");
PrepareSentence(nextInput, true, true, false, oobstart);
oobstart = false;
prepareMode = NO_MODE;
if (prepass && PrepassSentence(prepassTopic)) continue;
}
trace = (modifiedTrace) ? modifiedTraceVal : oldtrace;
}
tokenControl = oldToken;
}
static void MemorizeRegress(char* input)
{
char word[MAX_WORD_SIZE];
input = ReadCompiledWord(input,word); // file to read
char outputfile[MAX_WORD_SIZE];
ReadCompiledWord(input,outputfile); // file to write
FILE* in = FopenReadNormal(word); // source full name given
char file[SMALL_WORD_SIZE];
if (!in)
{
char* txt = strstr(word,(char*)".txt");
if (txt) *txt = 0;
sprintf(file,(char*)"%s/log-%s.txt",users,word); // presume only login given, go find full file
in = FopenReadNormal(file); // source
}
if (!in) Log(STDTRACELOG,(char*)"Couldn't find %s\r\n",file);
else
{
FILE* out = NULL;
if (*outputfile) out = FopenUTF8Write(outputfile);
if (!out)
{
char fname[200];
sprintf(fname, "%s/regress.txt", tmp);
strcpy(outputfile,fname);
out = FopenUTF8Write(outputfile);
if (!out)
{
(*printer)((char*)"cannot open %s\r\n",outputfile);
return;
}
}
char* at;
bool start = true;
while (ReadALine(readBuffer,in) >= 0) // read log file
{
if (!*readBuffer) continue;
size_t len;
// Start: user:fa bot:patient1a ip: rand:247 (~introductions) 0 ==> Hello Doctor When:Mar23'14-20:06:23 Version:4.2 Build0: Build1:Mar23'14-20:04:11 0:Mar23'14-20:06:18 F:0 P:0 Why:~introductions.0.0.~control.6.0
// Respond: user:fa bot:patient1a ip: (~introductions) 1 fa ==> I'm afraid I don't understand that question. When:Mar23'14-20:06:30 Why:~control.10.0=MAINCONTROL
// fields are: type, user, bot, ip, {rand}, (resulting topic), current volley id, user input, ==> bot output, when: {version/build1/build0/f:/p:) followed by why: rule tags for each issued output.
char kind[MAX_WORD_SIZE];
ReadCompiledWord(readBuffer,kind);
if (strcmp(kind,(char*)"Respond:") && strcmp(kind,(char*)"Start:") ) continue; // abnormal line? like a ^log entry.
// normal volley
char user[MAX_WORD_SIZE];
char* ptr = strstr(readBuffer,(char*)"user:") + 5;
ptr = ReadCompiledWord(ptr,user);
unsigned int volley;
char* endtopic = strchr(ptr,')');
volley = atoi(endtopic+2);
int rand = -1;
char* randptr = strstr(ptr,(char*)"rand:");
if (randptr) rand = atoi(randptr+5);
// confirm legit startup
if (start == true)
{
if (volley || *kind != 'S')
{
Log(STDTRACELOG,(char*)"Log file must begin with Start at turn 0, not turn %d\r\n",volley);
FClose(in);
return;
}
start = false;
}
char bot[MAX_WORD_SIZE];
ptr = strstr(readBuffer,(char*)"bot:") + 4;
ptr = ReadCompiledWord(ptr,bot);
char topic[MAX_WORD_SIZE];
at = strchr(ptr,'(') + 1;
*endtopic = 0;
at = ReadCompiledWord(at,topic);
char junk[MAX_WORD_SIZE];
at = endtopic + 2;
at = ReadCompiledWord(at,junk) - 1; // point at blank
char* input = at; // now points to user input start
char* output = strstr(at,(char*)" ==> ");
if (!output) continue;
*output = 0; // end of input
input = SkipWhitespace(input);
output += 5;
output = SkipWhitespace(output); // start of output
char* why = strstr(output,(char*)"Why:");
char* end = strstr(output,(char*)" When:");
if (end) *end = 0; // end of output
if (rand != -1) fprintf(out,(char*)"%s user:%s bot:%s rand:%d (%s) %d ==> %s\r\n", kind, user, bot,rand,topic,volley,output);
else fprintf(out,(char*)"%s user:%s bot:%s (%s) %d %s ==> %s\r\n", kind, user, bot,topic,volley,input,output);
if (!why) continue;
why += 4;
end = strchr(why+1,'~'); // 2nd rule via if there is one
if (end) *end = 0;
int topicid;
int id;
char* verify = GetVerify(why,topicid,id);
char* rule = GetRule(topicid,id); // the rule we want to test
char label[MAX_WORD_SIZE];
char pattern[MAX_WORD_SIZE];
char outputdata[MAX_WORD_SIZE];
char* output1 = GetPattern(rule,label,pattern);
len = strlen(output1);
if (len > 50) len = 50;
strncpy(outputdata,output1,len);
while (outputdata[len-1] == ' ') --len;
outputdata[len] = 0;
pattern[50] = 0; // limit it
fprintf(out,(char*)" verify: %s rule:%s kind:%c label:%s pattern: %s output: %s \r\n",verify, why,*rule,label,pattern,outputdata);
if (!end)
{
fprintf(out,(char*)"%s",(char*)" verify2: \r\n");
continue;
}
*end = '~';
rule = GetRule(topicid,id); // the rule we want to test
output1 = GetPattern(rule,label,pattern);
len = strlen(output1);
if (len > 50) len = 50;
strncpy(outputdata,output1,len);
outputdata[len] = 0;
pattern[50] = 0; // limit it
verify = GetVerify(end,topicid,id);
fprintf(out,(char*)" verify2: %s rule:%s kind:%c label:%s pattern: %s output: %s \r\n",verify, end,*rule,label,pattern,outputdata);
}
FClose(out);
FClose(in);
Log(STDTRACELOG,(char*)"Regression file %s created\r\n",outputfile);
}
}
static void VerifyRegress(char* file)
{
char word[MAX_WORD_SIZE];
char* at = ReadCompiledWord(file,word);
bool silent = false;
if (!stricmp(word,(char*)"terse"))
{
file = at;
silent = true;
}
FILE* in = FopenReadNormal(file); // source
if (!in)
{
(*printer)((char*)"No regression data found for %s\r\n",file);
return;
}
sprintf(logFilename,(char*)"%s/tmpregresslog.txt",users); // user log goes here so we can regenerate a new regression file if we need one
FILE* out = FopenUTF8Write(logFilename); // make a new log file to do this in.
FClose(out);
int olduserlog = userLog;
userLog = LOGGING_SET;
unsigned int changed = 0;
bool modified = false;
bool oldecho = echo;
echo = false;
unsigned int count = 0;
prepareMode = REGRESS_MODE;
size_t len;
unsigned int minorchange = 0;
char verifyinfo[MAX_BUFFER_SIZE];
unsigned int volley = 0;
char myBuffer[MAX_BUFFER_SIZE];
regression = REGRESS_REGRESSION;
char* holdmain = mainOutputBuffer;
while (ReadALine(myBuffer,in) >= 0 ) // read regression file
{
//Start: user:fd bot:rose rand:553 volley:0 topic:~hello input: output: Good morning.
// verify: rule:~hello.1.0. kind:t label: pattern: ( !$olduser =8%input=0 =7%hour<12 ) output: $olduser = 1 Good morning. $begintime = %fulltime
// verify2: rule:~submain_control.5.0 kind:u label: pattern: ( =8%input<%userfirstline ) output: $repeatstart = %userfirstline + 10 ^gambit ( ~hell
//Respond: user:fd bot:rose volley:1 topic:~hellold input: what is your name output: My name is Rose. What's yours?
// verify: rule:~hello.4.0=MYNAME. kind:t label:MYNAME pattern: ( ) output: My name is Rose. $begintime = %fulltime ^^if ( ! $
// verify2: what is your name? rule:~physical_self.101.0=TELLNAME kind:u label:TELLNAME pattern: ( ![ him my her them ] << what your [ name moniker output: ^reuse ( ~hello.myname ) `01b u: ( !my what * you
if (!*myBuffer) continue;
char* ptr = SkipWhitespace(myBuffer);
if (*ptr == '#' || !*ptr) continue;
if (strstr(ptr,(char*)":quit")) break; // assume done
if (strstr(ptr,(char*)":trace"))
{
trace = (trace == -1) ? 0 : -1;
echo = (trace != 0);
continue;
}
char user[MAX_WORD_SIZE];
char* u = strstr(ptr,(char*)"user:");
if (!u)
{
ReportBug("Inconsistent regression file");
return;
}
u += 5;
ptr = ReadCompiledWord(u,user);
char bot[MAX_WORD_SIZE];
u = strstr(myBuffer,(char*)"bot:") + 4;
ptr = ReadCompiledWord(u,bot);
ptr = strchr(ptr,'(');
char topic[MAX_WORD_SIZE];
char* end = strchr(ptr,')');
*end = 0;
ptr = ReadCompiledWord(ptr + 1,topic); // now points to volley
*end = ')';
char volleyid[MAX_WORD_SIZE];
ptr = ReadCompiledWord(ptr+1,volleyid); // volley id
char* vinput = ptr;
char* oldsaid = strstr(vinput,(char*)"==> ");
char actualOutput[MAX_WORD_SIZE];
strcpy(actualOutput,oldsaid + 4);
*oldsaid = 0;
if (*(oldsaid-1) == ' ') *(oldsaid-1) = 0;
if (*(oldsaid-2) == ' ') *(oldsaid-2) = 0;
oldsaid += 4;
oldsaid = TrimSpaces(oldsaid,true);
// EXECUTE the input choice
char buffer[MAX_BUFFER_SIZE];
mainOutputBuffer = buffer;
// bot login
strcpy(computerID,bot);
*computerIDwSpace = ' ';
MakeLowerCopy(computerIDwSpace+1,computerID);
strcat(computerIDwSpace,(char*)" ");
strcpy(loginID,user); // user login
if (*myBuffer == 'S' || myBuffer[3] == 'S' ) // start it - 1st line may have utf8 marker
{
int depth = globalDepth; // reset clears depth, but we are still in process so need to restore it
int turn = atoi(volleyid);
if (turn == 0) ResetUser(vinput); // force reset
globalDepth = depth;
*vinput = 0;
userFirstLine = volleyCount+1;
*readBuffer = 0;
nextInput = vinput;
ProcessInput("");
FinishVolley(vinput,buffer,NULL);
char* revised = Purify(buffer);
if (revised != buffer) strcpy(buffer,revised);
TrimSpaces(buffer,false);
if (!responseIndex)
{
Log(ECHOSTDTRACELOG,(char*)"*** No response to startup\r\n");
}
}
else if (*myBuffer == 'R' || myBuffer[3] == 'R' )// respond - 1st line may have utf8 marker
{
int depth = globalDepth; // reset clears depth, but we are still in process so need to restore it
++volley;
ReadUserData();
char myinput[MAX_WORD_SIZE];
strcpy(myinput,vinput);
globalDepth = depth;
ProcessInput(myinput);
FinishVolley(myinput,buffer,NULL);
char* revised = Purify(buffer);
if (revised != buffer) strcpy(buffer,revised);
TrimSpaces(buffer,false);
if (!responseIndex)
{
Log(ECHOSTDTRACELOG,(char*)"*** No response to user input\r\n");
}
}
else
{
Log(STDTRACELOG,(char*)"Bad regression file lineup %s\r\n",myBuffer);
continue;
}
++count;
// now get verification data
ReadALine(readBuffer,in);
strcpy(verifyinfo,readBuffer); // copy so we can debug seeing original data
// verify: rule:~hello.1.0. kind:t label: pattern: ( !$olduser =8%input=0 =7%hour<12 ) output: $olduser = 1 Good morning. $begintime = %fulltime
char* vverify = strstr(verifyinfo,(char*)"verify: ") + 8;
ptr = strstr(vverify,(char*)"rule:");
*ptr = 0; // end of verification level
ptr += 5;
char vtag1[MAX_WORD_SIZE];
ptr = ReadCompiledWord(ptr,vtag1);
ptr = strstr(ptr,(char*)"kind:")+5;
char vkind = *ptr;
char* vlabel = strstr(ptr,(char*)"label:") + 6;
char* vpattern = strstr(vlabel,(char*)"pattern: ");
*--vpattern = 0;
vpattern += 10;
char* equal = strchr(vtag1,'=');
if (equal) *equal = 0; // remove label from tag
char* voldoutputcode = strstr(vpattern,(char*)"output: "); // what it said then.
*--voldoutputcode = 0;
voldoutputcode += 9;
len = strlen(voldoutputcode);
while (voldoutputcode[len-1] == ' ') --len;
voldoutputcode[len] = 0;
char* at = strchr(voldoutputcode,'`');
if (at) *at = 0;
bool sametag = false;
bool sameruletype = false;
bool samelabel = false;;
bool samepattern = false;;
bool sameoutput = false;; // used same rule
char tag[MAX_WORD_SIZE];
int id;
int topicid;
char label[MAX_WORD_SIZE];
char pattern[MAX_WORD_SIZE];
char outputdata[MAX_WORD_SIZE];
for (int i = 0; i < responseIndex; ++i) // use last said as topicid (we usually prefix emotions and other things)
{
// get actual results
unsigned int order = responseOrder[i];
strcpy(tag,GetTopicName(responseData[order].topic));
strcat(tag,responseData[order].id);
GetVerify(tag,topicid,id);
char* rule = GetRule(topicid,id); // the rule we want to test
char* newoutputcode = GetPattern(rule,label,pattern);
size_t len = strlen(newoutputcode);
if (len > 50) len = 50;
strncpy(outputdata,newoutputcode,len);
while (outputdata[len-1] == ' ') --len;
outputdata[len] = 0;
char* close = strchr(outputdata,'`');
if (close) *close = 0; // end rule
pattern[50] = 0; // limit it
if (!sametag) sametag = !strnicmp(tag,vtag1,strlen(vtag1));
if (!sameruletype) sameruletype = vkind == *rule;
if (!samelabel) samelabel = *label && !stricmp(label,vlabel);
if (!samepattern) samepattern = !stricmp(pattern,vpattern);
if (!sameoutput) sameoutput = !stricmp(outputdata,voldoutputcode); // used same rule
}
bool samesaid = !stricmp(oldsaid,buffer);
if (!samesaid && strstr(oldsaid,buffer)) samesaid = true; // see if we subsume what was said
char changes[MAX_WORD_SIZE];
*changes = 0;
if (!sametag) strcat(changes,(char*)"Tag, ");
if (!sameruletype) strcat(changes,(char*)"Rule type, ");
if (!samelabel && (*label || *vlabel)) strcat(changes,(char*)"Label, ");
if (!samepattern) strcat(changes,(char*)"Pattern, ");
if (!sameoutput) strcat(changes,(char*)"Output, "); // we only care if outputdata changed, not what was said which might be random
ReadALine(readBuffer,in); // verify2 info
char verify2info[MAX_WORD_SIZE];
strcpy(verify2info,readBuffer); // copy so we can debug seeing original data
// verify: rule:~hello.1.0. kind:t label: pattern: ( !$olduser =8%input=0 =7%hour<12 ) output: $olduser = 1 Good morning. $begintime = %fulltime
char* vverify2 = strstr(verify2info,(char*)"verify2: ") + 8;
ptr = strstr(vverify2,(char*)"rule:");
if (ptr) *ptr = 0; // end of verification level
if (sametag && sameruletype && samepattern && sameoutput){;} // matches rule id, output, kind, pattern - perfect match
else if (sameoutput || samepattern || samelabel || samesaid)
{
if (!sametag && samesaid) ++minorchange;
else if (silent) {;}
else if (!sametag) Log(ECHOSTDTRACELOG,(char*)" Volley %d input %s changed tag. Was: %s is: %s\r\n",volley,vinput,vtag1,tag);
else Log(ECHOSTDTRACELOG,(char*)" Volley %d input %s is intact. %s changed\r\n",volley,vinput,changes);
if (!samesaid && !silent)
{
Log(ECHOSTDTRACELOG,(char*)" Old said: %s\r\n",oldsaid);
Log(ECHOSTDTRACELOG,(char*)" Now says: %s\r\n\r\n",buffer);
}
modified = true;
}
else
{
Log(ECHOSTDTRACELOG,(char*)"*** Volley %d input %s - changed radically. old: %s now: %s\r\n",volley,vinput, vtag1, tag);
if (!samesaid)
{
if (*SkipWhitespace(vverify) || *SkipWhitespace(vverify2)) Log(ECHOSTDTRACELOG,(char*)" Old verify: %s + %s\r\n",vverify,vverify2);
Log(ECHOSTDTRACELOG,(char*)" Old said: %s - %s pattern: %s",oldsaid,vlabel,vpattern);
int oldtopic;
int oldid;
GetVerify(vtag1,oldtopic,oldid);
TraceSample(oldtopic,oldid,ECHOSTDTRACELOG);
Log(ECHOSTDTRACELOG,(char*)"\r\n");
Log(ECHOSTDTRACELOG,(char*)" Now says: %s - %s pattern: %s ",buffer,label,pattern);
TraceSample(topicid,id,ECHOSTDTRACELOG);
Log(ECHOSTDTRACELOG,(char*)"\r\n\r\n");
}
++changed;
}
}
userLog = olduserlog;
FClose(in);
echo = oldecho;
mainOutputBuffer = holdmain;
prepareMode = NO_MODE;
regression = NO_REGRESSION;
// shall we revise the regression file?
if (changed) Log(ECHOSTDTRACELOG,(char*)"There were %d rules which changed radically of %d inputs.\r\n",changed,count);
if (minorchange) Log(ECHOSTDTRACELOG,(char*)"There were %d rules which changed tag.\r\n",minorchange);
if (changed || modified || minorchange)
{
(*printer)((char*)"%s",(char*)"\nRegression has changed. Do you want to update regression to the current results? Only \"yes\" will do so: ");
ReadALine(readBuffer,stdin);
if (!stricmp(readBuffer,(char*)"yes"))
{
char fdo[MAX_WORD_SIZE];
sprintf(fdo,(char*)"%s/tmpregresslog.txt %s",users,file);
MemorizeRegress(fdo);
}
}
else (*printer)((char*)"%s",(char*)"Regression passed.\r\n");
}
static void C_Regress(char* input)
{
char word[MAX_WORD_SIZE];
char* xxptr = ReadCompiledWord(input,word);
if (!strnicmp(input,(char*)"init ",5)) MemorizeRegress(input+5);
else VerifyRegress(input);
}
static void C_Source(char* input)
{
char word[MAX_WORD_SIZE];
multiuser = false;
char* ptr = ReadCompiledWord(input,word);
if (!stricmp(word, "multiuser")) // not functional yet
{
multiuser = true;
ptr = ReadCompiledWord(ptr, word);
}
FILE* in = FopenReadNormal(word); // source
if (in) sourceFile = in;
else Log(STDTRACELOG,(char*)"No such source file: %s\r\n",word);
SetUserVariable((char*)"$$document",word);
ReadCompiledWord(ptr,word);
echoSource = NO_SOURCE_ECHO;
if (!stricmp(word,(char*)"echo")) echoSource = SOURCE_ECHO_USER;
else if (!stricmp(word,(char*)"internal")) echoSource = SOURCE_ECHO_LOG;
sourceStart = ElapsedMilliseconds();
sourceTokens = 0;
sourceLines = 0;
}
static void ReadNextDocument(char* name,uint64 value) // ReadDocument(inBuffer,sourceFile) called eventually to read document
{
FILE* in = FopenReadNormal(name); // source
if (in) sourceFile = in;
else
{
Log(STDTRACELOG,(char*)"No such document file: %s\r\n",name);
return;
}
docSentenceCount = 0;
sourceStart = ElapsedMilliseconds();
sourceTokens = 0;
sourceLines = 0;
readingDocument = true;
SetBaseMemory();
inputSentenceCount = 0;
docVolleyStartTime = ElapsedMilliseconds(); // time limit control
tokenCount = 0;
InitJSONNames(); // reset indices for this volley
ClearVolleyWordMaps();
ResetEncryptTags();
ResetToPreUser(); // back to empty state before any user
ReadNewUser(); // read user info back in so we can continue (a form of garbage collection)
ShowStats(true);
SetUserVariable((char*)"$$document",name);
if (!trace) echo = false;