UNPKG

2.51 kBJavaScriptView Raw
1'use strict';
2const _ = require('lodash');
3
4module.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 * @param {object} assets
18 * @param {object} assets.intentSchema
19 * @param {object} assets.utterances
20 * @param {object} assets.customSlots
21 * @returns {function} returning stringified version of speech assetsuseful for printing in terminal
22 */
23const 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 * Creates stringified part of speechAssets
35 * @param {string} type
36 * @param {object} data
37 * @returns {string}
38 */
39const createAsset = (type, data) => {
40 return `${type}:\n${data}\n\n`;
41};
42
43/**
44 * Generates intent schema JSON string
45 * @return {string} strigified intent schema object generated from intents
46 */
47const genIntentSchema = (intents) => {
48 let intentSchema = {
49 intents: []
50 };
51
52 _.forOwn(intents, intent => {
53 let currentSchema = {
54 intent: intent.name
55 };
56
57 // Property slots is optional
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 * Generates sample utterances tied to intent name
70 * @return {string} interpretation of all sample utterances
71 */
72const 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 * @return {object} where key = slot type and value is string interpretation of
88 * custom slot type samples
89 */
90const genCustomSlots = (customSlots) => {
91 let allCustomSlotSamples = {};
92
93 _.forOwn(customSlots, (customSlot) => {
94 allCustomSlotSamples[customSlot.name] = customSlot.samples;
95 });
96
97 return allCustomSlotSamples;
98};