Prolog:句子解析器

发布于 2024-08-30 17:55:41 字数 1249 浏览 3 评论 0原文

我已经坐在这里好几个小时了,只是盯着这段代码,不知道我做错了什么。我通过跟踪代码知道发生了什么(当它遇到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℉絮湮 2024-09-06 17:55:41

经过一番网上搜索后,我现在弄清楚了,所以如果其他人遇到困难,我会在这里回答。

问题是追加创建了一个空列表。该列表作为参数传递,然后再次拆分为两个空列表。这被一遍又一遍地重复。为了阻止这种情况,每次使用追加函数时,都必须检查列表是否为空。

例如

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

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

verbPhrase(Phrase):-
append(VP, PP, Phrase),
VP \= [],
PP \= [],
verbPhrase(VP),
prepPhrase(PP).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文