0

I have defined my grammar in prolog as DCG (Definite Clause Grammar). Now I want to generate some phrases according with the facts present in my knowledge base. For example, if I have likes(mark, julia). I want to generate the sentence

Mark likes Julia.

How can I do this?

false
  • 10,264
  • 13
  • 101
  • 209
  • `print_phrase(Verb) :- call(Verb, X, Y), write(X), write(' '), write(Verb), write(Y), write('.'), nl.` and you can call, `print_phrase(like).` Or better, use mat's answer and then write a simple recursive rule to write the sentence/list out nicer. If you want to capitalize the names, you'll need to dig into the atom/char processing predicates. As a beginner, you should browse the Prolog predicate documentation and play with some of the promising looking ones. – lurker Aug 29 '18 at 16:37

1 Answers1

3

We must take into account that likes/2 is a normal Prolog predicate, not a DCG.

Hence, we use {}//1 to refer to regular Prolog predicates within DCGs.

For example:

sentence -->
        [X, likes, Y],
        { likes(X, Y) }.

Sample usage:

?- phrase(sentence, Ls).
Ls = [mark, likes, julia].
mat
  • 40,498
  • 3
  • 51
  • 78