我无法找到方法stream.on
和stream.recognizeStream.on
的文档。我搜索了documentation和API。我现在的具体问题是关于承诺,例如,这不起作用:
stream.on('data', function(transcribedSpeech) {
console.log(transcribedSpeech);
})
.then(function() {
console.log("Then...");
this.transcribedSpeech = transcribedSpeech;
})
.catch(function() {
console.log("Error");
});
错误为TypeError: stream.on(...).then is not a function
。我试图为IBM Watson Speech-to-text制作Angularjs服务。一切正常,即Watson的消息,成绩单,关键字等都登录到控制台,除了我无法从服务获取数据流到控制器。当我从控制器调用服务时,控制器只看到参数的初始值(undefined
),并且在Watson开始发送数据流之后永远不会看到这些值。我希望承诺会解决这个问题,但没有运气。 : - (
答案 0 :(得分:0)
我不是讲话的专家,但几个月前我使用API参考和IBM的项目做了一个例子。
如您所见,API参考中使用了以下示例,recognizeStream
用于语音文本的实例,并且用于识别您发送的语音,createRecognizeStream
{ {3}} function。
您可以使用其他变量,取决于您,但是,您需要将语音实例化为文本并调用createRecogniseStream来识别语音事件。
服务将在使用utf8设置后转录您的文件并添加文本识别的文字记录文件,如下例所示:
var SpeechToTextV1 = require('watson-developer-cloud/speech-to-text/v1');
var fs = require('fs');
var speech_to_text = new SpeechToTextV1 ({
username: '{username}',
password: '{password}'
});
var params = {
model: 'en-US_BroadbandModel',
content_type: 'audio/flac',
'interim_results': true,
'max_alternatives': 3,
'word_confidence': false,
timestamps: false,
keywords: ['colorado', 'tornado', 'tornadoes'],
'keywords_threshold': 0.5
};
// Create the stream with the instancie of speech to text and access the function to Recognise the speech.
var recognizeStream = speech_to_text.createRecognizeStream(params);
// Pipe in the audio in the parameter recognizeStream.
fs.createReadStream('audio-file.flac').pipe(recognizeStream);
// Pipe out the transcription to a file.
recognizeStream.pipe(fs.createWriteStream('transcription.txt'));
// Get strings instead of buffers from 'data' events.
recognizeStream.setEncoding('utf8');
// Listen for events using the paramter after set utf8 and the transcrit file.
recognizeStream.on('results', function(event) { onEvent('Results:', event); });
recognizeStream.on('data', function(event) { onEvent('Data:', event); });
recognizeStream.on('error', function(event) { onEvent('Error:', event); });
recognizeStream.on('close', function(event) { onEvent('Close:', event); });
recognizeStream.on('speaker_labels', function(event) { onEvent('Speaker_Labels:', event); });
// Displays events on the console.
function onEvent(name, event) {
console.log(name, JSON.stringify(event, null, 2));
};