Prolog:句子解析器
我已经坐在这里好几个小时了,只是盯着这段代码,不知道我做错了什么。我通过跟踪代码知道发生了什么(当它遇到 verbPhrase 时,它会进入一个永恒的循环)。任何提示都非常受欢迎。谢谢。
% 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).
Been sat here for hours now just staring at this code and have no idea what I'm doing wrong. I know what's happening from tracing the code through (it is going on an eternal loop when it hits verbPhrase). Any tips are more then welcome. Thank you.
% 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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一番网上搜索后,我现在弄清楚了,所以如果其他人遇到困难,我会在这里回答。
问题是追加创建了一个空列表。该列表作为参数传递,然后再次拆分为两个空列表。这被一遍又一遍地重复。为了阻止这种情况,每次使用追加函数时,都必须检查列表是否为空。
例如
I figured it out now after a bit of trolling the internet, so will answer it here if anyone else struggles with it.
The problem was that the append was creating an empty list. This list was passed as a parameter, then split again into two empty lists. And this was repeated over and over again. To stop this, everytime the append function is used, there must be a check if the lists are empty.
For example