-
You need to create an Wit instance first.
-
Install Node.JS on your computer.
-
Setup your project
Create a new Node.JS app :
$ mkdir myapp $ cd myapp $ npm init ...
Install and add node-wit as a dependencies in your package.json :
npm install --save node-wit
Execute
npm install
in your current folder to fetch the dependenciesWe will send an audio file to Wit.AI
You can use SoX to record WAV files from the command line.
brew install sox
on OSX andapt-get install sox
on ubuntu. The following options will create a Wit-ready WAV file (press Ctrl+C to stop recording):sox -d -b 16 -c 1 -r 16k sample.wav
Create a
index.js
file in myapp directory containing:var wit = require('node-wit'); var fs = require('fs'); var ACCESS_TOKEN = "IQ77NWUPUMNBYEUEKRTWU3VDR5YSLHTA"; console.log("Sending text & audio to Wit.AI"); wit.captureTextIntent(ACCESS_TOKEN, "Hello world", function (err, res) { console.log("Response from Wit for text input: "); if (err) console.log("Error: ", err); console.log(JSON.stringify(res, null, " ")); }); var stream = fs.createReadStream('sample.wav'); wit.captureSpeechIntent(ACCESS_TOKEN, stream, "audio/wav", function (err, res) { console.log("Response from Wit for audio stream: "); if (err) console.log("Error: ", err); console.log(JSON.stringify(res, null, " ")); });
-
Start your app
$ node index.js
Sending text & audio to Wit.AI
Response from Wit for text input:
{
"msg_id": "b46d4a08-1e2e-43f4-b30a-aaa7bccb88e3",
"_text": "Hello world",
"outcomes": [
{
"_text": "Hello world",
"intent": "greetings_hi",
"entities": {},
"confidence": 0.929
}
]
}
Response from Wit for audio stream:
{
"msg_id": "83c14e47-13cb-4ad4-9f5e-723cd47016be",
"_text": "what's the weather in New York",
"outcomes": [
{
"_text": "what's the weather in New York",
"intent": "weather",
"entities": {
"location": [
{
"suggested": true,
"value": "New York"
}
]
},
"confidence": 1
}
]
}
The captureTextIntent
function returns the meaning extracted from the text
input. The function takes 4 parameters:
access_token
: Your access token for your instancetext
: The text input you want to extract the meaning ofoptions
: [optional] A json object containing any call options such asverbose
orcontext
callback(error, response)
: A callback function get 2 arguments:- An
error
when applicable - A JSON object containing the Wit.AI response
- An
var wit = require('node-wit');
wit.captureTextIntent(ACCESS_TOKEN, "Hello world", function (err, res) {
console.log("Response from Wit for text input: ");
if (err) console.log("Error: ", err);
console.log(JSON.stringify(res, null, " "));
});
The captureSpeechIntent
function returns the meaning extracted from the audio
input. The function takes 5 arguments:
access_token
: Your access token for your instancestream
: The audio stream you want to extract the meaning ofcontent-type
: The content-type of your audio stream (audio/wav
,audio/mpeg3
,audio/raw;encoding=unsigned-integer;bits=16;rate=8000;endian=big
, ...)options
: [optional] A json object containing any call options such asverbose
orcontext
callback(error, response)
: A callback function get 2 arguments:- An
error
when applicable - A JSON object containing the Wit.AI response
- An
var wit = require('node-wit');
var fs = require('fs');
var stream = fs.createReadStream('sample.wav');
wit.captureSpeechIntent(ACCESS_TOKEN, stream, "audio/wav", function (err, res) {
console.log("Response from Wit for audio stream: ");
if (err) console.log("Error: ", err);
console.log(JSON.stringify(res, null, " "));
});
The captureSpeechIntentFromMic
function returns the meaning extracted from the audio recorded from your microphone using sox.
The function takes 5 arguments:
access_token
: Your access token for your instanceoptions
: [optional] A json object containing any call options such asverbose
orcontext
callback(error, response)
: A callback function get 2 arguments:- An
error
when applicable - A JSON object containing the Wit.AI response
- An
var wit = require('node-wit');
// The `captureSpeechIntent` function returns the `node-record-lpcm16` object
// See https://github.com/gillesdemey/node-record-lpcm16 for more details
var recording = wit.captureSpeechIntentFromMic(ACCESS_TOKEN, function (err, res) {
console.log("Response from Wit for microphone audio stream: ");
if (err) console.log("Error: ", err);
console.log(JSON.stringify(res, null, " "));
});
// The microphone audio stream will automatically attempt to stop when it encounters silence.
// You can stop the recording manually by calling `stop`
// Ex: Stop recording after five seconds
setTimeout(function () {
recording.stop();
}, 5000);