Skip to content

Instantly share code, notes, and snippets.

@dispeakble
Last active August 17, 2018 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dispeakble/5620beec176ca75cf31978329c279c2c to your computer and use it in GitHub Desktop.
Save dispeakble/5620beec176ca75cf31978329c279c2c to your computer and use it in GitHub Desktop.
ai parser.js
var parser = {
objects: {
},
api: {
parse: function (params) {
return new Promise(function (resolve_parse) {
var response = {
what:params.data.what,
hash:params.hash
};
switch (params.data.what) {
case 'letter':
db.api.query({
action: 'get',
what: 'letter',
add: false,
data: {id: params.data.id}
}).then(function (letter_response) {
var PythonShell = require('python-shell');
var options = {
mode: 'json',
pythonOptions: ['-u'],
scriptPath: './api/pyparsing',
args: [JSON.stringify({'data': letter_response.data[0].value, 'hash': params.hash})]
};
try {
PythonShell.run('main.py', options, function (err, results) {
if (err) throw err;
// the result = messages collected during execution
db.api.query({
action: 'get',
what: 'nlp',
add: true,
data: {
value: results[0].data
}
}).then(function (nlp) {
response.data = [{id: nlp.data[0].id}];
response.what = 'nlp';
resolve_parse(response);
});
});
} catch (e) {
response.hash = params.hash;
response.data = e;
response.what = 'error';
resolve_parse(response);
console.log(e);
}
});
break;
default:
resolve_parse(response);
break;
}
});
}
}
};
module.exports = parser;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment