2
votes

I have a Prolog sentence parser that returns a sentence (passed into it as a list) split into two parts - a Noun_Phrase and a Verb_Phrase. See example below:

sentence(Sentence, sentence(np(Noun_Phrase), vp(Verb_Phrase))) :- 
        np(Sentence, Noun_Phrase, Remainder),  
        vp(Remainder, Verb_Phrase). 

Now I want to take the Noun_Phrase and Verb_Phrase and pass them into another Prolog predicate, but first I want to extract the first term from the Verb_Phrase (which should always be a verb) into a variable and the rest of the Verb_Phrase into another one and pass them separately into the next predicate.

I thought about using unification for this and I have tried:

sentence(Sentence, sentence(np(Noun_Phrase), vp(Verb_Phrase))),
     [Head|Tail] = Verb_Phrase, 
     next_predicate(_, Noun_Phrase, Head, Tail, _).

But I am getting ERROR: Out of local stack exception every time. I think this has something to do with the Verb_Phrase not really being a list. This is a possible isntance of Verb_Phrase:

VP = vp(vp(verb(making), adj(quick), np2(noun(improvements))))

How could I get the verb(X) as variable Verb and the rest of the term as varaible Rest out of such compound term in Prolog?

1

1 Answers

1
votes

You could use =../2 like:

 Verb_Phrase=..[Verb|Rest_Term_list].

Example:

?- noun(improvements)=..[Verb|Rest_Term_list].
Verb = noun,
Rest_Term_list = [improvements].