Skip to content

Commit d11b82c

Browse files
committed
Read any graph
1 parent 1dd7208 commit d11b82c

30 files changed

+436
-4335
lines changed

README.md

+64-21
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ You can run the program with:
1313
```
1414
node explanations.js <template> <graph>
1515
```
16-
Template is the location of the pug template while graph is a graph in json format. Example:
16+
Template is the location of the pug template while graph is a graph in csv format (with "," as delimiter). Example:
1717
```
18-
node explanations.js templates/english/argument.pug graphs/explanationGraph.json
18+
node explanations.js templates/english/argument.pug graphs/exp_graph_1_en.csv
1919
```
2020

2121
You can specify the language, English or Italian (the default is English), like in the following example:
2222
```
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
2424
```
2525

2626
# Basic Tutorial for Templates
@@ -40,39 +40,75 @@ Spaces between different plain texts (except only punctuation) are automatically
4040
will still output: ```Hello world!```
4141

4242
## 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:
44+
```
45+
{
46+
timestamp: '1541622487915',
47+
startTime: '1541440506117',
48+
endTime: '1541622487915',
49+
quantity: '3',
50+
expectedQuantity: '2',
51+
priority: '1',
52+
level: '3',
53+
history: '1',
54+
explanationId: 'explanation_1yfzqe9kjrdmm0sc349qj3x7loeal6tzw7amlhhphpl5z78051_mr-diet-018-nweek_1541622487915',
55+
user: '1yfzqe9kjrdmm0sc349qj3x7loeal6tzw7amlhhphpl5z78051',
56+
ruleId: 'mr-diet-018-nweek',
57+
entityType: 'foodcategory',
58+
entity: {
59+
enLabel: 'red meat',
60+
negative: [
61+
{ enLabel: 'animal protein',
62+
cons_en: [ 'intestinal inflammation', 'the increment of cancer risk' ]
63+
},
64+
{ enLabel: 'animal lipids',
65+
cons_en: [
66+
'increment of cardiovascular disease risk',
67+
'increment of cholesterol in the blood'
68+
]
69+
}
70+
],
71+
alternatives: [ { enLabel: 'processed legumes' }, { enLabel: 'fish' } ]
72+
},
73+
timing: 'week',
74+
meals: [ 'meal-meal-1541440506117', 'meal-meal-1541440506118' ],
75+
goals: 'sp-goal-d-114',
76+
constraint: 'less'
77+
}
5378
```
54-
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":
5580
```
56-
|This dish contains !{explanation.entity}.
81+
|You ate #[+value(explanation.entity.enLabel)].
5782
```
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.
5986

6087
## 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_:
6289
```
6390
if explanation.constraint == 'greater'
6491
|You ate too much,
6592
|enough
6693
else
6794
|You did not eat enough,
6895
|more
69-
|!{explanation.entity}!
96+
|#[+value(explanation.entity.enLabel)]!
7097
```
7198
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"
73106
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:
76112
```
77113
if pluralize.isSingular(explanation.entity)
78114
|contains
@@ -81,6 +117,13 @@ else
81117
```
82118
This will output ```contains``` if the entity field is singular or ```contain``` if the entity field is plural.
83119

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+
84127
You can also add your own functions to pug (this is explained bellow in the RosaeNLG tutorial)
85128

86129
For other advanced features in Pug you can check the [Pug main website](https://pugjs.org/).

0 commit comments

Comments
 (0)