You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can specify the language, English or Italian (the default is English), like in the following example:
22
22
```
23
-
node explanations.js templates/italian/feedback.pug graphs/explanationGraph.json italian
23
+
node explanations.js templates/italian/feedback.pug graphs/exp_graph_1_it.csv italian
24
24
```
25
25
26
26
# Basic Tutorial for Templates
@@ -40,39 +40,75 @@ Spaces between different plain texts (except only punctuation) are automatically
40
40
will still output: ```Hello world!```
41
41
42
42
## Variables
43
-
The object passed to RosaeNLG (after the graph has been processed) is an explanation and has the following fields:
44
-
```
45
-
"entity", //name of the food
46
-
"timing", //time interval (Week or Day)
47
-
"quantity",
48
-
"expectedQuantity",
49
-
"constraint", //greater or less
50
-
"nutrient",
51
-
"consequence",
52
-
"alternative"
43
+
The object passed to RosaeNLG (after the graph has been processed) is an explanation. The following is the object generated from graphs/exp_graph_1_en.csv:
To print a field, you write ```!{explanation.field}```. For example, if nutrient is "vitamin d":
79
+
To print a field, you write ```#[+value(explanation.field)]``` or ```!{explanation.field}```. For example, the value of explanation.entity.enLabel is "red meat":
55
80
```
56
-
|This dish contains !{explanation.entity}.
81
+
|You ate #[+value(explanation.entity.enLabel)].
57
82
```
58
-
will output: ```This dish contains vitamin d.```
83
+
will output: ```You ate red meat.```
84
+
85
+
```#[+value(explanation.field)]``` will throw an error if the field is undefined while ```!{explanation.field}``` will print an empty string.
59
86
60
87
## Conditionals
61
-
If-else statements are used like in the following example where _entity=meat_ and _constraint=less_:
88
+
If-else statements are used like in the following example where _entity.enLabel=meat_ and _constraint=less_:
62
89
```
63
90
if explanation.constraint == 'greater'
64
91
|You ate too much,
65
92
|enough
66
93
else
67
94
|You did not eat enough,
68
95
|more
69
-
|!{explanation.entity}!
96
+
|#[+value(explanation.entity.enLabel)]!
70
97
```
71
98
will output: ```You ate too much, enough meat!```
72
-
Note that to use an explanation field in an if-else statement you must not use the ```!{}``` notation.
99
+
Note that to use an explanation field in an if-else statement you must not use the ```#[+value()]``` or the ```!{}``` notation.
100
+
101
+
## Javascript code and Functions
102
+
You can write javascript code in a template by starting the line with the "```-```" character,for example here i declare two variables and then I use them in the template:
103
+
```
104
+
- let nutrient = "cheese"
105
+
- let food = "pie"
73
106
74
-
## Functions
75
-
You can also use javascript functions in a template. With this program you can use ```pluralize.isSingular()``` and ```pluralize.isPlural()``` in a template to check if a word is either singular or plural. Example:
107
+
|the !{food} contains a lot of !{nutrient}
108
+
```
109
+
This will output ```The pie contains a lot of cheese```
110
+
### Pluralize
111
+
You can use ```pluralize.isSingular()``` and ```pluralize.isPlural()``` in a template to check if a word is either singular or plural. Example:
76
112
```
77
113
if pluralize.isSingular(explanation.entity)
78
114
|contains
@@ -81,6 +117,13 @@ else
81
117
```
82
118
This will output ```contains``` if the entity field is singular or ```contain``` if the entity field is plural.
83
119
120
+
### Random
121
+
The random function will return a random element of an array (if the argument is an array) or the argument if it is not an array.
122
+
```
123
+
- let nutrient = random(explanation.entity.negative)
124
+
- let consequence = random(nutrient.cons_en)
125
+
```
126
+
84
127
You can also add your own functions to pug (this is explained bellow in the RosaeNLG tutorial)
85
128
86
129
For other advanced features in Pug you can check the [Pug main website](https://pugjs.org/).
0 commit comments