Prolog 句子分析器

Prolog 句子分析器,prolog,dcg,Prolog,Dcg,我已经在这里坐了几个小时了,只是盯着这个代码,不知道我做错了什么。通过跟踪代码,我知道发生了什么(当它点击VerbPhase时,它将进行一个永恒的循环)。任何提示都欢迎。多谢各位 % Knowledge-base det(the). det(a). adjective(quick). adjective(brown). adjective(orange). adjective(sweet). noun(cat). noun(mat). noun(fox). noun(cucumber). n

我已经在这里坐了几个小时了,只是盯着这个代码,不知道我做错了什么。通过跟踪代码,我知道发生了什么(当它点击VerbPhase时,它将进行一个永恒的循环)。任何提示都欢迎。多谢各位

% Knowledge-base
det(the).
det(a).

adjective(quick).
adjective(brown).
adjective(orange).
adjective(sweet).

noun(cat).
noun(mat).
noun(fox).
noun(cucumber).
noun(saw).
noun(mother).
noun(father).
noun(family).
noun(depression).

prep(on).
prep(with).

verb(sat).
verb(nibbled).
verb(ran).
verb(looked).
verb(is).
verb(has).

% Sentece Structures
sentence(Phrase) :-
       append(NounPhrase, VerbPhrase, Phrase),
       nounPhrase(NounPhrase),
       verbPhrase(VerbPhrase).

sentence(Phrase) :-
 verbPhrase(Phrase).

nounPhrase([]).

nounPhrase([Head | Tail]) :-
 det(Head),
 nounPhrase2(Tail).

nounPhrase(Phrase) :-
 nounPhrase2(Phrase).

nounPhrase(Phrase) :-
 append(NP, PP, Phrase),
 nounPhrase(NP),
 prepPhrase(PP).

nounPhrase2([]).

nounPhrase2(Word) :-
 noun(Word).

nounPhrase2([Head | Tail]) :-
 adjective(Head),
 nounPhrase2(Tail).

prepPhrase([]).

prepPhrase([Head | Tail]) :-
 prep(Head),
 nounPhrase(Tail).

verbPhrase([]).

verbPhrase(Word) :-
 verb(Word).

verbPhrase([Head | Tail]) :-
 verb(Head),
 nounPhrase(Tail).

verbPhrase(Phrase) :-
 append(VP, PP, Phrase),
 verbPhrase(VP),
 prepPhrase(PP).

我在网上浏览了一段时间后发现了这一点,所以如果有人对此感到困惑,我会在这里回答

问题是追加正在创建一个空列表。此列表作为参数传递,然后再次拆分为两个空列表。这是一次又一次的重复。要停止此操作,每次使用append函数时,必须检查列表是否为空

比如说

verbPhrase(Phrase):-
append(VP, PP, Phrase),
VP \= [],
PP \= [],
verbPhrase(VP),
prepPhrase(PP).

我在网上浏览了一段时间后发现了这一点,所以如果有人对此感到困惑,我会在这里回答

问题是追加正在创建一个空列表。此列表作为参数传递,然后再次拆分为两个空列表。这是一次又一次的重复。要停止此操作,每次使用append函数时,必须检查列表是否为空

比如说

verbPhrase(Phrase):-
append(VP, PP, Phrase),
VP \= [],
PP \= [],
verbPhrase(VP),
prepPhrase(PP).