Skip to content

Instantly share code, notes, and snippets.

@cavedave
Last active September 5, 2017 10:58
Show Gist options
  • Save cavedave/6b0b39216a652cd02d75fb6e82e97ca2 to your computer and use it in GitHub Desktop.
Save cavedave/6b0b39216a652cd02d75fb6e82e97ca2 to your computer and use it in GitHub Desktop.
This is code to add to Watson's Speech to Text service to improve its accuracy. Blogpost explaining this is at https://watson-tricks.blogspot.ie
var SpeechToTextV1 = require('watson-developer-cloud/speech-to-text/v1');
var speech_to_text = new SpeechToTextV1 ({
username: 'username',
password: 'password'
});
var params = {
name: 'DC Example model',
'base_model_name': 'en-US_BroadbandModel',
description: 'Example custom language model'
};
speech_to_text.createCustomization(params, function(error, customization) {
if (error)
console.log('Error:', error);
else
console.log(JSON.stringify(customization, null, 2));
});
'use strict';
const util = require('util');
const SpeechToTextV1 = require('watson-developer-cloud/speech-to-text/v1');
const fs = require('fs');
const speech_to_text = new SpeechToTextV1({
username: 'username',
password: 'password'
});
speech_to_text.createSession({}, function(error, session) {
if (error)
console.log('Error:', error);
else
console.log(JSON.stringify(session, null, 2));
});
//include customisation id to tell it which model to use
const params = {
//you can put in audio here as well
// audio: fs.createReadStream(__dirname + '/DavidCarDemo.wav'),
content_type: 'audio/wav',
customization_id: '06da5480-915c-11e7-bed0-ef2634fd8461'
};
speech_to_text.recognize(params, function(error, transcript) {
if (error)
console.log('Error:', error);
else
console.log(JSON.stringify(transcript, null, 2));
});
// create the stream
const recognizeStream = speech_to_text.createRecognizeStream(params);
// pipe in some audio. This is my file you can use your own
fs.createReadStream(__dirname + '/DavidCarDemo.wav').pipe(recognizeStream);
// and pipe out the transcription
recognizeStream.pipe(fs.createWriteStream('transcription.txt'));
// listen for 'data' events for just the final text
// listen for 'results' events to get the raw JSON with interim results, timings, etc.
recognizeStream.setEncoding('utf8'); // to get strings instead of Buffers from `data` events
//['data', 'results', 'speaker_labels', 'error', 'close'].forEach(function(eventName) {
['data', 'speaker_labels', 'error', 'close'].forEach(function(eventName) {
recognizeStream.on(eventName, console.log.bind(console, eventName + ' event: '));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment