forked from mediarain/alexa-utterance-expander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.js
90 lines (79 loc) · 2.79 KB
/
convert.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
const flatten = require('array-flatten');
const { writeFile, readFileSync } = require('fs');
const UtteranceExpander = require('./UtteranceExpander.js');
const DEBUG = true;
const COMMAND = process.argv[2];
const INPUT_FILE = process.argv[3];
const OUTPUT_FILE = process.argv[4];
function invalid() {
console.log('convert.js [input | output] inputFile outputFile');
}
function getInputFile() {
return readFileSync(INPUT_FILE, 'utf8');
}
function getInputTypes(input) {
function merge(values) {
return flatten(values.map(v => {
let mergedValues = [v.value];
if (v.synonyms) {
mergedValues = mergedValues.concat(v.synonyms);
}
return mergedValues;
}));
}
let inputTypes = {};
input.inputTypes.forEach(type => inputTypes[type.name] = merge(type.values));
return inputTypes;
}
function getIntentInputs(input) {
let types = getInputTypes(input);
let intentInputs = {};
input.intents.forEach(intent => intent.inputs.forEach(intentInput => {
let intentInputType = typeof intentInput.type === 'string' ? intentInput.type : intentInput.type.alexa;
intentInputs[intentInput.name] = types[intentInputType];
}));
return intentInputs;
}
function getIntentPhrases(input) {
let intentPhrases = {};
input.intents.forEach(intent => intentPhrases[intent.name] = intent.phrases.map(phrase => phrase.replace(/\{/g, '[').replace(/\}/g, ']')));
return intentPhrases;
}
function formatExpander(inputValues, formatter) {
return Object.entries(inputValues)
.map(intentValue => intentValue[1].map(value => formatter(intentValue[0], value)).join('\n')).join('\n\n');
}
function cleanOutput(output) {
let INTENT_NAME_REGEX = /^(\w+) +([^\n]+)$/gm;
let cleanLines = [];
let lastIntentName = '';
let match;
while ((match = INTENT_NAME_REGEX.exec(output)) != null) {
let intentName = match[1];
if (lastIntentName !== intentName) {
lastIntentName = intentName;
cleanLines.push(`\n${intentName}\n`);
}
cleanLines.push(match[2]);
}
return cleanLines.join('\n').trim();
}
try {
if (OUTPUT_FILE !== undefined) {
if (COMMAND === 'input') {
let input = JSON.parse(getInputFile());
let intentInputs = formatExpander(getIntentInputs(input), (name, value) => `[${name}] ${value}`);
let intentPhrases = formatExpander(getIntentPhrases(input), (name, value) => `${name} ${value}`);
writeFile(OUTPUT_FILE, `${intentInputs}\n\n${intentPhrases}`, 'utf8', () => console.log(`Formatted language model -> ${OUTPUT_FILE}`));
} else if (COMMAND === 'output') {
writeFile(OUTPUT_FILE, cleanOutput(UtteranceExpander(getInputFile())), 'utf8', () => console.log(`Expanded Utterances -> ${OUTPUT_FILE}`));
} else {
invalid();
}
} else {
invalid();
}
} catch(e) {
console.log(DEBUG ? e : e.message);
invalid();
}