Skip to content

01 – Build your first sentence

Kira edited this page Apr 14, 2019 · 10 revisions

SimpleNLG German lets you build sentences in a way most similar as possible to SimpleNLG English. However, German grammar is different to English grammar and sometimes more complex. Therefore, in some cases additional user input is possible and required for certain sentence forms. This is also explained throughout this tutorial.

Setup

First, you need to create a Java program which can call SimpleNLG's methods. To do so, add the SimpleNLG .jar file to the classpath of your new Java project. Create a new Java class containing a main method.

In this class, add the following import statements and create the most important modules needed for using SimpleNLG. These modules only have to be created once at the beginning.

import main.java.simplenlgger.framework.*;
import main.java.simplenlgger.lexicon.Lexicon;
import main.java.simplenlgger.realiser.german.Realiser;
import main.java.simplenlgger.phrasespec.*;
import main.java.simplenlgger.features.*;

public class MyClass {
    public static void main(String[] args) {
        Lexicon lexicon = Lexicon.getDefaultLexicon();
        NLGFactory nlgFactory = new NLGFactory(lexicon);
        Realiser realiser = new Realiser(lexicon);
    }
}

Now, the setup is done and we can proceed with building the first sentence.

Create a simple sentence

SimpleNLG offers several ways of building sentences. The simplest one consists of so called "canned text", meaning the string should be just realised as it is. An example for a canned text can be seen below. The only thing SimpleNLG does here is to capitalise the 1st character and to terminate the sentence with a period.

NLGElement sentence = nlgFactory.createSentence("der Hund ist glücklich");
String output = realiser.realiseSentence(sentence);
System.out.println(output);

This is not the way SimpleNLG adds much value, of course, as most work is left to you, for instance inflecting the verb correctly or ordering the words. In the following, you will learn how to leave as much as possible effort to SimpleNLG.

Create a more complex sentence

Let's first create a placeholder for our new sentence, to which we will later add values:

SPhraseSpec sentence = nlgFactory.createClause();

Second, we need to tell SimpleNLG what we want to have as subject, verb, and optional, object(s) in our sentence.

NPPhraseSpec subject = nlgFactory.createNounPhrase("der hund");
VPPhraseSpec verb = nlgFactory.createVerbPhrase("jagen");
NPPhraseSpec object = nlgFactory.createNounPhrase("george");

sentence.setSubject(subject);
sentence.setVerb(verb);
sentence.setObject(object);

String output = realiser.realiseSentence(sentence);
System.out.println(output);

SimpleNLG German will output "Der Hund jagt George.", with the verb inflected and the words correctly ordered. This is obviously a very simple sentence. For building more complex sentences, i.e. with adjectives or subordinate clauses added, in a different tense or voice, please refer to the corresponding chapter in this tutorial.