如何提取从createRecognizeStream()方法返回的值? [英] How to extract the values that return from the createRecognizeStream() method?
本文介绍了如何提取从createRecognizeStream()方法返回的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
将Watson语音用于文本服务 如何提取从createRecognizeStream()方法返回的值?
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
],
[
示例代码:
// 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, ''));
});
推荐答案
如果想要漂亮的多行JSON,则需要对JSON进行字符串化:
You need to stringify the JSON if you want a pretty, multiline JSON:
您应该使用:
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, ''));
});
这篇关于如何提取从createRecognizeStream()方法返回的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文