-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
159 lines (139 loc) · 3.61 KB
/
app.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const { Wit, log, interactive } = require('node-wit');
const { prompt } = require('inquirer');
const fetch = require('node-fetch');
const { nextAction } = require('./intent');
const _ = require('lodash');
const accessToken = 'TY2XCMBJOCHWC3JQPHD7M4YRUTC5FSJN';
const client = new Wit({
accessToken
});
const headers = {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json'
};
const message = [
{
type : 'input',
name : 'message',
message : 'Enter Message: '
}
];
const correction = [
{
type : 'confirm',
name : 'confirm',
message : 'Is that correct?'
}
];
const newIntent = [
{
type : 'confirm',
name : 'confirm',
message : 'Do you want to train a new intent?'
}
];
const addIntent = [
{
type : 'input',
name : 'intent',
message : 'Enter Intent Name: '
}
];
let context = {};
query();
function extractEntities(entities) {
return _.mapValues(entities, e => e[0].value);
}
function query () {
let question = message;
if (context.next)
question = [
{
type: 'input',
name: 'message',
message: context.next.text
}
];
return prompt(question).then(answers =>
client.message(answers.message, {}, false, true).then(data => {
const entities = data.entities;
let next = false;
if (entities.intent) {
console.log(`Recognized intent: "${entities.intent[0].value}" Confidence: ${entities.intent[0].confidence}`);
next = nextAction({intent: entities.intent[0].value, context, entities: extractEntities(entities)});
} else
console.log(`No intent recognized :(`);
let other = Object.keys(entities);
other = other.filter(e => !(e === 'intent'));
if (other.length > 0) {
console.log(`Found other entities: `);
for (let e of other) {
console.log(`\t${e}:`);
for (let v of entities[e])
console.log(`\t\t"${v.value}" Confidence: ${v.confidence}`);
}
} else
console.log("No other entities found");
if (next) {
console.log(`Next Action: ask_${next.name}`);
context.next = next;
} else
context = {};
return isCorrect(answers.message, entities);
})
.catch(console.error)
);
}
function isCorrect(text, entities) {
return prompt(correction).then(a => {
if (a.confirm) {
entities = _.map(entities, (e, k) => {
const selected = e[0];
const entity = {
entity: k,
value: selected.value
};
if (selected._start) {
_.set(entity, 'start', selected._start);
_.set(entity, 'end', selected._end);
}
return entity;
});
return witTrainRequest({text, entities});
}
console.log("OK... :(");
return prompt(newIntent).then(a => {
if (a.confirm) {
return prompt(addIntent).then(a => {
entities = [
{
entity: 'intent',
value: a.intent
}
];
return witTrainRequest({text, entities});
});
}
query();
});
});
}
function witTrainRequest ({ text, entities }) {
const body = JSON.stringify([{ text, entities }]);
return fetch("https://api.wit.ai/samples?v=20170307", {
method: 'POST',
headers,
body
})
.then(rsp => rsp.json()).then(json => {
if (json.sent) {
console.log("The app is now training the new record...");
query();
}
if (json.error) {
console.log("Wit training failed...");
console.log(json.error);
}
})
.catch(err => console.log(`Something bad happened... ${err}`));
}