我使用swiprolog并知道一些知识,例如:
det(the).
det(a).
adjective(quick).
adjective(brown).
noun(cat).
noun(fox).
prep(on).
prep(with).
verb(sat).
verb(ran).
我有一条规则
generate_grammar(GrammarList):-
new_rule(GrammarList).
此规则会传递一个包含未知数量元素的列表,例如[det,noun,verb,prep]
并且应该为传递的“语法”生成新规则。新规则应该生成一个带有给定事实和新语法的“句子”。我测试了一些东西,但我没有得到它的工作。
我认为所需的规则类似于:
new_rule(List) :-
Head=sentence(X),
Body=[List]
dynamic(Head),
assertz(Head :- Body).
我不知道如何做到这一点。那怎么可能呢?
提前致谢!
答案 0 :(得分:4)
虽然可能使用assertz/1
等动态地向商店添加规则。人。我从来没有真正去做过,因为让Prolog评估其他数据结构太容易了。
对于您的特定数据库,您的所有事实都是1,所以我们可以使用call/2
来评估它们:
?- call(det, D).
D = the ;
D = a.
利用这一点,您可以使用maplist/2
解决问题,而无需任何实际的中间结构:
sentence(Grammar, Words) :- maplist(call, Grammar, Words).
使用它:
?- maplist(call, [det,adjective,noun,verb], Sentence).
Sentence = [the, quick, cat, sat] ;
Sentence = [the, quick, cat, ran] ;
Sentence = [the, quick, fox, sat] ;
Sentence = [the, quick, fox, ran] ;
Sentence = [the, brown, cat, sat] ;
Sentence = [the, brown, cat, ran] ;
Sentence = [the, brown, fox, sat] ;
Sentence = [the, brown, fox, ran] ;
Sentence = [a, quick, cat, sat] ;
Sentence = [a, quick, cat, ran] ;
Sentence = [a, quick, fox, sat] ;
Sentence = [a, quick, fox, ran] ;
Sentence = [a, brown, cat, sat] ;
Sentence = [a, brown, cat, ran] ;
Sentence = [a, brown, fox, sat] ;
Sentence = [a, brown, fox, ran].
答案 1 :(得分:4)
您可以通过为已知语法类别添加事实来将其转换为真正的关系:
category(det).
category(adjective).
category(noun).
category(prep).
category(verb).
然后,您可以描述属于这些类别的类别和单词之间的关系:
cat_word(C,W) :-
category(C),
call(C,W).
?- cat_word(C,W).
C = det,
W = the ;
C = det,
W = a ;
C = adjective,
W = quick ;
C = adjective,
W = brown .
.
.
.
最后,根据@Daniel Lyons提出的想法,您可以将maplist / 3应用于cat_word / 2:
grammar_sentence(G,S) :-
maplist(cat_word,G,S).
在语法到句子方向上,这产生与Daniels谓词句子2相同的结果:
?- grammar_sentence([det,adjective,noun,verb],S).
S = [the, quick, cat, sat] ;
S = [the, quick, cat, ran] ;
S = [the, quick, fox, sat] ;
S = [the, quick, fox, ran] ;
S = [the, brown, cat, sat] ;
S = [the, brown, cat, ran] ;
S = [the, brown, fox, sat] ;
S = [the, brown, fox, ran] ;
S = [a, quick, cat, sat] ;
S = [a, quick, cat, ran] ;
S = [a, quick, fox, sat] ;
S = [a, quick, fox, ran] ;
S = [a, brown, cat, sat] ;
S = [a, brown, cat, ran] ;
S = [a, brown, fox, sat] ;
S = [a, brown, fox, ran].
但是grammar_sentence / 2也可以在句子中用于语法方向:
?- grammar_sentence(G,[the,quick,cat,sat]).
G = [det, adjective, noun, verb] ;
false.
?- grammar_sentence(G,[the,cat,sat]).
G = [det, noun, verb] ;
false.