Prolog: Sentence Parser Problem
- by Devon
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).