Professional Documents
Culture Documents
01 Eliza Written in Pascal
01 Eliza Written in Pascal
{=========================================================}
{ Keywords }
{=========================================================}
const MaxKey = 37;
type KeyWordArray = array[1..MaxKey] of string[14];
const KeyWords : KeyWordArray = (
'CAN YOU','CAN I','YOU ARE','YOU''RE','I DON''T',
'I FEEL','WHY DON''T YOU','WHY CAN''T I','ARE YOU',
'I CAN''T','I AM','I''M','YOU','I WANT','WHAT',
'HOW','WHO','WHERE','WHEN','WHY','NAME','CAUSE',
'SORRY','DREAM','HELLO','HI','MAYBE','NO',
'YOUR','ALWAYS','THINK','ALIKE','YES','FRIEND',
'COMPUTER','NO KEY FOUND','REPEAT INPUT');
{=========================================================}
{ Data for finding the right responses }
{=========================================================}
{=========================================================}
{ String data for conjugations }
{=========================================================}
const MaxCon = 7;
type ConStr = string[8];
ConjArray = array[1..MaxCon] of ConStr;
const Con1 : ConjArray =
(' are ',' we''re ',' you ',' your ',' I''ve ',' I''m ',' me ');
Con2 : ConjArray =
(' am ',' was ',' I ',' my ',' you''ve ',' you''re ',' !you ');
{=========================================================}
{ Other misc information needed by the program }
{=========================================================}
{- possible punctuation -}
const PuncSet = [' ','.','!','?',','];
{=========================================================}
{ drop leading and trailing spaces and punctuation }
{=========================================================}
procedure Ctrim(var Xstr:string);
begin
while (length(Xstr) > 0) and (Xstr[1] in PuncSet) do
delete(Xstr,1,1);
while (length(Xstr) > 0) and (Xstr[length(Xstr)] in PuncSet) do
dec(Xstr[0]);
end;
{=========================================================}
{ return a string in upper case }
{=========================================================}
function UpCopy(Wstr:string; Pos,Cnt:byte):string;
var Xstr:string;
i:integer;
begin
Xstr[0] := #0;
for i := 1 to Cnt do
begin
inc(Xstr[0]);
Xstr[i] := upcase(Wstr[pred(Pos+i)]);
end;
UpCopy := Xstr;
end;
{=========================================================}
{ Find keyword in Wstr }
{=========================================================}
{- a keyword is a relational word that we can respond to }
{- see the keyword table to see the types of relational words}
{- that are used. Returns "Key" pointing to keyword in table,}
{- returns "Kpos" pointing to first char after keyword in Wstr}
{- Returns function true if keyword found, or false if not}
{- if no keyword found Key = pred(MaxKey), repeated string = MaxKey}
{=========================================================}
{ Take the right part of the string and conjugate it }
{ using the list of strings to be swapped }
{=========================================================}
{-procedure Conjugate-}
begin
Cstr := copy(Wstr,Kpos,length(Wstr)); {pull out the right part}
Ctrim(Cstr); {clean it up}
if length(Cstr) = 0 then Cstr := Wstr; {if empty use entire string}
Cstr := ' '+Cstr+' '; {add working spaces}
for i := 1 to MaxCon do
begin
Cp := 0;
while Cp < length(Cstr) do
begin
inc(Cp);
if not(ConSwap(Con1[i],Con2[i])) then
if ConSwap(Con2[i],Con1[i]) then {nop};
end;
end;
{============================================================}
{ Get a response based on the keyword number in variable Key }
{============================================================}
{-if no "*" or "@" at the end of the response, then just return the response}
{-if there was an "*" at the end of the response string, then return}
{-the response plus the conjugation word/phrase in Rstr plus a "?"}
{-if "@" then add a period instead}
if Fstr[length(Fstr)] = '*' then goto QAppend;
if Fstr[length(Fstr)] = '@' then goto PAppend;
Rstr := Fstr;
Exit;
{- replace the '*' with a space, append the conjugated string and add "?" -}
QAppend:
Fstr[length(Fstr)] := ' ';
Rstr := Fstr+Rstr+'?';
Exit;
{- replace the '@' with a space, append the conjugated string and add "." -}
PAppend:
Fstr[length(Fstr)] := ' ';
Rstr := Fstr+Rstr+'.';
end;
{============================================================}
{- program Eliza -}
begin
RspIndex := KeyIndex; {- start the index array -}
writeln;
writeln('Hi! I''m Eliza. I am your personal therapy computer.');
writeln('Please tell me your problem.');
writeln;
while true do
begin
readln(Istr);
Ctrim(Istr); {- strip out any extra blanks from work string -}
Cstr := UpCopy(Istr,1,length(Istr));
if (Cstr = 'STOP') or (Cstr = 'QUIT') then Halt;