Repository navigation
Expand file tree
/
Copy pathhelpers.js
More file actions
357 lines (276 loc) · 11.4 KB
/
Copy pathhelpers.js
File metadata and controls
357 lines (276 loc) · 11.4 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
const constants = require('./constants.js');
const AWS = constants.AWS;
const DYNAMODB_TABLE = constants.DYNAMODB_TABLE;
const model = require('./model.json'); // a static copy of your model, used to suggest custom slot values
module.exports = {
'randomArrayElement': function(myArray) {
return(myArray[Math.floor(Math.random() * myArray.length)]);
},
'capitalize': function(myString) {
return myString.replace(/(?:^|\s)\S/g, function(a) { return a.toUpperCase(); });
},
'supportsDisplay': function(handlerInput) // returns true if the skill is running on a device with a display (Echo Show, Echo Spot, etc.)
{ // Enable your skill for display as shown here: https://alexa.design/enabledisplay
const hasDisplay =
handlerInput.requestEnvelope.context &&
handlerInput.requestEnvelope.context.System &&
handlerInput.requestEnvelope.context.System.device &&
handlerInput.requestEnvelope.context.System.device.supportedInterfaces &&
handlerInput.requestEnvelope.context.System.device.supportedInterfaces.Display;
return hasDisplay;
},
'timeDelta': function(t1, t2) {
const dt1 = new Date(t1);
const dt2 = new Date(t2);
const timeSpanMS = dt2.getTime() - dt1.getTime();
const span = {
"timeSpanMIN": Math.floor(timeSpanMS / (1000 * 60 )),
"timeSpanHR": Math.floor(timeSpanMS / (1000 * 60 * 60)),
"timeSpanDAY": Math.floor(timeSpanMS / (1000 * 60 * 60 * 24)),
"timeSpanDesc" : ""
};
if (span.timeSpanHR < 2) {
span.timeSpanDesc = span.timeSpanMIN + " minutes";
} else if (span.timeSpanDAY < 2) {
span.timeSpanDesc = span.timeSpanHR + " hours";
} else {
span.timeSpanDesc = span.timeSpanDAY + " days";
}
return span;
},
'sayArray': function(myData, penultimateWord = 'and') {
// the first argument is an array [] of items
// the second argument is the list penultimate word; and/or/nor etc. Default to 'and'
let result = '';
myData.forEach(function(element, index, arr) {
if (index === 0) {
result = element;
} else if (index === myData.length - 1) {
result += ` ${penultimateWord} ${element}`;
} else {
result += `, ${element}`;
}
});
return result;
},
'stripTags': function(str) {
return str.replace(/<\/?[^>]+(>|$)/g, "");
},
'getSlotValues': function(filledSlots) {
const slotValues = {};
Object.keys(filledSlots).forEach((item) => {
const name = filledSlots[item].name;
if (filledSlots[item] &&
filledSlots[item].resolutions &&
filledSlots[item].resolutions.resolutionsPerAuthority[0] &&
filledSlots[item].resolutions.resolutionsPerAuthority[0].status &&
filledSlots[item].resolutions.resolutionsPerAuthority[0].status.code) {
switch (filledSlots[item].resolutions.resolutionsPerAuthority[0].status.code) {
case 'ER_SUCCESS_MATCH':
slotValues[name] = {
heardAs: filledSlots[item].value,
resolved: filledSlots[item].resolutions.resolutionsPerAuthority[0].values[0].value.name,
ERstatus: 'ER_SUCCESS_MATCH'
};
break;
case 'ER_SUCCESS_NO_MATCH':
slotValues[name] = {
heardAs: filledSlots[item].value,
resolved: '',
ERstatus: 'ER_SUCCESS_NO_MATCH'
};
break;
default:
break;
}
} else {
slotValues[name] = {
heardAs: filledSlots[item].value,
resolved: '',
ERstatus: ''
};
}
}, this);
return slotValues;
},
'getExampleSlotValues': function(intentName, slotName) {
let examples = [];
let slotType = '';
let slotValuesFull = [];
let intents = model.interactionModel.languageModel.intents;
for (let i = 0; i < intents.length; i++) {
if (intents[i].name == intentName) {
let slots = intents[i].slots;
for (let j = 0; j < slots.length; j++) {
if (slots[j].name === slotName) {
slotType = slots[j].type;
}
}
}
}
let types = model.interactionModel.languageModel.types;
for (let i = 0; i < types.length; i++) {
if (types[i].name === slotType) {
slotValuesFull = types[i].values;
}
}
slotValuesFull = module.exports.shuffleArray(slotValuesFull);
examples.push(slotValuesFull[0].name.value);
examples.push(slotValuesFull[1].name.value);
if (slotValuesFull.length > 2) {
examples.push(slotValuesFull[2].name.value);
}
return examples;
},
'getRecordCount': function(callback) {
const params = {
TableName: DYNAMODB_TABLE
};
let docClient = new AWS.DynamoDB.DocumentClient();
docClient.scan(params, (err, data) => {
if (err) {
console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
} else {
const skillUserCount = data.Items.length;
callback(skillUserCount);
}
});
},
'changeProsody' : function(attribute, current, change) {
let newValue = '';
if (attribute === 'rate') {
switch(current + '.' + change) {
case 'x-slow.slower':
case 'slow.slower':
newValue = 'x-slow';
break;
case 'medium.slower':
case 'x-slow.faster':
newValue = 'slow';
break;
case 'fast.slower':
case 'slow.faster':
newValue = 'medium';
break;
case 'x-fast.slower':
case 'medium.faster':
newValue = 'fast';
break;
case 'x-fast.faster':
case 'fast.faster':
newValue = 'x-fast';
break;
default:
newValue = 'medium';
}
}
return newValue;
},
'sendTxtMessage': function(params, locale, callback) {
let mobileNumber = params.PhoneNumber.toString();
if (locale === 'en-US') {
if (mobileNumber.length < 10 ){
const errMsg = 'mobileNumber provided is too short: ' + mobileNumber + '. ';
callback(errMsg);
}
if (mobileNumber.length == 10 ) {
mobileNumber = '1' + mobileNumber;
}
} else {
if (locale === 'other locales tbd') {
// add validation and format code
}
}
if (mobileNumber.substring(0,1) !== '+') {
mobileNumber = '+' + mobileNumber;
}
let snsParams = params;
snsParams.PhoneNumber = mobileNumber;
const SNS = new AWS.SNS();
SNS.publish(snsParams, function(err, data){
// console.log('sending message to ' + mobileNumber );
if (err) console.log(err, err.stack);
callback('I sent you a text message. ');
});
},
'generatePassPhrase': function() {
// 'correct', 'horse', 'battery', 'staple'
const word1 = ['nice', 'good', 'clear', 'kind', 'red', 'green', 'orange', 'yellow', 'brown', 'careful',
'powerful', 'vast', 'happy', 'deep', 'warm', 'cold', 'heavy', 'dry', 'quiet', 'sweet',
'short', 'long', 'late', 'early', 'quick', 'fast', 'slow', 'other','public','clean','proud',
'flat','round', 'loud', 'funny', 'free', 'tall', 'short', 'big', 'small'];
const word2 = ['person', 'day', 'car', 'tree', 'fish', 'wheel', 'chair', 'sun', 'moon', 'star',
'story', 'voice', 'job', 'fact', 'record', 'computer', 'ocean', 'building', 'cat', 'dog', 'rabbit',
'carrot', 'orange', 'bread', 'soup', 'spoon', 'fork', 'straw', 'napkin', 'fold', 'pillow', 'radio',
'towel', 'pencil', 'table', 'mark', 'teacher', 'student', 'developer', 'raisin', 'pizza', 'movie',
'book', 'cup', 'plate', 'wall', 'door', 'window', 'shoes', 'hat', 'shirt', 'bag', 'page', 'clock',
'glass', 'button', 'bump', 'paint', 'song', 'story', 'memory', 'school', 'corner', 'wire', 'cable'
];
const numLimit = 999;
const phraseObject = {
'word1': randomArrayElement(word1),
'word2': randomArrayElement(word2),
'number': Math.floor(Math.random() * numLimit)
};
return phraseObject;
},
'shuffleArray': function(array) { // Fisher Yates shuffle!
let currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
},
'incrementArray': function(arr, element) {
for(let i = 0; i < arr.length; i++) {
if (arr[i].name === element) {
arr[i].value += 1;
return arr;
}
}
// no match, create new element
arr.push({'name':element, 'value': 1});
return arr;
},
'sortArray': function(arr) {
return arr.sort(function(a,b) {return (a.value > b.value) ? -1 : ((b.value > a.value) ? 1 : 0);} );
},
'rankArray': function(arr) { // assumes sorted array
let rank = 0;
let previousValue = 0;
let tiesAll = {};
let ties = [];
for(let i = 0; i < arr.length; i++) {
if (arr[i].value !== previousValue) {
rank += 1;
ties = [];
}
ties.push(arr[i].name);
arr[i].rank = rank;
arr[i].ties = ties;
previousValue = arr[i].value;
}
// list other elements tied at the same rank
for(let i = 0; i < arr.length; i++) {
let tiesCleaned = [];
for (let j = 0; j < arr[i].ties.length; j++) {
if (arr[i].ties[j] !== arr[i].name) {
tiesCleaned.push(arr[i].ties[j]);
}
}
arr[i].ties = tiesCleaned;
}
return arr;
}
};
// another way to define helpers: extend a native type with a new function
Array.prototype.diff = function(a) {
return this.filter(function(i) {return a.indexOf(i) < 0;});
};