-
Notifications
You must be signed in to change notification settings - Fork 0
/
3-n-best.js
40 lines (37 loc) · 1.11 KB
/
3-n-best.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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*/
const {
FIREBASE_CONFIG,
queryWit,
interactive,
firstEntity,
} = require('../shared');
const firebase = require('firebase');
const N = 3;
const THRESHOLD = 0.7;
function handleMessage(question, readline) {
return queryWit(question, N).then(({entities}) => {
const intents = entities['intent'] || [];
const bestIntent = intents[0];
const dateTime = firstEntity(entities, 'datetime') || {};
if (!bestIntent || bestIntent.confidence < THRESHOLD) {
console.log('🤖 what would you like to do?');
intents.forEach(intent => console.log(`\n -- ${intent.value}`));
readline.question('choice > ', choice => {
console.log(`🤖 okay, running > ${choice}`);
});
return;
}
return firebase
.database()
.ref(`answers/${bestIntent.value}`)
.once('value')
.then(snapshot => {
const val = snapshot.val();
console.log(`🤖 ${val || bestIntent.value}`, dateTime);
});
});
}
firebase.initializeApp(FIREBASE_CONFIG);
interactive(handleMessage);