Skip to content

Instantly share code, notes, and snippets.

@bradvogel
Last active April 8, 2018 01:54
Show Gist options
  • Save bradvogel/9759b3fe50828ad6bae8 to your computer and use it in GitHub Desktop.
Save bradvogel/9759b3fe50828ad6bae8 to your computer and use it in GitHub Desktop.
Lacona (natural language parser) basic example using ES5
var lacona = require('lacona');
var laconaPhrase = require('lacona-phrase');
var datetime = require('lacona-phrase-datetime');
var string = require('lacona-phrase-string');
var parser = new lacona.Parser({
grammar: laconaPhrase.createElement('sequence', {
children: [
laconaPhrase.createElement('literal', {
text: 'remind me to '
}),
laconaPhrase.createElement(string.String, {
id: 'activity'
}),
laconaPhrase.createElement('literal', {
text: ' '
}),
laconaPhrase.createElement('choice', {
id: 'date',
children: [
laconaPhrase.createElement(datetime.DateTime, {
past: false
}),
laconaPhrase.createElement(datetime.Range, {
past: false
})
]
})
]
})
});
var _ = require('underscore');
function parse(text) {
var results = parser.parseArray(text);
var topResult = _.first(_.sortBy(results, 'score').reverse());
return topResult ? topResult.result : null;
}
console.log(parse('remind me to exercise well 1pm to 2pm'))
console.log(parse('remind me to exercise well tomorrow 6pm'))
{
"name": "lacona_sample",
"version": "1.0.0",
"description": "sample",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-runtime": "^6.3.19",
"lacona": "^0.37.0",
"lacona-phrase": "^0.9.1",
"lacona-phrase-datetime": "^0.7.1",
"lacona-phrase-string": "^0.4.0",
"underscore": "^1.8.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment