
(*MATCH FUNCTION FOR NATURAL LANGUAGE*)
(*RESPONSE ANALYSIS*)

(*PAUL F. MERRILL*)
(*BRIGHAM YOUNG UNIVERSITY*)

PROGRAM PILOT;
 TYPE STRARRAY=ARRAY[1..100] OF STRING;
 VAR C:CHAR;
     KEYWORDSTR,ANS,MATCHSTR:STRING;
 
 FUNCTION MATCH (ANSSTR,STUDENTANS:STRING):BOOLEAN;
    CONST SPACE=' ';
    VAR P,NWORDS,INDEX:INTEGER;
        ORDER,FOUND:BOOLEAN;ANSARRAY:STRARRAY;
 
    PROCEDURE DELCOMMA (VAR MATCHSTR:STRING);
      VAR I:INTEGER;
      BEGIN
         FOR I:= 1 TO LENGTH(MATCHSTR) DO
            IF MATCHSTR[I]<'A'
              THEN
                MATCHSTR[I]:= ' ';
      END;  (*DELCOMMA*)
    
    PROCEDURE SEPRTE (ANSSTR:STRING;VAR ANSARRAY:STRARRAY;VAR NWORDS:INTEGER);
       CONST COMMA=',';
       VAR LOCCOMMA:INTEGER;
       BEGIN
          ANSSTR:=CONCAT(ANSSTR,COMMA);
          NWORDS:=0;
          WHILE LENGTH (ANSSTR)>1 DO
            BEGIN
              LOCCOMMA:=POS(COMMA,ANSSTR);
              NWORDS:=NWORDS+1;
              ANSARRAY[NWORDS]:=COPY(ANSSTR,1,LOCCOMMA-1);
              DELETE (ANSSTR,1,LOCCOMMA);
            END; (*WHILE*)
      END;  (*SEPRTE*)
      
    
    PROCEDURE ORDERCHK;
      BEGIN
        FOUND:=TRUE;
        IF ORDER 
          THEN
            BEGIN
              DELETE (STUDENTANS,1,P+LENGTH(ANSARRAY[INDEX])-1);
              STUDENTANS:=CONCAT(SPACE,STUDENTANS);
            END;  (*IF*)
      END;  (*ORDERCHK*)
   
    PROCEDURE ORTEST;
      BEGIN
        ANSARRAY[INDEX]:=COPY(ANSARRAY[INDEX],2,LENGTH(ANSARRAY[INDEX])-1);
        WHILE POS(')',ANSARRAY[INDEX])=0 DO
          BEGIN
             P:=POS (ANSARRAY[INDEX],STUDENTANS);
             IF P<>0
                THEN ORDERCHK;
             INDEX:=INDEX+1;
          END; (*WHILE*)
        IF NOT FOUND 
          THEN
            BEGIN
              DELETE (ANSARRAY[INDEX],LENGTH(ANSARRAY[INDEX]),1);
              P:=POS (ANSARRAY[INDEX],STUDENTANS);
              IF P<>0
                 THEN ORDERCHK
            END; (*IF NOT FOUND*)
      END;  (*ORTEST*)
    
   BEGIN  (*MATCH FUNCTION*)
     DELCOMMA(STUDENTANS);
     STUDENTANS:=CONCAT(SPACE,STUDENTANS,SPACE);
     ORDER:=ANSSTR[1]='*';
     IF ORDER THEN DELETE (ANSSTR,1,1);
     SEPRTE(ANSSTR,ANSARRAY,NWORDS);
     INDEX:=1;
     REPEAT
        FOUND:=FALSE;
        IF ANSARRAY[INDEX,1]='('
            THEN
               BEGIN
                 ORTEST;
                 IF NOT FOUND 
                   THEN
                      BEGIN
                        MATCH:=FALSE;
                        EXIT(MATCH)
                      END;
               END (*THEN*)
            ELSE 
              BEGIN
                P:=POS (ANSARRAY[INDEX],STUDENTANS);
                IF P<>0
                  THEN ORDERCHK
                  ELSE 
                    BEGIN
                      MATCH:=FALSE;
                      EXIT(MATCH);
                    END;  (*ELSE*)
              END; (*ELSE*)
        INDEX:=INDEX+1
     UNTIL INDEX>NWORDS;
     MATCH:=FOUND
   END;  (*MATCH FUNCTION*)
  
 BEGIN  (*MAIN PROGRAM*)
   KEYWORDSTR:='';
   REPEAT
     PAGE (OUTPUT);
     WRITELN;WRITELN;
     WRITELN('MATCH FUNCTION DEMO':28);
     WRITELN;WRITELN('THIS DEMONSTRATION PROGRAM CAN BE USED');
     WRITELN('TO EXPERIMENT WITH THE MATCH FUNCTION.');
     WRITELN;WRITELN('ENTER CORRECT @NSWER STRING OR RETURN:');
     WRITELN(' EG.:  * RED ,(GREEN, PINK ),YELLOW');
     WRITELN;
     READLN (ANS);
     IF LENGTH(ANS)<>0 
        THEN KEYWORDSTR:=ANS
        ELSE 
          BEGIN
             WRITELN;WRITELN('DEFAULT CORRECT ANSWER STRING:');
             WRITELN('  ',KEYWORDSTR);
          END;
     WRITELN;WRITELN('ENTER MATCH STRING:');
     WRITELN;
     READLN(MATCHSTR);
     WRITELN;WRITE('*****   ');
     IF MATCH (KEYWORDSTR,MATCHSTR)
       THEN WRITE('MATCH')
       ELSE WRITE('NO MATCH');
     WRITELN('   *****');
     WRITELN;WRITE('<SPACE> TO CONTINUE     <ESC> TO QUIT ');
     READ(C);
  UNTIL C=CHR(27);
END.
