{"action":"opened","issue":{"url":"https://api.github.com/repos/watson-developer-cloud/node-sdk/issues/259","repository_url":"https://api.github.com/repos/watson-developer-cloud/node-sdk","labels_url":"https://api.github.com/repos/watson-developer-cloud/node-sdk/issues/259/labels{/name}","comments_url":"https://api.github.com/repos/watson-developer-cloud/node-sdk/issues/259/comments","events_url":"https://api.github.com/repos/watson-developer-cloud/node-sdk/issues/259/events","html_url":"https://github.com/watson-developer-cloud/node-sdk/issues/259","id":157727381,"number":259,"title":"[speech-to-text] Improve event-handling and output for createRecognizeStream() example","user":{"login":"jeffpk62","id":10051651,"avatar_url":"https://avatars.githubusercontent.com/u/10051651?v=3","gravatar_id":"","url":"https://api.github.com/users/jeffpk62","html_url":"https://github.com/jeffpk62","followers_url":"https://api.github.com/users/jeffpk62/followers","following_url":"https://api.github.com/users/jeffpk62/following{/other_user}","gists_url":"https://api.github.com/users/jeffpk62/gists{/gist_id}","starred_url":"https://api.github.com/users/jeffpk62/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jeffpk62/subscriptions","organizations_url":"https://api.github.com/users/jeffpk62/orgs","repos_url":"https://api.github.com/users/jeffpk62/repos","events_url":"https://api.github.com/users/jeffpk62/events{/privacy}","received_events_url":"https://api.github.com/users/jeffpk62/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-05-31T17:32:50Z","updated_at":"2016-05-31T17:32:50Z","closed_at":null,"body":"The current example for the createRecognizeStream() method in https://github.com/watson-developer-cloud/node-sdk/blob/master/examples/speech_to_text.v1.js reads as follows:\r\n\r\n```\r\n'use strict';\r\n\r\nvar watson = require('watson-developer-cloud');\r\nvar fs = require('fs');\r\n\r\nvar speech_to_text = watson.speech_to_text({\r\n  username: 'INSERT YOUR USERNAME FOR THE SERVICE HERE',\r\n  password: 'INSERT YOUR PASSWORD FOR THE SERVICE HERE',\r\n  version: 'v1',\r\n  url: 'https://stream.watsonplatform.net/speech-to-text/api'\r\n});\r\n\r\nvar params = {\r\n  content_type: 'audio/wav'\r\n};\r\n\r\n// create the stream\r\nvar recognizeStream = speech_to_text.createRecognizeStream(params);\r\n\r\n// pipe in some audio\r\nfs.createReadStream(__dirname + '/resources/speech.wav').pipe(recognizeStream);\r\n\r\n// and pipe out the transcription\r\nrecognizeStream.pipe(fs.createWriteStream('transcription.txt'));\r\n\r\n// listen for 'data' events for just the final text\r\n// listen for 'results' events to get the raw JSON with interim results, timings, etc.\r\n\r\nrecognizeStream.setEncoding('utf8'); // to get strings instead of Buffers from `data` events\r\n\r\n['data', 'results', 'error', 'connection-close'].forEach(function(eventName) {\r\n  recognizeStream.on(eventName, console.log.bind(console, eventName + ' event: '));\r\n});\r\n```\r\n\r\nThe event-handling code in the example produces the following output:\r\n\r\n```\r\nresults event:  { results: [ { alternatives: [Object], final: false } ],\r\n  result_index: 0 }\r\nresults event:  { results: [ { alternatives: [Object], final: false } ],\r\n  result_index: 0 }\r\nresults event:  { results: [ { alternatives: [Object], final: true } ],\r\n  result_index: 0 }\r\ndata event:  hello world\r\n```\r\n\r\nBy changing the event-handling code to the following:\r\n\r\n```\r\n// Listen for events.\r\nrecognizeStream.on('data', function(event) { onEvent('Data:', event); });\r\nrecognizeStream.on('results', function(event) { onEvent('Results:', event); });\r\nrecognizeStream.on('error', function(event) { onEvent('Error:', event); });\r\nrecognizeStream.on('close-connection', function(event) { onEvent('Close:', event); });\r\n\r\n// Displays events on the console.\r\nfunction onEvent(name, event) {\r\n    console.log(name, JSON.stringify(event, null, 2));\r\n};\r\n```\r\n\r\nWe make the example a bit easier to understand and produce complete output of the following form, which is what the user would expect to see based on existing examples and parameters passed in the call:\r\n\r\n```\r\nResults: {\r\n  \"results\": [\r\n    {\r\n      \"alternatives\": [\r\n        {\r\n          \"timestamps\": [\r\n            [\r\n              \"hello\",\r\n              0.03,\r\n              0.17\r\n            ]\r\n          ],\r\n          \"transcript\": \"hello \"\r\n        }\r\n      ],\r\n      \"final\": false\r\n    }\r\n  ],\r\n  \"result_index\": 0\r\n}\r\nResults: {\r\n  \"results\": [\r\n    {\r\n      \"alternatives\": [\r\n        {\r\n          \"timestamps\": [\r\n            [\r\n              \"hello\",\r\n              0.03,\r\n              0.25\r\n            ],\r\n            [\r\n              \"world\",\r\n              0.25,\r\n              0.72\r\n            ]\r\n          ],\r\n          \"transcript\": \"hello world \"\r\n        }\r\n      ],\r\n      \"final\": false\r\n    }\r\n  ],\r\n  \"result_index\": 0\r\n}\r\nResults: {\r\n  \"results\": [\r\n    {\r\n      \"alternatives\": [\r\n        {\r\n          \"word_confidence\": [\r\n            [\r\n              \"hello\",\r\n              0.9999999999999836\r\n            ],\r\n            [\r\n              \"world\",\r\n              0.9960954077011375\r\n            ]\r\n          ],\r\n          \"confidence\": 0.997,\r\n          \"transcript\": \"hello world \",\r\n          \"timestamps\": [\r\n            [\r\n              \"hello\",\r\n              0.03,\r\n              0.25\r\n            ],\r\n            [\r\n              \"world\",\r\n              0.25,\r\n              0.72\r\n            ]\r\n          ]\r\n        }\r\n      ],\r\n      \"final\": true\r\n    }\r\n  ],\r\n  \"result_index\": 0\r\n}\r\nData: \"hello world \"\r\n```\r\n\r\n\r\n\r\n\r\n\r\n"}}
{"actor":{"display_login":"jeffpk62"}}