Adding random variation to your text
Reading the current output, you might notice it is monotonous — more like a robot’s report than a human’s. Every office is described in exactly the same way. Let’s add some variation to make the output more interesting. For this, we use the chooseAtRandom
function. We provide to the function a selection of options, and then every time a request comes in, the function picks a different option.
Instead of saying made in every report, let’s change it so that the output can alternate with the phrases sold a total of or had sales worth. As ATL, that looks like this:
[[chooseAtRandom('made','sold a total of','had sales worth')]]
We’ll add the chooseAtRandom
function in two places:
once to randomize the ways of saying made in one sentence
and a second time to provide a separate sentence that reports on sales in a different way.
After adding this randomization to our narrative, we’ll then make the system randomly choose between the two different sentence options.
Randomizing: Example 1
In the DescribeOffice script, add
chooseAtRandom
after the word office, like this:The [[office.name]] office [[chooseAtRandom]] made [[currencyFormat(office.sales,'','¤#,###')]].
To specify the parameters for the function, replace the word made with three options for expressing the same meaning
('made','sold a total of','had sales worth')
like this:chooseAtRandom('made','sold a total of','had sales worth')
Your sentence with the
chooseAtRandom
function should look like this:The [[office.name]] office [[chooseAtRandom('made','sold a total of','had sales worth')]] [[currencyFormat(office.sales,'','¤#,###')]].
Go to the Main script and click Preview.
Each time you click Preview, your system will select an option at random. With , you can supply as many arguments as you like. The more arguments you supply, the more variation you get.
You can even add variation at a higher level, varying phrases or whole sentences. Let’s add to our script a second way of describing sales. This time let’s use a shortcut to make it easier.
Randomizing: Example 2
In the DescribeOffice script, add the following sentence below the current one:
In [[office.name]], we saw, there was, we achieved [[currencyFormat(office.sales,'','¤#,###')]] in sales.
What are the options for choosing at random here? They are: we saw, there was, and we achieved.
Now let’s put the
chooseAtRandom
function into the sentence and make sure the brackets and single quotes appear as correct ATL:In [[office.name]], [[chooseAtRandom('we saw', 'there was', 'we achieved')]] [[currencyFormat(office.sales,'','¤#,###')]] in sales.
Go to the Main script and click Preview. What do we have now?
Each office has two sentences about how it performed. Now let’s make the system choose from those two sentences at random.
Return to the DescribeOffice script and type a vertical bar between the two sentences. In ATL, vertical bars separate different options.
Select both sentences (plus the vertical bar), then click on Insert Function and choose language > chooseAtRandom.
This opens the Function Builder dialog with the
chooseAtRandom
function selected. Because you selected the sentences and vertical bar separator before choosing the function, the builder has already populated the first two parameters with the two sentences:Click Save.
The two sentences and vertical separator should now be replaced with the following ATL:
[[chooseAtRandom("The [[office.name]] office [[chooseAtRandom('made','sold a total of','had sales worth')]] [[currencyFormat(office.sales,'','¤#,###')]].","In [[office.name]], [[chooseAtRandom('we saw','there was','we achieved')]] [[currencyFormat(office.sales,'','¤#,###')]] in sales.")]]
Go to the Main script and click Preview.
Now we see only one sentence reporting on each office’s sales in the report. If you scroll down, you’ll see that NLG Studio has given us randomly selected instances of the two sentence options.
What have we learned?
In these examples, we have added random variation in two ways: once by adding options for how to talk about sales achieved (in two different sentences), and a second time to make the system choose between those two sentences.
In this step of the tutorial, we have learned the following:
Use of the
chooseAtRandom
function.You can increase the amount of variation by supplying more arguments to the function.
You can add variation to more than just words or phrases but to whole sentences if you like.
In ATL, a vertical bar is used to separate different options.
chooseAtRandom
is one of NLG Studio’s language functions. Next, we’ll use a few of the other language functions in ATL.