Information Technology Reference
In-Depth Information
Figure 8.6.
A parser of noun phrases in Prolog
np.pl
np([Name],X) :- proper_noun(Name,X).
np([Art|Rest],X) :- article(Art), np2(Rest,X).
np2([Adj|Rest],X) :- adjective(Adj,X), np2(Rest,X).
np2([Noun|Rest],X) :- common_noun(Noun,X), mods(Rest,X).
mods([],_).
mods(Words,X) :-
append(Start,End,Words), % Break the words into two pieces.
pp(Start,X), % The first part is a PP.
mods(End,X). % The last part is a Mods again.
pp([Prep|Rest],X) :- preposition(Prep,X,Y), np(Rest,Y).
8.2.3 Writing a parser
In writing a parser of noun phrases, the idea is that each nonterminal category in
the grammar will have its own predicate in the parser. (The terminal categories are
already defined in the lexicon.) Each such predicate takes two arguments:
A list of words to be parsed
An object in the world model
The lists studied in chapter 7 are now used to represent sequences of words . Each
predicate in the parser will hold if the sequence of words is both of the category
stated by the predicate and can be used to refer to the object according to the facts in
the world model.
The parser corresponding to the grammar for noun phrases shown in figure 8.1
appears in figure 8.6. Note how closely this Prolog program follows the grammar.
Each rule (in the noun phrase portion of the grammar) becomes a clause in Prolog.
Typically each predicate in the parser uses list notation to extract the first word in
the sequence (the head of the list) and the remaining words (the tail of the list). For
example, the first clause for np2 says that the head can be an adjective and the tail
can be another np2 , both describing the same object X in the world model. (The parser
uses Prolog variables with descriptive names like Adj and Noun instead of A and N ,
but this is just to help keep track of what is being looked for.)
The rule for a pp is similar. The first word must be a preposition, and the remaining
words must form an np . In this case, however, there are two objects in the world
model to consider. The prepositional phrase can be used to refer to an object X only if
the embedded noun phrase can be used to refer to another object Y , where the X and
 
 
Search WWH ::




Custom Search