-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbracery-expand.js
50 lines (42 loc) · 1.56 KB
/
bracery-expand.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* This is a small AWS lambda function for expanding Bracery in DynamoDB.
*/
//console.log('Loading function');
// const config = require('./bracery-config');
const util = require('./bracery-util');
global.nlp = require('./compromise.es6.min'); // hack/workaround so Bracery can see nlp. Not very satisfactory.
const Bracery = require('./bracery').Bracery;
const rita = require('./rita-tiny');
// The Lambda function
exports.handler = async (event, context, callback) => {
//console.log('Received event:', JSON.stringify(event, null, 2));
// Get initial vars as query or body parameters, if supplied
const vars = util.getVars (event);
// Set up some returns
const respond = util.respond (callback, event);
// Set up Bracery
let bracery = new Bracery (null, { rita });
let braceryConfig = util.braceryExpandConfig (bracery, vars, util.dynamoPromise());
// GET (single symbol) or POST (arbitrary Bracery)?
let expansion = null;
switch (event.httpMethod) {
case 'GET':
// Get symbol name
const name = event.pathParameters.name;
// Call expandSymbol
expansion = await braceryConfig.expandFull ({ symbolName: name });
break;
case 'POST':
// Get Bracery to expand
const braceryText = util.getBody(event).bracery;
// Call expand
expansion = await bracery.expand ({ rhsText: braceryText },
braceryConfig);
break;
default:
return respond.badMethod();
}
// And return
delete expansion.tree // too big for the wire
delete expansion.value // redundant
respond.ok(expansion);
};