Skip to content
Snippets Groups Projects
Select Git revision
2 results Searching

script.js

Blame
  • script.js 4.82 KiB
    var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
    var SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList;
    var SpeechRecognitionEvent = SpeechRecognitionEvent || webkitSpeechRecognitionEvent;
    
    var phrases = [
      'I love to sing because it\'s fun',
      'where are you going',
      'can I call you tomorrow',
      'why did you talk while I was talking',
      'she enjoys reading books and playing games',
      'where are you going',
      'have a great day',
      'she sells seashells on the seashore'
    ];
    
    var phrasePara = document.querySelector('.phrase');
    var resultPara = document.querySelector('.result');
    var diagnosticPara = document.querySelector('.output');
    
    var testBtn = document.querySelector('button');
    
    function randomPhrase() {
      var number = Math.floor(Math.random() * phrases.length);
      return number;
    }
    
    function testSpeech() {
      testBtn.disabled = true;
      testBtn.textContent = 'Test in progress';
    
      var phrase = phrases[randomPhrase()];
      // To ensure case consistency while checking with the returned output text
      phrase = phrase.toLowerCase();
      phrasePara.textContent = phrase;
      resultPara.textContent = 'Right or wrong?';
      resultPara.style.background = 'rgba(0,0,0,0.2)';
      diagnosticPara.textContent = '...diagnostic messages';
    
      var grammar = '#JSGF V1.0; grammar phrase; public <phrase> = ' + phrase +';';
      var recognition = new SpeechRecognition();
      var speechRecognitionList = new SpeechGrammarList();
      speechRecognitionList.addFromString(grammar, 1);
      recognition.grammars = speechRecognitionList;
      recognition.lang = 'en-US';
      recognition.interimResults = false;
      recognition.maxAlternatives = 1;
    
      recognition.start();
    
      recognition.onresult = function(event) {
        // The SpeechRecognitionEvent results property returns a SpeechRecognitionResultList object
        // The SpeechRecognitionResultList object contains SpeechRecognitionResult objects.
        // It has a getter so it can be accessed like an array
        // The first [0] returns the SpeechRecognitionResult at position 0.
        // Each SpeechRecognitionResult object contains SpeechRecognitionAlternative objects that contain individual results.
        // These also have getters so they can be accessed like arrays.
        // The second [0] returns the SpeechRecognitionAlternative at position 0.
        // We then return the transcript property of the SpeechRecognitionAlternative object 
        var speechResult = event.results[0][0].transcript.toLowerCase();
        diagnosticPara.textContent = 'Speech received: ' + speechResult + '.';
        if(speechResult === phrase) {
          resultPara.textContent = 'I heard the correct phrase!';
          resultPara.style.background = 'lime';
        } else {
          resultPara.textContent = 'That didn\'t sound right.';
          resultPara.style.background = 'red';
        }
    
        console.log('Confidence: ' + event.results[0][0].confidence);
      }