您有一个 CNL(受控自然语言)规范:规则和情况以(非常)受限的英语子集表示。因此,作业的重点将放在处理语句的“真值”上……在 SWI-Prolog 中,我们可以这样写:
kb :- rules(Rs), maplist(writeln,Rs), situations(Ls), maplist(writeln,Ls).
rules(Rs) :- tokenize_atom('
If it is a nice day and it is summer, then I go to the beach.
If it is a nice day and it is winter, then I go to the canal boating resort.
If it is not a nice day and it is summer, then I go to work.
If it is not a nice day and it is winter, then I go to class.
If I go to the beach, then I swim.
If I go to the canal boating resort , then I go boat riding.
If I go boat riding or I swim, then I have fun.
If I go to work, then I make money.
If I go to class, then I learn something.
', L), maplist(downcase_atom,L,D), phrase(rule(Rs), D).
rule([r(C -> A)|Rs]) --> [if], condition(C), [,], [then], consequence(A), [.], rule(Rs).
rule([]) --> [].
situations(S) :- tokenize_atom('
It is a nice day and it is summer.
It is not a nice day and it is winter.
It is a nice day and it is winter.
It is not a nice day and it is summer.
',L), maplist(downcase_atom,L,D), phrase(situations(S), D).
situations([s(S)|Rs]) --> condition(S), [.], situations(Rs).
situations([]) --> [].
condition(and(A,B)) --> fact(A), [and], condition(B).
condition(or(A,B)) --> fact(A), [or], condition(B).
condition(C) --> fact(C).
consequence(C) --> fact(C).
fact(fact(true, What)) --> [it,is], what(What).
fact(fact(false, What)) --> [it,is,not], what(What).
fact(fact(true, Action)) --> [i], action(Action).
what([A,B,C]) --> [A,B,C].
what([A]) --> [A].
action(L) --> {between(1,6,N),length(L,N)},L.
我们得到
?- kb.
r((and(fact(true,[a,nice,day]),fact(true,[summer]))->fact(true,[go,to,the,beach])))
r((and(fact(true,[a,nice,day]),fact(true,[winter]))->fact(true,[go,to,the,canal,boating,resort])))
r((and(fact(false,[a,nice,day]),fact(true,[summer]))->fact(true,[go,to,work])))
r((and(fact(false,[a,nice,day]),fact(true,[winter]))->fact(true,[go,to,class])))
r((fact(true,[go,to,the,beach])->fact(true,[swim])))
r((fact(true,[go,to,the,canal,boating,resort])->fact(true,[go,boat,riding])))
r((or(fact(true,[go,boat,riding]),fact(true,[swim]))->fact(true,[have,fun])))
r((fact(true,[go,to,work])->fact(true,[make,money])))
r((fact(true,[go,to,class])->fact(true,[learn,something])))
s(and(fact(true,[a,nice,day]),fact(true,[summer])))
s(and(fact(false,[a,nice,day]),fact(true,[winter])))
s(and(fact(true,[a,nice,day]),fact(true,[winter])))
s(and(fact(false,[a,nice,day]),fact(true,[summer])))
true
.
现在应该更简单地处理情况,推断动作......