簡體   English   中英

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 文檔並嘗試了以下代碼:

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)
});

但是,這主要用於調試。 如果您設置objectMode: true並且調用stream.setEncoding('utf8'); ,則結果 JSON 應該在data事件上發出stream.setEncoding('utf8'); .

(這與 Watson Node.js SDK 有點不同,如果您熟悉它的行為。有計划將兩者統一,但時間不夠...)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM