1 | 'use strict';
|
2 | const _ = require('lodash');
|
3 |
|
4 | module.exports = (app) => {
|
5 | let assets = {
|
6 | intentSchema: genIntentSchema(app.intents),
|
7 | utterances: genUtterances(app.intents),
|
8 | customSlots: genCustomSlots(app.customSlots)
|
9 | };
|
10 |
|
11 | assets.toString = createStringifyAssets(assets);
|
12 |
|
13 | return assets;
|
14 | };
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | const createStringifyAssets = (assets) => () => {
|
24 | let customSlotsString = _.map(assets.customSlots, (samples, name) => {
|
25 | return `${name}:\n${samples.join('\n')}\n`;
|
26 | }).join('\n');
|
27 |
|
28 | return createAsset('intentSchema', assets.intentSchema) +
|
29 | createAsset('utterances', assets.utterances) +
|
30 | createAsset('customSlots', customSlotsString);
|
31 | };
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | const createAsset = (type, data) => {
|
40 | return `${type}:\n${data}\n\n`;
|
41 | };
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | const genIntentSchema = (intents) => {
|
48 | let intentSchema = {
|
49 | intents: []
|
50 | };
|
51 |
|
52 | _.forOwn(intents, intent => {
|
53 | let currentSchema = {
|
54 | intent: intent.name
|
55 | };
|
56 |
|
57 |
|
58 | if (intent.slots && intent.slots.length > 0) {
|
59 | currentSchema.slots = intent.slots;
|
60 | }
|
61 |
|
62 | intentSchema.intents.push(currentSchema);
|
63 | });
|
64 |
|
65 | return JSON.stringify(intentSchema, null, 2);
|
66 | };
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | const genUtterances = (intents) => {
|
73 | let sampleUtterances = [];
|
74 |
|
75 | _.forOwn(intents, intent => {
|
76 | intent.utterances.forEach(utterance => {
|
77 | if (utterance) {
|
78 | sampleUtterances.push(`${intent.name} ${utterance}`);
|
79 | }
|
80 | });
|
81 | });
|
82 |
|
83 | return sampleUtterances.join('\n');
|
84 | };
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | const genCustomSlots = (customSlots) => {
|
91 | let allCustomSlotSamples = {};
|
92 |
|
93 | _.forOwn(customSlots, (customSlot) => {
|
94 | allCustomSlotSamples[customSlot.name] = customSlot.samples;
|
95 | });
|
96 |
|
97 | return allCustomSlotSamples;
|
98 | };
|