Skip to content

Commit

Permalink
Read any graph
Browse files Browse the repository at this point in the history
  • Loading branch information
radu1690 committed Jun 14, 2021
1 parent 1dd7208 commit d11b82c
Show file tree
Hide file tree
Showing 30 changed files with 436 additions and 4,335 deletions.
85 changes: 64 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ You can run the program with:
```
node explanations.js <template> <graph>
```
Template is the location of the pug template while graph is a graph in json format. Example:
Template is the location of the pug template while graph is a graph in csv format (with "," as delimiter). Example:
```
node explanations.js templates/english/argument.pug graphs/explanationGraph.json
node explanations.js templates/english/argument.pug graphs/exp_graph_1_en.csv
```

You can specify the language, English or Italian (the default is English), like in the following example:
```
node explanations.js templates/italian/feedback.pug graphs/explanationGraph.json italian
node explanations.js templates/italian/feedback.pug graphs/exp_graph_1_it.csv italian
```

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

## Variables
The object passed to RosaeNLG (after the graph has been processed) is an explanation and has the following fields:
```
"entity", //name of the food
"timing", //time interval (Week or Day)
"quantity",
"expectedQuantity",
"constraint", //greater or less
"nutrient",
"consequence",
"alternative"
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:
```
{
timestamp: '1541622487915',
startTime: '1541440506117',
endTime: '1541622487915',
quantity: '3',
expectedQuantity: '2',
priority: '1',
level: '3',
history: '1',
explanationId: 'explanation_1yfzqe9kjrdmm0sc349qj3x7loeal6tzw7amlhhphpl5z78051_mr-diet-018-nweek_1541622487915',
user: '1yfzqe9kjrdmm0sc349qj3x7loeal6tzw7amlhhphpl5z78051',
ruleId: 'mr-diet-018-nweek',
entityType: 'foodcategory',
entity: {
enLabel: 'red meat',
negative: [
{ enLabel: 'animal protein',
cons_en: [ 'intestinal inflammation', 'the increment of cancer risk' ]
},
{ enLabel: 'animal lipids',
cons_en: [
'increment of cardiovascular disease risk',
'increment of cholesterol in the blood'
]
}
],
alternatives: [ { enLabel: 'processed legumes' }, { enLabel: 'fish' } ]
},
timing: 'week',
meals: [ 'meal-meal-1541440506117', 'meal-meal-1541440506118' ],
goals: 'sp-goal-d-114',
constraint: 'less'
}
```
To print a field, you write ```!{explanation.field}```. For example, if nutrient is "vitamin d":
To print a field, you write ```#[+value(explanation.field)]``` or ```!{explanation.field}```. For example, the value of explanation.entity.enLabel is "red meat":
```
|This dish contains !{explanation.entity}.
|You ate #[+value(explanation.entity.enLabel)].
```
will output: ```This dish contains vitamin d.```
will output: ```You ate red meat.```

```#[+value(explanation.field)]``` will throw an error if the field is undefined while ```!{explanation.field}``` will print an empty string.

## Conditionals
If-else statements are used like in the following example where _entity=meat_ and _constraint=less_:
If-else statements are used like in the following example where _entity.enLabel=meat_ and _constraint=less_:
```
if explanation.constraint == 'greater'
|You ate too much,
|enough
else
|You did not eat enough,
|more
|!{explanation.entity}!
|#[+value(explanation.entity.enLabel)]!
```
will output: ```You ate too much, enough meat!```
Note that to use an explanation field in an if-else statement you must not use the ```!{}``` notation.
Note that to use an explanation field in an if-else statement you must not use the ```#[+value()]``` or the ```!{}``` notation.

## Javascript code and Functions
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:
```
- let nutrient = "cheese"
- let food = "pie"
## Functions
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:
|the !{food} contains a lot of !{nutrient}
```
This will output ```The pie contains a lot of cheese```
### Pluralize
You can use ```pluralize.isSingular()``` and ```pluralize.isPlural()``` in a template to check if a word is either singular or plural. Example:
```
if pluralize.isSingular(explanation.entity)
|contains
Expand All @@ -81,6 +117,13 @@ else
```
This will output ```contains``` if the entity field is singular or ```contain``` if the entity field is plural.

### Random
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.
```
- let nutrient = random(explanation.entity.negative)
- let consequence = random(nutrient.cons_en)
```

You can also add your own functions to pug (this is explained bellow in the RosaeNLG tutorial)

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

0 comments on commit d11b82c

Please sign in to comment.