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:
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.
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.
Recent Comments