diff --git a/README.md b/README.md index 1da2e5b..8c5ec53 100644 --- a/README.md +++ b/README.md @@ -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``` @@ -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()``` or ```pluralize.singular()``` 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 @@ -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) +``` \ No newline at end of file diff --git a/explanations.js b/explanations.js index dc0097c..0bb2054 100644 --- a/explanations.js +++ b/explanations.js @@ -2,13 +2,8 @@ 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)){ @@ -16,17 +11,12 @@ function randomElement(items){ }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') @@ -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]) @@ -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)) diff --git a/graphToObject.js b/graphToObject.js index 94f9159..a0f7104 100644 --- a/graphToObject.js +++ b/graphToObject.js @@ -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])) @@ -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])) @@ -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])) diff --git a/graphs/exp_graph_1_it.csv b/graphs/exp_graph_1_it.csv index c37905e..56e43fc 100644 --- a/graphs/exp_graph_1_it.csv +++ b/graphs/exp_graph_1_it.csv @@ -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 diff --git a/templates/english/feedback.pug b/templates/english/feedback.pug index d3dbc7f..5c5591a 100644 --- a/templates/english/feedback.pug +++ b/templates/english/feedback.pug @@ -14,4 +14,4 @@ if explanation.constraint == 'greater' if explanation.constraint == 'less' |of maximum |#[+value(explanation.expectedQuantity)] -|) +|). diff --git a/templates/italian/argument.pug b/templates/italian/argument.pug index fa4e1d0..bdc2ab7 100644 --- a/templates/italian/argument.pug +++ b/templates/italian/argument.pug @@ -5,4 +5,5 @@ |contiente/contengono |#[+value(nutrient.itLabel)] |che può/possono portare +|#[+value(consequence)] |! diff --git a/templates/italian/feedback.pug b/templates/italian/feedback.pug index 4b5304f..f759a55 100644 --- a/templates/italian/feedback.pug +++ b/templates/italian/feedback.pug @@ -14,4 +14,4 @@ if explanation.constraint == 'greater' if explanation.constraint == 'less' |su un massimo di |#[+value(explanation.expectedQuantity)] -|) +|). diff --git a/templates/italian/suggestion.pug b/templates/italian/suggestion.pug index 496135b..c51c13c 100644 --- a/templates/italian/suggestion.pug +++ b/templates/italian/suggestion.pug @@ -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)]