Prolog Eliza 实现 - 循环和递归
Posted
技术标签:
【中文标题】Prolog Eliza 实现 - 循环和递归【英文标题】:Prolog Eliza implementation - Loops and Recursion 【发布时间】:2021-07-20 23:42:33 【问题描述】:有人可以向我解释为什么这段代码不能正常工作吗?它陷入了一个说假的循环中。我不太确定如何运行它,这可能是问题所在。我希望它看起来像这样
Ready> [you,are,a,computer].
Eliza: i am not a computer
Ready> [do,you,speak,portuguese].
Eliza: no, i speak russian
Ready>
change(X,Y):- X = 'you', Y = 'i'.
change(X,Y):- X = 'i', Y = 'why do you'.
change(X,Y):- X = 'are', Y = 'am not'.
change(X,Y):- X = 'do', Y = 'no'.
change(X,Y):- X = 'portuguese', Y = 'russian'.
eliza :-
write('Hello! My name is eliza.'), nl,
eliza_loop.
eliza_loop :-
write('Eliza > '),
read(Input), respond(Input).
respond(Input) :-
member(Term, Input),
member(Term, [ quit, exit, leave ]),
write('Goodbye!').
respond([my,name,is,Name | _ ]) :-
write('Hello, '), write(Name), write('! Pleased to meet you.'), nl,
eliza_loop.
respond([my,Thing,is,called,Name | _ ]) :-
write(Name), write(' is a nice name for a '), write(Thing), write('.'), nl,
eliza_loop.
respond(Input) :-
member(Animal, Input),
member(Animal, [ cat, dog, fish, hamster, gerbil, snake, tortoise ]),
write('You just mentioned your '), write(Animal), write('. Tell me more about your '), write(Animal), nl,
eliza_loop.
respond(Input) :-
member(Term, Input),
member(Term, [ hate, dislike ]),
member(Term2, Input),
member(Term2, [ you ]),
write(':('), nl,
eliza_loop.
respond([ _ ]) :-
retract(list_of_excuses([ Next | Rest ])),
append(Rest, [ Next ], NewExcuseList),
asserta(list_of_excuses(NewExcuseList)),
write(Next), nl,
eliza_loop.
【问题讨论】:
【参考方案1】:我确实工作。
例如,如果您输入[dog].
(带有点!):
在文件eliza.pl
中加载eliza:
?- [eliza].
true.
运行它,然后输入[dog].
(一个列表,后跟一个点。请注意,SWI-Prolog 不接受最终的]
并出于某种原因发出 CR):
?- eliza.
Hello! My name is eliza.
[dog].> [dog
You just mentioned your dog. Tell me more about your dog
Eliza > |:
不过,read(Input)
仍然是个问题,因为它应该读取“术语”(即序列化输出)。
如果您的 Prolog 中有 read_line_to_string/2,请使用它,然后使用 atom_split/3 之类的内容来断开空格处的字符串。
也可以使用format/2 进行打印。
【讨论】:
以上是关于Prolog Eliza 实现 - 循环和递归的主要内容,如果未能解决你的问题,请参考以下文章