Usage
Template string is written in Handlebar or Mustache format. Following is a sample temple string.
- if-else construct
{{#if no-data}}I looked for{{else}}Here are{{/if}} the industries for {{company}}.
- if greater than construct
You have to pay {{#if_gt test '1.32'}}more {{else}} not more {{/if_gt}}
- if less than equal construct
You have to pay {{#if_leq test '1.32'}} less than {{else}} not less than {{/if_leq}}
- string match
{{#iff name 'Adal'}} Hallo {{else}} Hi {{/iff}}
- Multi-Map values
I did not understand what is meant by {{#missed-phrases}}'{{phrase}}'{{#if conjunction}} {{conjunction}} {{/if}}{{/missed-phrases}}.
In above example missed-phrases is a multi-map. Where each value is a map containing a mandatory entry with key as phrase and an optional entry with conjunction as key.
With only one entry in each map, this could be used as a list.
Example:
{
"templateID": "test-discounts_template",
"description": "",
"footNoteId":[""],
"templateStrings":["You have to pay {{payment}}. But you are eligible for the following discounts: {{#discounts}}\n {{@index_1}})For {{type}} you will get {{value}} discount{{/discounts}}."],
"templateParameters": [
{
"name": "discounts",
"type":"multi-map",
"required": true,
"default_value": ""
},
{
"name":"payment",
"type":"single-value",
"required":true,
"default_value":""
}
],
"headTemplates":[""],
"headTemplateParameters": [
],
"footTemplates":["You are eligible for the above discounts because {{reason}}."],
"footTemplateParameters": [
{
"name": "reason",
"type":"single-value",
"required": true,
"default_value": ""
}
]
}
Input:
{
"templateID": "test-discounts_template",
"templateParameters": {
"discounts": [{
"type": "discount type 1",
"value": "5"
}, {
"type": "discount type 2",
"value": "25 %"
}],
"payment": "500$"
}
}
Output:
You have to pay 500$. But you are eligible for the following discounts:
1)For discount type 1 you will get 5 discount
2)For discount type 2 you will get 25 % discount.
Check sample specification file here.
templateStrings parameter in input json object is a list of strings. Each string will be considered as one candidate template string and during run-time one of template will be chosen at random.
- You can download the latest jar from: jar
- Maven dependency:(Not a published library, it has to be build in the system before adding the dependency)
<dependency>
<groupId>com.twig.template.core</groupId>
<artifactId>twig-template-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
Driver class is TwigTemplateDriver
import com.twig.template.core.processor.TwigTemplateDriver
templateDriver.getNLGFromJsonString(inputJson)
- Example usage script:
import com.twig.template.core.exceptions.TwigTemplateException;
import com.twig.template.core.processor.InputProcessor;
import com.twig.template.core.processor.TwigTemplateDriver;
import com.twig.template.core.util.FileProcess;
public class Test {
private static final String TEST_TEMPLATE_FILE = "resources/testfiles/test-templates.json";
public static void main(String[] args) {
String jsonInput = FileProcess.readFile("resources/testfiles/discount-template-input.json");
TwigTemplateDriver driver = new TwigTemplateDriver(TEST_TEMPLATE_FILE);
String o;
try {
o = driver.getNLGFromJsonString(jsonInput);
System.out.println(o);
} catch (TwigTemplateException e) {
e.printStackTrace();
}
}
}
Sample usage file: Example