Prolog: Sentence Parser Problem

Posted by Devon on Stack Overflow See other posts from Stack Overflow or by Devon
Published on 2010-04-29T13:51:01Z Indexed on 2010/04/29 14:37 UTC
Read the original article Hit count: 344

Filed under:

Hey guys,

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

© Stack Overflow or respective owner

Related posts about swi-prolog