SpeechRecognition: phrases 屬性
SpeechRecognition 介面的 phrases 屬性用於設定一個 SpeechRecognitionPhrase 物件陣列,這些物件將用於上下文偏置。
值
一個 SpeechRecognitionPhrase 物件的 ObservableArray。
示例
基本用法
以下程式碼首先建立一個包含要提升的短語及其boost值的陣列。我們透過將原始陣列對映到 SpeechRecognitionPhrase() 建構函式呼叫,將此資料轉換為 SpeechRecognitionPhrase 物件的 ObservableArray。
js
const phraseData = [
{ phrase: "azure", boost: 5.0 },
{ phrase: "khaki", boost: 3.0 },
{ phrase: "tan", boost: 2.0 },
];
const phraseObjects = phraseData.map(
(p) => new SpeechRecognitionPhrase(p.phrase, p.boost),
);
在建立 SpeechRecognition 例項後,我們將上下文偏置短語透過設定為 SpeechRecognition.phrases 屬性的值的 phraseObjects 陣列插入其中。
js
const recognition = new SpeechRecognition();
recognition.continuous = false;
recognition.lang = "en-US";
recognition.interimResults = false;
recognition.processLocally = true;
recognition.phrases = phraseObjects;
// …
此程式碼摘自我們的 裝置端語音顏色更改器(線上執行演示)。有關完整解釋,請參閱 使用 Web 語音 API。
規範
| 規範 |
|---|
| Web Speech API # dom-speechrecognition-phrases |
瀏覽器相容性
載入中…