IBM Watson Speech-to-Text JavaScript SDK:如何获取消息?

IBM Watson Speech-to-Text JavaScript SDK: how to get messages?

我在浏览器或控制台中看到了文字转录,但我没有看到诸如 {'state': 'listening'} 之类的消息。更重要的是,我没有看到 {"results": [{"alternatives": [{"transcript": "name the mayflower "}],"final": true}],"result_index": 0}.

这样的结果

我阅读了 RecognizeStream documentation 并尝试了这段代码:

stream.on('message', function(message) {
    console.log(message);
  });

但这不起作用。我在 truefalse 中都尝试了 object_mode 但输出是相同的。

这是我使用的完整代码:

document.querySelector('#button').onclick = function () {

  var stream = WatsonSpeech.SpeechToText.recognizeMicrophone({
    token: token,
    model: 'en-US_BroadbandModel',
    keywords: ["Colorado"],
    keywords_threshold: 0.50,
    word_confidence: true,
    // outputElement: '#output' // send text to browser instead of console
    object_mode: false
  });

  stream.setEncoding('utf8'); // get text instead of Buffers for on data events

  stream.on('data', function(data) { // send text to console instead of browser
    console.log(data);
  });

  stream.on('error', function(err) {
    console.log(err);
  });

  document.querySelector('#stop').onclick = function() {
    stream.stop();
  };
};

recognizeMicrophone() 方法是一个将多个流链接在一起的助手。 message 事件在中间的一个流上触发。但是,您可以在 stream.recognizeStream 访问那个 - 它总是附加到链中的最后一个以支持这样的情况。

因此,在您的代码中,它应该如下所示:

stream.recognizeStream.on('message', function(frame, data) {
  console.log('message', frame, data)
});

不过,那主要是为了调试。结果 JSON 应该在 data 事件中发出,如果你设置 objectMode: true 并且 不要 调用 stream.setEncoding('utf8');.

(这与 Watson Node.js SDK 有点不同,如果您熟悉它的行为。有计划统一两者,但时间不够...)