将原子累积到列表中而不进行递归Prolog

时间:2013-05-02 02:01:52

标签: prolog

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

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

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

4 个答案:

答案 0 :(得分:1)

您熟悉setof/3吗?

它允许您在列表中收集结果集:

setof( Category, is_a(Word, Category), L),
say_something(Word, L), 
....

答案 1 :(得分:1)

使用findall

说你有:

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

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

在这里,答案将有一个项目列表(在这种情况下,'好'和'不坏')

答案 2 :(得分:0)

ELIZA,即将迎来50(!)个生日,这是我所知道的最着名的聊天机器人。您可以谷歌搜索“ELIZA prolog source”并获得第一个答案,然后根据您的需求调整数据库。

答案 3 :(得分: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 ).