$ include "seed7_05.s7i";
include "stdio.s7i";
const array string: keyword_table is [](
"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"
);
const array array string: replace_words is [](
[](" are ", " am "),
[](" were ", " was "),
[](" you ", " I "),
[]("your ", "my "),
[](" I've ", " you've "),
[](" I'm ", " you're "),
[](" me ", " !you ")
);
const array array string: example_sentence is [](
[]( "Don't you believe that I can*",
"Perhaps you would like to be able to*",
"You want me to be able to*" ),
[]( "Perhaps you don't want to*",
"Do you want to be able to*" ),
[]( "What makes you think I am*",
"Does it please you to believe I am*",
"Perhaps you would like to be*",
"Do you sometimes wish you were*" ),
[]( "Don't you really*",
"Why don't you*",
"Do you wish to be able to*",
"Does that trouble you?" ),
[]( "Tell me more about such feelings",
"Do you often feel*",
"Do you enjoy feeling*" ),
[]( "Do you really believe I don't*",
"Perhaps in good time I will*",
"Do you want me to*" ),
[]( "Do you think you should be able to*",
"Why can't you*" ),
[]( "Why are you interested in whether or not I am*",
"Would you prefer if I were not*",
"Perhaps in your fantasies I am*" ),
[]( "How do you know you can't*",
"Have you tried?",
"Perhaps you can now*" ),
[]( "Did you come to me because you are*",
"How long have you been*",
"Do you believe it is normal to be*",
"Do you enjoy being*" ),
[]( "We were discussing you, not me.",
"Oh, I*",
"You're not really talking about me, are you?" ),
[]( "What would it mean to you if you got*",
"Why do you want*",
"Suppose you soon got*",
"What if you never got*",
"I sometimes also want*" ),
[]( "Why do you ask?",
"Does that question interest you?",
"What answer would please you the most?",
"What do you think?",
"Are such questions on your mind often?",
"What is it that you really want to know?",
"Have you asked anyone else?",
"Have you asked such questions before?",
"What else comes to mind when you ask that?" ),
[]( "Names don't interest me.",
"I don't care about names... please go on." ),
[]( "Is that the real reason?",
"Don't any other reasons come to mind?",
"Does that reason explain anything else?",
"What other reasons might there be?" ),
[]( "Please don't apologize!",
"Apologies are not necessary.",
"What feelings do you have when you apologize?",
"Don't be so defensive!" ),
[]( "What does that dream suggest to you?",
"Do you dream often?",
"What persons appear in your dreams?",
"Are you disturbed by your dreams?" ),
[]( "How do you do. please state your problem." ),
[]( "You don't seem quite certain.",
"Why the uncertain tone?",
"Can't you be more positive?",
"You aren't sure?",
"Don't you know?" ),
[]( "Are you saying 'no' just to be negative?",
"You are being a bit negative.",
"Why not?",
"Are you sure?",
"Why no?" ),
[]( "Why are you concerned about my*",
"What about your own*" ),
[]( "Can you think of an example?",
"When?",
"What are you thinking of?",
"Really,...always?" ),
[]( "Do you really think so?",
"But you are not sure you*",
"Do you doubt you*" ),
[]( "In what way?",
"What similarity do you see?",
"What does the similarity suggest to you?",
"What other connections do you see?",
"Could there really be some connection?",
"How?",
"You seem quite positive." ),
[]( "Are you sure?",
"Hmmm...I see.",
"I understand." ),
[]( "Do you have any friends?",
"Do your friends worry you?",
"Do they pick on you?",
"Are your friends a source of anxiety?",
"Do you impose your problems on your friends?",
"Perhaps your dependence on friends worries you." ),
[]( "Do computers worry you?",
"Are you talking about me in particular?",
"Are you frightened by machines?",
"Why do you mention computers?",
"What do you think machines have to do with your problems?",
"Don't you think computers can help you?",
"What is it about machines that worries you?" ),
[]( "Do you feel intense psychological stress?",
"What does that suggest to you?",
"I see.",
"I'm not sure I understand you fully",
"Now, please clarify yourself.",
"Can you elaborate on that?",
"That is quite interesting." )
);
const array integer: keyword_assignment is [](
1, 2, 3, 3, 4, 5, 6, 7, 8, 9,
10, 10, 11, 12, 13, 13, 13, 13, 13, 13,
14, 15, 16, 17, 18, 18, 19, 20, 21, 22,
23, 24, 25, 26, 27, 28
);
var array integer: current_sentence is length(keyword_assignment) times 1;
const proc: main is func
local
var integer: number is 0;
var integer: position is 0;
var string: user_input is "";
var string: last_input is "";
var string: keyword_stri is "";
var string: keyword_found is "";
var integer: keyword_num is 0;
var integer: keyword_pos is 0;
var string: replace1 is "";
var string: replace2 is "";
var string: replaced_stri is "";
var integer: sentence_num is 0;
var string: sentence is "";
begin
for number range 1 to 25 do
writeln;
end for;
writeln("ELIZA - The Computer Psychiatrist");
writeln("Original ELIZA by Joseph Weizenbaum, MIT, 1965");
writeln;
writeln;
writeln;
writeln("Why have you requested this session?");
repeat
repeat
write("? ");
readln(user_input);
user_input := " " & user_input & " ";
user_input := replace(user_input, "'", "");
if pos(user_input, "shut") <> 0 then
writeln("Good-bye!");
exit(PROGRAM);
end if;
if user_input = last_input then
writeln("Please don't repeat yourself!");
end if;
until user_input <> last_input;
keyword_num := 0;
for number range 1 to length(keyword_table) do
keyword_stri := keyword_table[number];
if keyword_num = 0 then
position := pos(upper(user_input), upper(keyword_stri));
if position <> 0 then
keyword_num := number;
keyword_pos := position;
keyword_found := keyword_stri;
end if;
end if;
end for;
if keyword_num = 0 then
keyword_num := 36;
else
replaced_stri := " " & user_input[keyword_pos + length(keyword_found) ..] & " ";
for number range 1 to length(replace_words) do
replace1 := replace_words[number][1];
replace2 := replace_words[number][2];
for position range 1 to length(replaced_stri) do
if replaced_stri[position len length(replace1)] = replace1 then
replaced_stri := replaced_stri[.. pred(position)] &
replace2 & replaced_stri[position + length(replace1) ..];
position +:= length(replace2);
elsif replaced_stri[position len length(replace2)] = replace2 then
replaced_stri := replaced_stri[.. pred(position)] &
replace1 & replaced_stri[position + length(replace2) ..];
position +:= length(replace1);
end if;
end for;
end for;
if replaced_stri[2] = ' ' then
replaced_stri := replaced_stri[2 ..];
end if;
replaced_stri := replace(replaced_stri, "!", "");
end if;
sentence_num := keyword_assignment[keyword_num];
sentence := example_sentence[sentence_num][current_sentence[keyword_num]];
incr(current_sentence[keyword_num]);
if current_sentence[keyword_num] > length(example_sentence[sentence_num]) then
current_sentence[keyword_num] := 1;
end if;
if sentence[length(sentence)] <> '*' then
writeln(sentence);
else
writeln(sentence[.. pred(length(sentence))] <& replaced_stri);
end if;
last_input := user_input;
until FALSE;
end func;