【问题标题】:Prolog: Sentence ParserProlog:句子解析器
【发布时间】:2013-11-24 21:14:58
【问题描述】:

现在已经坐了几个小时了,只是盯着这段代码,不知道我做错了什么。我知道通过跟踪代码会发生什么(当它遇到动词短语时,它会进入一个永恒的循环)。任何提示都更受欢迎。谢谢。

% 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).

【问题讨论】:

    标签: prolog dcg


    【解决方案1】:

    在网上浏览了一番之后,我现在想通了,所以如果其他人对此感到困惑,我会在这里回答。

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

    例如

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      相关资源
      最近更新 更多