Last active
August 17, 2018 09:41
-
-
Save dispeakble/5620beec176ca75cf31978329c279c2c to your computer and use it in GitHub Desktop.
ai parser.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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