Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the intent parser #8

Merged
merged 1 commit into from
Oct 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 20 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const shortid = require('shortid');
const WebSocketServer = require('ws').Server;
const stt = require('./lib/stt');
const watson = require('watson-developer-cloud');
const IntentParser = require('intent-parser');

const sorryUnderstand = 'Sorry, but I did not quite understand.';
const sorryTooLong = 'Sorry, but this was a bit too long for me.';
const sorryUnderstand = 'I did not understand that. Can you repeat?';
const sorryService = 'Sorry, the service is not available at the moment.';
const unknown = '<unknown>';

Expand Down Expand Up @@ -110,6 +110,8 @@ const serve = (config, callback) => {
query = url.parse(client.upgradeReq.url, true).query,
sttParams = { audio: audio };

const intentParser = new IntentParser();

audio.on('error', err => log('problem passing audio - ' + err));
rawlog.on('error', err => log('problem logging audio - ' + err));

Expand All @@ -129,6 +131,22 @@ const serve = (config, callback) => {
log('failed - ' + message);
};

const interpret = (command, confidence) => {
// Some cleaning. Remove things like [SMACK], [COUGH] and such...
command = command
.replace(/\[\w+\]/g, '');

intentParser.parse(command)
.then((res) => {
// @todo Implement the query case.
answer(OK, res.confirmation, command, confidence);
})
.catch((err) => {
log('problem interpreting - ' + command, err);
answer(ERROR_PARSING, sorryUnderstand, command, confidence);
});
};

const answer = (status, message, command, confidence) => {
log('sending answer - ' + status + ' - ' + message);
try {
Expand Down Expand Up @@ -165,25 +183,6 @@ const serve = (config, callback) => {
}
};

const interpret = (command, confidence) => {
var product;
try {
// Parsing should happen here.
product = (command);
} catch (ex) {
log('problem interpreting - ' + command);
answer(ERROR_PARSING, sorryUnderstand, command, confidence);
return;
}
if (product.split(' ').length > config.maxwords) {
log('product name too long - ' + product);
answer(ERROR_PARSING, sorryTooLong, command, confidence);
return;
}

answer(OK, 'Virtually added ' + product + ' to your shopping list.', command, confidence);
};

client.on('error', err => fail('client connection' + err));
client.on('message', data => data === 'EOS' ? closeSinks() : writeToSinks(data));

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"google-proto-files": "^0.4.0",
"grpc": "^0.15.0",
"http": "0.0.0",
"intent-parser": "^1.0.6",
"path": "^0.12.7",
"process": "^0.11.7",
"shortid": "^2.2.6",
Expand Down