Skip to content

Section 5 Generating a simple sentence

Ruud de Jong edited this page Oct 24, 2018 · 1 revision

This page is based on a page of the wiki for the original SimpleNLG.

In the sample Java class TestMain shown earlier, we have the statements:

    import simplenlg.framework.*;
    import simplenlg.lexicon.*;
    import simplenlg.realiser.*;
    import simplenlg.phrasespec.*;
    import simplenlg.features.*;

These classes allow you to specify the parts of speech of a sentence and to perform various operations on them. It’s important to note that SimpleNLG provides only simple grammars: its notions of a sentence, noun phrase, etc., are very basic and are by no means representative of the incredibly varied and complicated grammars of any of the languages.

Let’s see how we would define and combine various parts of speech to generate a simple sentence such as Marie gooit de bal 'Mary throws the ball'. This example shows a use of the Dutch part of SimpleNLG-NL, but it is very similar for the English part (and perhaps French, too).

As discussed in Section 3, we’ll make use of the SimpleNLG construct SPhraseSpec, which allows us to define a sentence or a clause in terms of its syntactic constituents. This is useful because it allows us to hand different parts of a clause to SimpleNLG, in no particular order, and SimpleNLG will assemble those parts into the appropriate grammatical structure.

    SPhraseSpec p = nlgFactory.createClause();
    p.setSubject("Marie");
    p.setVerb("gooien");
    p.setObject("de bal");

The above set of calls to SimpleNLG-NL defines the components of the sentence we wish to construct: we have specified a subject, a verb and an object for our sentence. Now, all that remains is to use the Realiser, which will take these different components of the sentence, combine them, and realise the text to make the result syntactically and morphologically correct:

    String output2 = realiser.realiseSentence(p); // Realiser created earlier.
    System.out.println(output2);

The resulting output is:

    Marie gooit de bal.

When parts of speech are defined and assembled into an instance of the SPhraseSpec class, methods associated with that class, such as setSubject, setVerb and setObject, assemble the parts of speech by obeying the simple grammar embodied in SimpleNLG-NL [1].

→ For more examples on clauses, look at testsrc/ClauseTest.java.


[1] And, as we will see later, rules of grammar will have also been enforced in building up the smaller constituents of the sentence (such as NPPhraseSpec and PPPhraseSpec) to ensure they are well-formed. Thus, the rules of grammar which SimpleNLG-NL implements are not defined within a single module of the SimpleNLG code but instead are spread throughout the various class definitions.