Skip to content

Commit

Permalink
Updated readme, fixed pug templates
Browse files Browse the repository at this point in the history
  • Loading branch information
radu1690 committed Jun 25, 2021
1 parent c8d485c commit e92fc99
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 62 deletions.
56 changes: 32 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ You can do loop with the ```eachz``` operator from RosaeNLG:
```
-let data = ["fruits", "vegetables", "meat"]
eachz element in data
|!{data}
|!{data}
```
will output: ```fruits vegetables meat```

Expand All @@ -117,13 +117,15 @@ The ```eachz``` operator can accept some [parameters](https://rosaenlg.org/rosae
-let data = ["fruits", "vegetables", "meat"]
|I bought
eachz element in data with {separator: ",", last_separator: "and", end: "."}
|!{element}
|!{element}
```
will output: ```I bought fruits, vegetables and meat.```

## Functions
You can use javascript functions directly in templates. You can either use libraries or create you own functions.
We added Pluralize and Random functions.
### Pluralize
You can use ```pluralize.isSingular()``` and ```pluralize.isPlural()``` in a template to check if a word is either singular or plural. Example:
[Pluralize](https://www.npmjs.com/package/pluralize) is a library which can pluralize and singularize any given word. Use ```pluralize(<word>)``` or ```pluralize.singular(<word>)``` in a template to either pluralize or singularize a word. Use ```pluralize.isSingular()``` and ```pluralize.isPlural()``` to check if a word is either singular or plural. Example:
```
if pluralize.isSingular(explanation.entity)
|contains
Expand All @@ -133,32 +135,38 @@ 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.
Random is a custom function which returns 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)
```

## Functions customization
You can also add your own javascript functions to use in pug templates.
Here I create a function in my JS file which will print the input text two times and then I pass it to RosaeNLG:
### JS file
## How to add a function
Here is the example of how I added the Random function.
First you declare your function in explanations.js:
```
var rosae = require('rosaenlg');
function customFunction(input){
text = input + " " + input
return text
function randomElement(items){
if(Array.isArray(items)){
return items[Math.floor(Math.random()*items.length)]
}else{
return items
}
}
console.log( rosae.renderFile('testNLG.pug', {
language: 'en_US',
double : customFunction
}) );
```
### PUG template testNLG.pug
```
-let data = ['apples', 'bananas', 'apricots']
|!{double(data[0])}
```
This will output: ```Apples apples```.
Then you pass it to RosaeNLG in graph2Object.main function (again in Explanations.js):
```
graph2Object.main(dir_explanationGraph)
.then(data =>{
var output = rosaenlgPug.renderFile(dir_template, {
language: rosae_language,
explanation: data,
pluralize: pluralize, //pluralize function
random: randomElement //random function
});
console.log(output)
})
```
Now you can call the function from your pug template:
```
- let nutrient = random(explanation.entity.negative)
```
34 changes: 7 additions & 27 deletions explanations.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,21 @@ var rosaenlgPug = require('rosaenlg');
var pluralize = require('pluralize')
var graph2Object = require("./graphToObject")

var language// = 'en'; //en or it
var conseq_language// = 'cons_en' //cons_en or cons_it
var rosae_language// = 'en_US' //en_US or it_IT
var rosae_language//en_US or it_IT

function random(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min
}

function randomElement(items){
if(Array.isArray(items)){
return items[Math.floor(Math.random()*items.length)]
}else{
return items
}

}

function setLanguage(lang){
language = 'en';
conseq_language = 'cons_en'
rosae_language = 'en_US'
if(lang!=null){
if (!lang.localeCompare('italian')){
language = 'it';
conseq_language = 'cons_it'
rosae_language = 'it_IT'
}else if(lang.localeCompare('english')){
console.log('Language ' + lang + ' is not supported')
Expand All @@ -40,19 +30,9 @@ function main(){
const args = process.argv;
//console.log(args);

var dir_template = args[2];
var dir_explanationGraph = args[3];
var dir_template = args[2]; //pug template
var dir_explanationGraph = args[3]; //csv graph

const fs = require('fs');

/* try{
var template = fs.readFileSync(dir_template);
}catch(e){
console.log("Template file not found ("+dir_template)+")";
return;
} */


//console.log(explanationGraph);
//console.log(template.toString());
setLanguage(args[4])
Expand All @@ -61,16 +41,16 @@ function main(){

graph2Object.main(dir_explanationGraph)
.then(data =>{
//console.log(data)
//console.log(data.entity)
//console.log(data.entity.negative)
//console.log(data.entity.alternatives)
var output = rosaenlgPug.renderFile(dir_template, {
language: rosae_language,
explanation: data,
pluralize: pluralize,
random: randomElement
});
//console.log(data)
//console.log(data.entity)
//console.log(data.entity.negative)
//console.log(data.entity.alternatives)
console.log(output)
})
.catch(err => console.error(err))
Expand Down
7 changes: 0 additions & 7 deletions graphToObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ function graphToObject(graph){
}
var nodes = graphlib.alg.postorder(graph, graphName)
//console.log(nodes)
/* var json = {}
var f = false */
for (n in nodes){
//console.log(graph.node(nodes[n]))
//console.log(graph.successors(nodes[n]))
Expand All @@ -41,10 +39,6 @@ function graphToObject(graph){
//console.log(graph.node(nodes[n]))
//this is object
var children = graph.successors(nodes[n])
/* if(f){
console.log(children)
f=false;
} */
var obj = {}
for (child in children){
//console.log(graph.node(children[child]))
Expand Down Expand Up @@ -99,7 +93,6 @@ function main(csvPath){
graph.setNode(d[0], new Label(d[0], d[0]))
}

//to
//avoid identical numerical nodes and cycles
if(isNaN(d[2]) && d[0]!=d[2]){
graph.setNode(d[2], new Label(d[1], d[2]))
Expand Down
2 changes: 1 addition & 1 deletion graphs/exp_graph_1_it.csv
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ AnimalProtein cons_it all'aumento di problemi artritici
AnimalProtein cons_it all'aumento del rischio di malattie tumorali
RedMeat alternatives LegumesAndLegumeProducts
RedMeat alternatives Fish
Fish itLabel Pesce
Fish itLabel Pesce
LegumesAndLegumeProducts itLabel Legumi Confezionati e Lavorati
explanation timing Week
explanation quantity 3
Expand Down
2 changes: 1 addition & 1 deletion templates/english/feedback.pug
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ if explanation.constraint == 'greater'
if explanation.constraint == 'less'
|of maximum
|#[+value(explanation.expectedQuantity)]
|)
|).
1 change: 1 addition & 0 deletions templates/italian/argument.pug
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
|contiente/contengono
|#[+value(nutrient.itLabel)]
|che può/possono portare
|#[+value(consequence)]
|!
2 changes: 1 addition & 1 deletion templates/italian/feedback.pug
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ if explanation.constraint == 'greater'
if explanation.constraint == 'less'
|su un massimo di
|#[+value(explanation.expectedQuantity)]
|)
|).
2 changes: 1 addition & 1 deletion templates/italian/suggestion.pug
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
- let alternative = random(explanation.entity.alternatives)
|La prossima volta
if alternative == 'null'
if alternative == null
|cerca di fare piu attenzione e ricorda di mangiare in modo sano!
else
|prova ad alternare con #[+value(alternative.itLabel)]
Expand Down

0 comments on commit e92fc99

Please sign in to comment.