Third edition of Artificial Intelligence: foundations of computational agents, Cambridge University Press, 2023 is now available (including the full text).

12.6.6 Building a Natural Language Interface to a Database

You can augment the preceding grammar to implement a simple natural language interface to a database. The idea is that, instead of transforming sub-phrases into parse trees, you transform them into a form that can be queried on a database. For example, a noun phrase becomes an individual with a set of predicates defining it.

Example 12.38: The phrase "a female student enrolled in a computer science course" could be translated into
answer(X) ←
     female(X)∧student(X)∧enrolled_in(X,Y)∧course(Y)
     ∧department(Y,comp_science).

Let us ignore the problems of quantification, such as how the words "all," "a," and "the" get translated into quantifiers. You can construct a query by allowing noun phrases to return an individual and a list of constraints imposed by the noun phrase on the individual. Appropriate grammar rules are specified in Figure 12.10, and they are used with the dictionary of Figure 12.11.


% A noun phrase is a determiner followed by modifiers followed by a noun followed by an optional prepositional phrase.

noun_phrase(T0,T4,Obj,C0,C4) ←
     det(T0,T1,Obj,C0,C1)∧
     modifiers(T1,T2,Obj,C1,C2)∧
     noun(T2,T3,Obj,C2,C3)∧
     pp(T3,T4,Obj,C3,C4).

% Modifiers consist of a sequence of adjectives.

modifiers(T,T,Obj,C,C).
modifiers(T0,T2,Obj,C0,C2)←
     adjective(T0,T1,Obj,C0,C1)∧
     modifiers(T1,T2,Obj,C1,C2).

% An optional prepositional phrase is either nothing or a preposition followed by a noun phrase.

pp(T,T,Obj,C,C).
pp(T0,T2,O1,C0,C2)←
     preposition(T0,T1,O1,O2,C0,C1)∧
     noun_phrase(T1,T2,O2,C1,C2).
Figure 12.10: A grammar that constructs a query

In this grammar,

noun_phrase(T0,T1,O,C0,C1)

means that list T1 is an ending of list T0, and the words in T0 before T1 form a noun phrase. This noun phrase refers to the individual O. C0 is an ending of C1, and the formulas in C1, but not in C0, are the constraints on individual O imposed by the noun phrase.


det(T,T,O,C,C).
det([a|T],T,O,C,C).
det([the|T],T,O,C,C).
noun([course|T],T,O,C,[course(O)|C]).
noun([student|T],T,O,C,[student(O)|C]).
noun([john|T],T,john,C,C).
noun([cs312|T],T,312,C,C).
adjective([ computer,science|T],T,O,C,[dept(O,comp_science)|C]).
adjective([female|T],T,O,C,[female(O)|C]).
preposition([enrolled,in|T],T,O1,O2,C,[enrolled(O1,O2)|C]).
Figure 12.11: A dictionary for constructing a query

Example 12.39: The query
ask noun_phrase([a, computer,science,course],[],Obj,[],C).

will return

C=[course(Obj),dept(Obj,comp_science)].

The query

ask noun_phrase([a,female,student,enrolled,in,a,computer,
     science,course],[],P,[],C).

returns

C=[course(X),dept(X,comp_science),enrolled(P,X),student(P),
     female(P)].

If the elements of list C are queried against a database that uses these relations and constants, precisely the female students enrolled in a computer science course could be found.