0

我正在尝试编写一个 Prolog 程序,该程序接受用户输入并根据输入的语义做出决定(基本上是一个聊天机器人)。

我最初认为(不是逻辑程序员)我会检查多组单词(即疑问句、家庭单词、具有好/坏情感内容的单词),然后将描述所用单词类型的原子累积到一个列表中,然后对照该列表来决定要说什么。

显然,这不起作用,所以现在我几乎迷路了。有没有办法在 Prolog 中做这样的事情,或者我认为这一切都错了?

4

4 回答 4

1

使用 findall

说你有:

a('你好吗','好')。a('你好吗','不错')。

findall( 答案, a('你好吗', 答案), 答案)

在这种情况下,答案将有一个项目列表(在这种情况下,“好”和“不错”)

于 2013-05-02T12:47:33.263 回答
1

你熟悉setof/3吗?

它使您可以在列表中收集结果集:

setof( Category, is_a(Word, Category), L),
say_something(Word, L), 
....
于 2013-05-02T05:53:08.723 回答
0

ELIZA,快 50 岁(!)生日了,这是我所知道的最著名的聊天机器人。您可以搜索“ELIZA prolog source”并获取第一个答案,然后根据您的需要调整数据库。

于 2013-05-02T05:55:54.103 回答
0

好的,我的回答最终摆脱了列表的想法,而是使用了谓词:

maleWords(  [he, him, his]).
femaleWords([she, her, hers]).
familyWords([mother, mom, father, dad, sister, brother]).
goodEmotes( [happy, ecstatic, great, good, yay, love, like, better, best]).
badEmotes(  [sad, bad, horrible, depressed, sucks, death, die, died, dead]).

chat:-writeln('What\'s on your mind?'), bot.
bot:-readln(A),
 maplist(downcase_atom, A, X),
 X \= [quit] ->
    maplist(checkAtom,X,Temp),
    sort(Temp,List),
    wrt(List),
    bot;
 writeln('Fine. Ruin your life then.').

checkAtom(A,X):-
 maleWords(Y),   member(A,Y)-> X = maleW;
 femaleWords(Y), member(A,Y)-> X = femaleW;
 familyWords(Y), member(A,Y)-> X = family;
 goodEmotes(Y),  member(A,Y)-> X = goodEm;
 badEmotes(Y),   member(A,Y)-> X = badEm;
 X = nothing.

wrt(List):-
 sublist([badEm,family,femaleW],List) -> writeln('Do you feel like she\'s causing your problems?');
 sublist([badEm,family,maleW],List) -> writeln('Do you feel like he\'s causing your problems?');
 sublist([family,femaleW,goodEm],List) -> writeln('Do you feel like she\'s helping you?');
 sublist([family,goodEm,maleW],List) -> writeln('Do you feel like he\'s helping you?');
 writeln('Hmm.*writes on clipboard*').

%%%%%%% Got this predicate from another SO question... %%%%%%%%%%
sublist( [], _ ).
sublist( [X|XS], [X|XSS] ) :- sublist( XS, XSS ).
sublist( [X|XS], [_|XSS] ) :- sublist( [X|XS], XSS ).
于 2013-06-24T15:56:53.643 回答