Я хочу сопоставить текст, который я получу от распознавания речи в webkit, с моим абзацем и выделить текст абзаца - PullRequest
0 голосов
/ 18 июня 2019

Я получу текст из распознавания речи Webkit, у меня есть абзац в Html, и я хочу сопоставить текст, который я получу из распознавания речи, с моим абзацем и выделить текст абзаца.

var message = document.querySelector('#message');
var text = document.querySelector('#text').innerHTML;

var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
var SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList;

var grammar = '#JSGF V1.0;'

var recognition = new SpeechRecognition();
var speechRecognitionList = new SpeechGrammarList();
speechRecognitionList.addFromString(grammar, 1);
recognition.grammars = speechRecognitionList;
recognition.lang = 'en-US';
recognition.interimResults = false;

recognition.onresult = function(event) {
    var last = event.results.length - 1;
    var command = event.results[last][0].transcript;
    message.textContent = 'Voice Input: ' + command + '.';

    if (command.length !== 0) {
             //Handle special characters used in regex
            var searchregexp = new RegExp(command.replace(/[.*+?^${}()|[\]\\]/, '\\$&'));
            //$& will maintain uppercase and lowercase characters.
            $(this).html($(this).html().toLowerCase().replace(searchregexp, "<span class = 'highlight'>$&</span>"));
    }
};

recognition.onspeechend = function() {
    recognition.stop();
};

recognition.onerror = function(event) {
    message.textContent = 'Error occurred in recognition: ' + event.error;
}        

document.querySelector('#btnGiveCommand').addEventListener('click', function(){
    recognition.start();
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...