foundations of computational agents
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 directly into queries on a knowledge base. To do this, make the following simplifying assumptions, which are not always true, but form a useful first approximation:
nouns and adjectives correspond to properties
verbs and prepositions correspond to a binary relation between two individuals, the subject and the object.
In this case, a noun phrase becomes an individual with a set of properties defining it. To answer a question, the system can find an individual that has these properties. A noun phrase followed by a verb phrase describes two individuals constrained by the verb.
In the sentence, “a tall student passed a math course”, the phrase “a tall student” is the subject of the verb “passed” and the phrase “a math course” is the object of the verb. For the individual that is the subject, and are true. For the individual that is the object, and . The verb specifies that . Thus the question “Who is a tall student that passed a math course?” can be converted into the query:
The phrase “a tall student enrolled in cs312 that passed a math course” could be translated into
% A noun phrase is a determiner followed by adjectives followed by a noun followed by an optional modifying phrase:
% Adjectives consist of a sequence of adjectives.
% An optional modifying phrase / relative clause is either a relation (verb or preposition) followed by a noun-phrase or nothing
% is true if is an adjective that is true of
% is true if is a noun that is true of
% The following are for proper nouns:
% is true if is a relation on individuals and
Figure 13.11 shows a simple grammar that parses an English question and answers it at the same time. This ignores most of the grammar of English, such as the differences between prepositions and verbs or between determiners and adjectives, and makes a guess at the meaning, even if the question is not grammatical. Adjectives, nouns and noun phrases refer to an individual. The extra argument to the predicates is an individual which satisfies the adjectives and nouns. Here an is a modifying phrase, which could be a prepositional phrase or a relative clause. A , either a verb or a preposition, is a relation between two individuals, the subject and the object, so these are extra arguments to the predicate.
Suppose means is an answer to question , where a question is a list of words. The following provides some ways questions can be asked from the clauses of Figure 13.11, even given the very limited vocabulary used there.
The following clause allows it to answer questions, such as “Is a tall student enrolled in a computer science course?” and returns the student:
The following rule is used to answer questions, such as “Who is enrolled in a computer science course?”, or “Who is enrolled in cs312” (assuming that is true):
The following rule is used to answer questions, such as “Who is a tall student?”:
The following rule allows it to answer questions, such as “Who is tall?”:
The following rule can be used to answer questions, such as “Which tall student passed a computer science course?” or even “Which tall student enrolled in a math course passed a computer science course?”:
The following rule allows it to answer questions that have “is” between the noun phrase and the modifying phrase, such as “Which tall student is enrolled in a computer science course?” or “Which student enrolled in a math course is enrolled in a computer science course?”:
The preceding grammar directly found an answer to the natural language question. One problem with this way of answering questions is that it is difficult to separate the cases where the program could not parse the language from the case where there were no answers; in both cases the answer is “no”. This makes it difficult to debug such a program. An alternative is instead of directly querying the knowledge base while parsing, to build a logical form of the natural language – a logical proposition that conveys the meaning of the utterance – before asking it of the knowledge base. The semantic form can be used for other tasks such as telling the system knowledge, paraphrasing natural language, or even translating it into a different language.
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 13.12, and they are used with the dictionary of Figure 13.13.
% A noun phrase is a determiner followed by adjectives followed by a noun followed by an optional prepositional phrase.
% Adjectives consist of a sequence of adjectives.
% An optional prepositional phrase is either nothing or a preposition followed by a noun phrase.
In this grammar,
means that list is an ending of list , and the words in before form a noun phrase. This noun phrase refers to the individual . is an ending of , and the formulas in , but not in , are the constraints on the individual imposed by the noun phrase.
Procedurally, is the list of words to be parsed, and is the list of remaining words after the noun phrase. is the list of conditions coming into the noun-phrase, and is with the extra conditions imposed by the noun-phrase added.
If the elements of list are queried against a database that uses these relations and constants, precisely the tall students enrolled in a computer science course could be found.