<commit_before>/* global counter, loadPano, panosList, playAudio, SpeechRecognition, SpeechGrammarList */
(function () {

var commands = {
  'next': 1,
  'previous': -1,
  'forward': 1,
  'back': -1,
  'computer next': 1,
  'computer previous': -1,
  'computer next program': 1,
  'computer previous program': -1,
  'computer start next program': 1,
  'computer start previous program': -1,
  'computer forward': 1,
  'computer back': -1,
  'computer go forward': 1,
  'computer go back': -1,
};

var panosByKey = {};


panosList.then(function (panos) {

  panos.forEach(function (pano) {
    panosByKey[pano.id] = pano;
    (pano.commands || []).forEach(function (cmd) {
      commands[cmd] = pano;
    });
  });

  var grammar = Object.keys(commands).join(' | ');

  createGrammar();
  setGrammar('#JSGF V1.0; grammar holodeck; public <simple> = ' + grammar + ';');
  initSpeech();

});


var finalTranscript = '';
// var language = 'en-US';
var recognising = false;
var recognition;
var speechrecognitionlist;


function initSpeech() {
  // Create the recognition object and define four event handlers (onstart, onerror, onend, onresult).
  recognition = new SpeechRecognition();

  console.log('SpeechRecognition ready');

  //recognition.continuous = true;         // keep processing input until stopped // MOZ: NS_ERROR_NOT_IMPLEMENTED
  //recognition.interimResults = true;     // show interim results // MOZ: NS_ERROR_NOT_IMPLEMENTED
  //recognition.lang = language;           // specify the language // MOZ: NS_ERROR_NOT_IMPLEMENTED

  recognition.onstart = function () {
    recognising = true;
    console.log('Speak slowly and clearly');
  };

  recognition.onerror = function (e) {
    console.log('There was a recognition error\n', e);
  };

  recognition.onend = function () {
    recognising = false;
    console.log('Done');
  };

  recognition.onresult = function (e) {
    console.log('recognition.onresult called');

    var interimTranscript = '';
    var score = '';

    // Assemble the transcript from the array of results.
    for (var i = e.resultIndex; i < e.results.length; ++i) {
      if (e.results[i].isFinal) {
        console.log('recognition.onresult: isFinal');
        finalTranscript += e.results[i][0].transcript;
      } else {
        console.log('recognition.onresult: not isFinal');
        interimTranscript += e.results[i][0].transcript;
      }

      score = e.results[i][0].confidence;
    }

    console.log('interim: %s', interimTranscript);
    console.log('final: %s', finalTranscript);

    if (finalTranscript === 'ERROR') {
      return start();
    }

    if (finalTranscript || interimTranscript) {
      process(finalTranscript || interimTranscript, score);
    }
  };
}

function stop() {
  recognition.stop();
  console.log('Stopped');
  recognising = false;
}

function start() {
  finalTranscript = '';

  // Request access to the user's mic and start recognising voice input.
  recognition.start();

  console.log('Starting');
  console.log('Allow the browser to use your Microphone');

  recognising = true;
}

function createGrammar() {
  speechrecognitionlist = new SpeechGrammarList();
  console.log('SpeechGrammarList created');
}

function setGrammar(grammar) {
  speechrecognitionlist.addFromString(grammar, 1);
  console.log('Grammar set', grammar);
}

// function associateGrammar() {
//   recognition.grammars = speechrecognitionlist;
// }

function process(transcript, score) {
  console.log('confidence score: %.2f', score);

  if (transcript === 'ERROR') {
    console.error('invalid transcript');
  } else {
    console.log('transcript:', transcript);
    console.log(commands, transcript[commands]);
    if (transcript in commands) {
      startProgram(commands[transcript]);
    }
  }
}

function startProgram(program) {
  if (typeof program === 'number') {
    loadPano(program, true);
  } else if (typeof program === 'object') {
    panosList.then(function (panos) {
      console.log('Loading program:', program.title);

      counter = panos.indexOf(program);

      loadPano(0, true);
    });
  }
}


document.addEventListener('keypress', function (e) {
  if (e.alt || e.ctrlKey || e.metaKey) {
    return;
  }

  if (e.key === ' ') {
    e.preventDefault();
    if (recognising) {
      stop();
    } else {
      playAudio('audio/hologram_on.mp3');
      start();
    }
  }
});

})();
<commit_msg>add commands to start Holodeck<commit_after>/* global counter, loadPano, panosList, playAudio, SpeechRecognition, SpeechGrammarList */
(function () {

var startCommands = {
  'start': startHolodeck,
  'computer start': startHolodeck,
};

var commands = {
  'next': 1,
  'previous': -1,
  'forward': 1,
  'back': -1,
  'computer next': 1,
  'computer previous': -1,
  'computer start program': 1,
  'computer next program': 1,
  'computer previous program': -1,
  'computer start next program': 1,
  'computer start previous program': -1,
  'computer forward': 1,
  'computer back': -1,
  'computer go forward': 1,
  'computer go back': -1,
};

var panosByKey = {};


panosList.then(function (panos) {

  panos.forEach(function (pano) {
    panosByKey[pano.id] = pano;
    (pano.commands || []).forEach(function (cmd) {
      commands[cmd] = pano;
    });
  });

  var grammar = Object.keys(commands).join(' | ');

  createGrammar();
  setGrammar('#JSGF V1.0; grammar holodeck; public <simple> = ' + grammar + ';');
  initSpeech();

});


var finalTranscript = '';
// var language = 'en-US';
var recognising = false;
var recognition;
var speechrecognitionlist;


function initSpeech() {
  // Create the recognition object and define four event handlers (onstart, onerror, onend, onresult).
  recognition = new SpeechRecognition();

  console.log('SpeechRecognition ready');

  //recognition.continuous = true;         // keep processing input until stopped // MOZ: NS_ERROR_NOT_IMPLEMENTED
  //recognition.interimResults = true;     // show interim results // MOZ: NS_ERROR_NOT_IMPLEMENTED
  //recognition.lang = language;           // specify the language // MOZ: NS_ERROR_NOT_IMPLEMENTED

  recognition.onstart = function () {
    recognising = true;
    console.log('Speak slowly and clearly');
  };

  recognition.onerror = function (e) {
    console.log('There was a recognition error\n', e);
  };

  recognition.onend = function () {
    recognising = false;
    console.log('Done');
  };

  recognition.onresult = function (e) {
    console.log('recognition.onresult called');

    var interimTranscript = '';
    var score = '';

    // Assemble the transcript from the array of results.
    for (var i = e.resultIndex; i < e.results.length; ++i) {
      if (e.results[i].isFinal) {
        console.log('recognition.onresult: isFinal');
        finalTranscript += e.results[i][0].transcript;
      } else {
        console.log('recognition.onresult: not isFinal');
        interimTranscript += e.results[i][0].transcript;
      }

      score = e.results[i][0].confidence;
    }

    console.log('interim: %s', interimTranscript);
    console.log('final: %s', finalTranscript);

    if (finalTranscript === 'ERROR') {
      return start();
    }

    if (finalTranscript || interimTranscript) {
      process(finalTranscript || interimTranscript, score);
    }
  };
}

function stop() {
  recognition.stop();
  console.log('Stopped');
  recognising = false;
}

function start() {
  finalTranscript = '';

  // Request access to the user's mic and start recognising voice input.
  recognition.start();

  console.log('Starting');
  console.log('Allow the browser to use your Microphone');

  recognising = true;
}

function createGrammar() {
  speechrecognitionlist = new SpeechGrammarList();
  console.log('SpeechGrammarList created');
}

function setGrammar(grammar) {
  speechrecognitionlist.addFromString(grammar, 1);
  console.log('Grammar set', grammar);
}

// function associateGrammar() {
//   recognition.grammars = speechrecognitionlist;
// }

function process(transcript, score) {
  console.log('confidence score: %.2f', score);

  if (transcript === 'ERROR') {
    console.error('invalid transcript');
  } else {
    console.log('transcript:', transcript);
    console.log(commands, transcript[commands]);
    if (transcript in commands) {
      startProgram(commands[transcript]);
    }
  }
}

function startProgram(program) {
  if (typeof program === 'number') {
    loadPano(program, true);
  } else if (typeof program === 'object') {
    panosList.then(function (panos) {
      console.log('Loading program:', program.title);

      counter = panos.indexOf(program);

      loadPano(0, true);
    });
  }
}

function startHolodeck() {
  counter = 0;
  loadPano(0, true);
}


document.addEventListener('keypress', function (e) {
  if (e.alt || e.ctrlKey || e.metaKey) {
    return;
  }

  if (e.key === ' ') {
    e.preventDefault();
    if (recognising) {
      stop();
    } else {
      playAudio('audio/hologram_on.mp3');
      start();
    }
  }
});

})();

{'fraction_non_alphanumeric': 0.15080870050195203, 'fraction_numerical': 0.005465699944227551, 'mean_word_length': 3.3907933398628796, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6382978', 'n_tokens_mistral': 2957, 'n_tokens_neox': 2822, 'n_words': 669}