Skip to content

Instantly share code, notes, and snippets.

@germanattanasio
Created August 1, 2015 03:42
Show Gist options
  • Save germanattanasio/359580d99ea22f47c6ba to your computer and use it in GitHub Desktop.
Save germanattanasio/359580d99ea22f47c6ba to your computer and use it in GitHub Desktop.
Using Speech to Text with and shows interim results
'use strict';
// replace username and password with speech to text credentials
// audio.wav can be found here: https://github.com/watson-developer-cloud/nodejs-wrapper/blob/master/test/resources/audio.wav?raw=true
var watson = require('watson-developer-cloud'),
fs = require('fs');
var speechToText = watson.speech_to_text({
password: '<username>',
username: '<password>',
version: 'v1'
});
var noop = function(){};
var observeResult = function(err, transcript) {
if (err){
console.log(err);
return;
}
// print the interim results
console.log(JSON.stringify(transcript, null, 2));
};
speechToText.createSession({}, function(err, session){
if (err){
console.log('error:', err);
return;
}
var request = speechToText.recognizeLive({
content_type: 'audio/l16;rate=44100',
continuous: true,
word_confidence: true,
interim_results: true,
session_id: session.session_id,
cookie_session: session.cookie_session }, noop);
// call observe result to get intermin results
speechToText.observeResult({
interim_results: true,
session_id: session.session_id,
cookie_session: session.cookie_session }, observeResult);
// pipe the audio to the request
// once the stream is consumed it will call request.end()
fs.createReadStream('audio.wav').pipe(request);
});
@germanattanasio
Copy link
Author

The output is:

{
  "results": [
    {
      "alternatives": [
        {
          "transcript": "thunder "
        }
      ],
      "final": false
    }
  ],
  "result_index": 0
}
{
  "results": [
    {
      "alternatives": [
        {
          "transcript": "thunderstone "
        }
      ],
      "final": false
    }
  ],
  "result_index": 0
}
{
  "results": [
    {
      "alternatives": [
        {
          "transcript": "thunderstorms "
        }
      ],
      "final": false
    }
  ],
  "result_index": 0
}
{
  "results": [
    {
      "alternatives": [
        {
          "transcript": "thunderstorms could "
        }
      ],
      "final": false
    }
  ],
  "result_index": 0
}
{
  "results": [
    {
      "alternatives": [
        {
          "transcript": "thunderstorms could produce "
        }
      ],
      "final": false
    }
  ],
  "result_index": 0
}
{
  "results": [
    {
      "alternatives": [
        {
          "transcript": "thunderstorms could produce law "
        }
      ],
      "final": false
    }
  ],
  "result_index": 0
}
{
  "results": [
    {
      "alternatives": [
        {
          "transcript": "thunderstorms could produce large "
        }
      ],
      "final": false
    }
  ],
  "result_index": 0
}
{
  "results": [
    {
      "alternatives": [
        {
          "transcript": "thunderstorms could produce large hail "
        }
      ],
      "final": false
    }
  ],
  "result_index": 0
}
{
  "results": [
    {
      "alternatives": [
        {
          "transcript": "thunderstorms could produce large hail basil "
        }
      ],
      "final": false
    }
  ],
  "result_index": 0
}
{
  "results": [
    {
      "alternatives": [
        {
          "transcript": "thunderstorms could produce large hail isolated "
        }
      ],
      "final": false
    }
  ],
  "result_index": 0
}
{
  "results": [
    {
      "alternatives": [
        {
          "transcript": "thunderstorms could produce large hail isolated torn "
        }
      ],
      "final": false
    }
  ],
  "result_index": 0
}
{
  "results": [
    {
      "alternatives": [
        {
          "transcript": "thunderstorms could produce large hail isolated tornado "
        }
      ],
      "final": false
    }
  ],
  "result_index": 0
}
{
  "results": [
    {
      "alternatives": [
        {
          "transcript": "thunderstorms could produce large hail isolated tornadoes and "
        }
      ],
      "final": false
    }
  ],
  "result_index": 0
}
{
  "results": [
    {
      "alternatives": [
        {
          "transcript": "thunderstorms could produce large hail isolated tornadoes and have you "
        }
      ],
      "final": false
    }
  ],
  "result_index": 0
}
{
  "results": [
    {
      "alternatives": [
        {
          "transcript": "thunderstorms could produce large hail isolated tornadoes and heavy rain "
        }
      ],
      "final": false
    }
  ],
  "result_index": 0
}
{
  "results": [
    {
      "alternatives": [
        {
          "transcript": "thunderstorms could produce large hail isolated tornadoes and heavy rain "
        }
      ],
      "final": true
    }
  ],
  "result_index": 0
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment