4
votes

I have DCG sentences with two persons, representing a male and a female. I want to refer to the person metioned in a previous sentence using "he" or "she".

Suppose we have these DCGs:

father --> [Peter].
mother --> [Isabel].

child --> [Guido].
child --> [Claudia].

verb --> [is].
relation --> [father, of].
relation --> [mother, of].

pronoun --> [he].
pronoun --> [she].

adjective --> [a, male].
adjective --> [a, female].

s --> father, verb, relation, child.
s --> mother, verb, relation, child.
s --> pronoun, verb, adjective.

Querying ?- s([Peter, is, father, of, Guido], []). returns true.

How can I make sure that when I query now for ?- s([he, is, a, male], []). should return true only because I already mentioned Peter (a male) in the previous sentence. Otherwise it should return false.

This question uses the same example as here.

1
Of interest: Norvig Chapter 20Guy Coder
You also need to add the ' that were put in the other answer. Peter is a variable, 'Peter' is an atom. See: write_canonical/1Guy Coder
The idea is to add facts like male('Peter') and go from there. Once that is working, then remove the fact and get one sentence to convey the idea to the other sentence. Might need to have first sentence assert facts, or view the two sentences as one semantic idea that have an implied operator between the sentences.Guy Coder

1 Answers

3
votes

You can augment your DCG to keep some state (the gender of the last sentence):

father --> ['Peter'].
mother --> ['Isabel'].

child --> ['Guido'].
child --> ['Claudia'].

verb --> [is].
relation --> [father, of].
relation --> [mother, of].

pronoun(he) --> [he].
pronoun(she) --> [she].

adjective --> [a, male].
adjective --> [a, female].

s(G) --> s(none,G).

s(_,he) --> father, verb, relation, child.
s(_,she) --> mother, verb, relation, child.
s(G,G) --> pronoun(G), verb, adjective.

And now you can chain queries using this state:

?- phrase(s(G1),['Peter', is, father, of, 'Guido']), phrase(s(G1,G2),[he, is, a, male]).
G1 = G2, G2 = he

You may want to modify a bit the DCG to constrain the relations (using the Gender parameter). For example you DCG currently accepts 'Peter' is mother of 'Guido' which I'm not sure was intended.