Commit f0961218 authored by Chris Lord's avatar Chris Lord

Parse slots and load custom slots (but don't do anything with them yet)

parent db5f5113
......@@ -10,11 +10,13 @@ var skillsPath = Path.join(__dirname, 'skills');
function loadSkills() {
var skills = [];
Fs.readdirSync(skillsPath).forEach(path => {
// Check that this is a directory and not a file
var skillDir = Path.join(skillsPath, path);
if (!Fs.statSync(skillDir).isDirectory()) {
return;
}
// Check that the src and speechAssets directories exist
var srcDir = Path.join(skillDir, 'src');
var intentsDir = Path.join(skillDir, 'speechAssets');
if (!Fs.statSync(srcDir).isDirectory() ||
......@@ -22,6 +24,7 @@ function loadSkills() {
return;
}
// Check the intent and utterances files exist
var intentSchemaFile = Path.join(intentsDir, 'IntentSchema.json');
var utterancesFile = Path.join(intentsDir, 'SampleUtterances.txt');
if (!Fs.statSync(intentSchemaFile).isFile() ||
......@@ -29,6 +32,7 @@ function loadSkills() {
return;
}
// Load the skill
console.log('Loading skill: ' + path);
var srcPath = Path.join(srcDir, 'index.js');
try {
......@@ -36,19 +40,60 @@ function loadSkills() {
skill.name = path;
skill.module = require(srcPath);
skill.intents = {};
skill.customSlots = {};
// Read intents and slots
var data = Fs.readFileSync(intentSchemaFile, 'utf-8');
var intentSchema = JSON.parse(data);
for (var intent of intentSchema.intents) {
skill.intents[intent.intent] = [];
var localIntent = skill.intents[intent.intent] = {};
if (intent.slots) {
localIntent.slots = {};
for (var slot of intent.slots) {
localIntent.slots[slot.name] = slot.type;
}
}
}
// Check for custom slots
try {
var slotsDir = Path.join(intentsDir, 'customSlotTypes');
if (Fs.statSync(slotsDir).isDirectory()) {
Fs.readdirSync(slotsDir).forEach(slotName => {
// Ignore hidden files
if (slotName.startsWith('.')) {
return;
}
var slotFile = Path.join(slotsDir, slotName);
// Make sure it's a file
if (!Fs.statSync(slotFile).isFile()) {
return;
}
skill.customSlots[slotName] = [];
data = Fs.readFileSync(slotFile, 'utf-8');
var slotValues = data.split('\n');
for (var slotValue of slotValues) {
if (slotValue.length) {
skill.customSlots[slotName].push(slotValue);
}
}
});
}
} catch(e) {}
// Read utterances
data = Fs.readFileSync(utterancesFile, 'utf-8');
var utterances = data.split('\n');
for (var utterance of utterances) {
for (var intent in skill.intents) {
if (utterance.startsWith(intent)) {
skill.intents[intent].push(
if (!skill.intents[intent].utterances) {
skill.intents[intent].utterances = [];
}
skill.intents[intent].utterances.push(
utterance.substr(intent.length + 1));
break;
}
......@@ -164,8 +209,22 @@ function listSkills() {
console.log('Skill \'' + skill.name + '\'');
for (var intent in skill.intents) {
console.log('\tIntent \'' + intent + '\'');
for (var utterance of skill.intents[intent]) {
console.log('\t\tUtterance \'' + utterance + '\'');
intent = skill.intents[intent];
if (intent.utterances) {
for (var utterance of intent.utterances) {
console.log('\t\tUtterance \'' + utterance + '\'');
}
}
if (intent.slots) {
for (var slot in intent.slots) {
console.log('\t\tSlot \'' + slot + '\': ' + intent.slots[slot]);
}
}
}
for (var customSlot in skill.customSlots) {
console.log('\tCustom slot \'' + customSlot + '\'');
for (var slotValue of skill.customSlots[customSlot]) {
console.log('\t\tSlot value \'' + slotValue + '\'');
}
}
}
......@@ -236,7 +295,7 @@ rl.on('line', command => {
default:
if (activeSkill) {
for (var intent in activeSkill.intents) {
for (var utterance of activeSkill.intents[intent]) {
for (var utterance of activeSkill.intents[intent].utterances) {
// TODO: Investigate using nlp for fuzzy matching?
if (utterance.localeCompare(command) == 0) {
launch(activeSkill, intent);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment