How to extract the values that return from the createRecognizeStream() method?

走远了吗. 提交于 2019-12-11 00:42:58

问题


Using Watson Speech to Text Services How to extract the values that return from the createRecognizeStream() method?

Here is a chunk of the sample code. I am trying to see in the terminal the interim results but all i get is this. How do I set the options for the results to appear?

{ results: [ { alternatives: [Object], final: false } ],
result_index: 0 }
{ results: [ { alternatives: [Object], final: false } ],
result_index: 0 }
{ results: [ { alternatives: [Object], final: false } ]...

they should look like this:

{
 "results": [
{
  "alternatives": [
    {
      "timestamps": [
        [
          "Here",
          0.08,
          0.63
        ],
        [
          "I",
          0.66,
          0.95
        ],
        [
          "Open",
          0.95,
          1.07
        ],
        [
          "a",
          1.07,
          1.33
        ],
        [
          "strong",
          1.33,
          1.95
        ],
        [
          "group",
          2.03,
          2.18
        ],
        [
          "of",
          2.18,
          2.72
        ],
        [

sample code:

  // create the stream
    var recognizeStream = speech_to_text.createRecognizeStream(params);

    // pipe in some audio
    fs.createReadStream(filepath).pipe(recognizeStream);;
    // and pipe out the transcription
    recognizeStream.pipe(fs.createWriteStream('transcriptions/transcription-' + path.basename(filepath) + '.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', 'error', 'connection-close'].forEach(function(eventName) {
    //JSON.stringify(eventName, null, 2)            
    //fs.writeFile('./transcript.txt', JSON.stringify(transcript), function(err) {if(err){return console.log('err')}});
        recognizeStream.on('results', console.log.bind(console, ''));
    });

回答1:


You need to stringify the JSON if you want a pretty, multiline JSON:

Instead of using console.log you should use:

JSON.stringify(value[, replacer[, space]])

Based on your example you will do:

var recognizeStream = speech_to_text.createRecognizeStream(params);

fs.createReadStream(filepath).pipe(recognizeStream);

recognizeStream.setEncoding('utf8');

// print interim results and final results
['data','results'].forEach(function(eventName) {
  recognizeStream.on(eventName, JSON.stringify.bind(JSON));
});

// print error and connection-close events
['error', 'connection-close'].forEach(function(eventName) {
  recognizeStream.on(eventName, console.log.bind(console, ''));
});


来源:https://stackoverflow.com/questions/35943698/how-to-extract-the-values-that-return-from-the-createrecognizestream-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!