Advanced examples of NLG features

This is the documentation for 3.1.0 version, which is not the latest version. Consider upgrading to 4.3.0.

Management of empty elements and itemz

RosaeNLG automatically detects empty elements in itemz structures. This is hugely useful to avoid sentences finishing with and. when the last element was not triggered.

Let’s take an example. The last element of the list has a large battery is triggered by a condition: the phone must have a large battery.

With RosaeNLG:

Only using Pug’s features:

The first sentence doesn’t have an . at its end, and has no and between the two elements.

Only using Pug’s features, more sophisticated:

You now have an business condition triggering syntax - which not a good practice at all:

if phone.bigBattery
  | ,
else
  |  and

You have to care to put the |. at the right place.

The template is less readable as you don’t put an explicit structure on what you list.

Enumeration and punctuation

Let’s keep the same example, but we want, when there is a large battery, to end the sentence with a !.

With RosaeNLG:

You can see that we ended the second sentence with has a large battery!: a . was not added after the !. This is completely automatic.

Only using Pug’s features, more sophisticated (as it is an automatic filtering feature, we must add the protect tag to simulate Pug’s original behaviour):

It is a mess:

  • we don’t have capitalization of sentences no more

  • there is a !.

  • there is a missing space in screen,exists

  • there are undesirable line feeds

Utility of hasSaid and recordSaid

When you need to keep track of what was said to influence the production of the next texts, the most straightforward approach for a developer is to use plain variables, here a boolean called SAID_BATTERY:

It may randomly produce these texts, which are not correct:

  • The phone, has a 6 inches screen and exists in red and in black.: there is a strange comma, and we do not talk at all about the battery

  • The phone exists in red and in black, and has a 6 inches screen.: battery is not mentionned at all, and there is a strange , and

This is because RosaeNLG tests if texts are empty or not before producing them for real. When this test is performed, the variables are changed, but RosaeNLG is not able to properly roll back.

You must use recordSaid and hasSaid to safely record states:

You will get these 2 nice sentences: The phone has a 6 inches screen along with a battery of 3300 mAh and exists in red and in black. The phone exists in red and in black, has a battery of 3300 mAh and has a 6 inches screen.

Contractions and gender

Let’s take an example in French:

As you can see:

  • contractions have been made automatically when required like in l’abricot and l’hibiscus, but not in la pomme, le haricot

  • gender of words has been found like magically: la pomme, le haricot

You can add an adjective that will be agreed automatically:

Synonyms global optimization

Let’s take an example where you wish to describe the situation of a client:

You often get sentences like:

  • Mr Dupont is married. This client is engineer. This client is client since 2016.: repetition of client in This client is client

  • Mr Dupont is married. Andre Dupont is engineer. Andre Dupont is client since 2016.: too much Dupont!

There are multiple ways to tweak it, but one is to let RoseaNLG choose the best combination using choosebest, just adding choosebest {among: 20} at the right place:

You will get sentences like:

  • Mr Dupont practices the profession of engineer. He is client since 2016. He is married.

  • Mr Dupont is married. He is engineer. He is client since 2016.

he is a stop word and is therefore, by default, not considered an issue if repeated.

Plain punctuation

With RosaeNLG:

Punctuation and capitalization is just perfect.

Only using Pug’s features, you would have:

Of course, you can fine tune spacing and put capital letters where required, but this is very tedious and error prone.

And when capitalization is dynamic, it becomes even more difficult:

One first complete sentence. And something else. Or just the beginning of a sentence, and something else.