Definite Clause Grammars in Lisp – Part 2

Written by on June 4, 2017 in LISP, Natural Language Processing, Programming with 0 Comments

In the last post, I showed how we can implement DCGs in LispWorks using the KnowledgeWorks package. The grammar discussed in that post did not take into account subject/predicate number agreement. This is one of the basic constraints in English grammar. Today I will show how easy it is to encode this constraint.

Here is a Prolog DCG:

Prolog Grammar

Prolog Grammar

You can see that Number is passed as a parameter to the main non-terminals NP and VP. As is expected, different words have a corresponding encoding, for example book is marked as singular noun and sleep is marked as plural intransitive verb.

Similar to what we did in the earlier grammar, each grammar rule in Lisp will take an argument that represents the Number to use.

Grammar in Lisp

Grammar in Lisp

Note how we ignore the Number agreement for the Noun Phrase that appears after the Transitive Verb.

The top level grammar for sentence s says that whatever number is chosen for the Noun Phrase is to be satisfied by the matching Verb Phrase. So, if the input sentence has a plural Noun Phrase, then it must be followed by a Verb Phrase that is also plural.

Here are some inputs to our parser:

CP-USER 2 > (parse-grammar ‘s ‘(he sleeps))

(S (HE SLEEPS) NIL)

T

So the input is accepted.

CP-USER 3 > (parse-grammar ‘s ‘(he sleep))

NIL

NIL

The input is rejected.

CP-USER 4 > (parse-grammar ‘s ‘(they write books))

(S (THEY WRITE BOOKS) NIL)

T

Input is accepted.

That is it! Fairly straightforward to model.

One thing you would have noticed is that we do not get a parse tree of the input if parsing is successful. This is not very useful.

In the next post, I will show how we can enhance the grammar to support Number agreement and also construct the parse tree along the way.

You can download the grammar here.

Tags: , ,

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *

Top