fixed problem where recognition started too early

plus readability improvements
This commit is contained in:
Niko 2019-07-02 18:10:06 +02:00
parent 11c87ece10
commit cbddf1d1c7

View File

@ -67,12 +67,12 @@ function LoadQuestionTwo () {
// #endregion // #endregion
// #region points // #region points
var questionOnePoints = 0; const questionPoints = {
var questionTwoPoints = 0; 1: 0,
var questionThreePoints = 0; 2: 0,
var questionFourPoints = 0; 3: 0,
var questionFivePoints = 0; 4: 0,
var questionSixPoints = 0; 5: 0 };
// #endregion // #endregion
// tokenization // tokenization
@ -136,10 +136,11 @@ speechsynth.onend = function (event) {
break; break;
} }
if (!skipRecording) { if (!skipRecording) {
recognizeSpeech(); recognition.start();
console.log('reocgnition started. Question: ' + question);
} }
skipRecording = false; skipRecording = false;
diagnosticPara = ''; diagnosticPara.textContent = '';
console.log('global speech end'); console.log('global speech end');
}; };
// #endregion // #endregion
@ -246,7 +247,8 @@ function readQuestionOne () {
window.speechSynthesis.speak(utterance); window.speechSynthesis.speak(utterance);
if (i === 9) { if (i === 9) {
utterance.onend = function (event) { utterance.onend = function (event) {
recognizeSpeech(); recognition.start();
console.log('reocgnition started. Question: ' + question);
}; };
} }
} }
@ -261,21 +263,22 @@ function readQuestionTwo () {
window.setTimeout( window.setTimeout(
function () { function () {
recognition.stop(); recognition.stop();
console.log('recognition stopped');
handleAnswer(answerQuery); handleAnswer(answerQuery);
}, 60000); }, 6000);
recognizeSpeech(); recognition.start();
console.log('reocgnition started. Question: ' + question);
}; };
} }
function readQuestionThree () { function readQuestionThree () {
recognition = false; skipRecording = true;
speak('Dankeschön. Weiter geht es mit der nächsten Frage. '); speak('Dankeschön, weiter geht es mit der nächsten Frage.');
let utterance = new SpeechSynthesisUtterance(); let utterance = new SpeechSynthesisUtterance();
utterance.voice = voices[2]; utterance.voice = voices[2];
utterance.text = QUESTION_THREE; utterance.text = QUESTION_THREE;
window.speechSynthesis.speak(utterance); window.speechSynthesis.speak(utterance);
utterance.onend = function (event) { utterance.onend = function (event) {
console.log('speach end');
speak(QUESTION_THREE_QUESTIONS_PT1[questionThreeCount]); speak(QUESTION_THREE_QUESTIONS_PT1[questionThreeCount]);
}; };
utterance.onerror = function (event) { utterance.onerror = function (event) {
@ -303,9 +306,10 @@ function handleAnswer (query) {
function handleAnswerToFirstQuestion (answer) { function handleAnswerToFirstQuestion (answer) {
var tokens = answer.split(new RegExp(separators.join('|'), 'g')); var tokens = answer.split(new RegExp(separators.join('|'), 'g'));
questionOnePoints += calculatePoints(tokens, QUESTION_ONE_ANSWERS); questionPoints[question] += calculatePoints(tokens, QUESTION_ONE_ANSWERS);
if (partTwo) { if (partTwo) {
partTwo = false; partTwo = false;
console.log('question 1 points: ' + questionPoints[question]);
skipRecording = true; skipRecording = true;
speak('Vielen Dank, nun geht es weiter mit der nächsten Frage'); speak('Vielen Dank, nun geht es weiter mit der nächsten Frage');
startQuestion(2); startQuestion(2);
@ -321,7 +325,8 @@ function handleAnswerToFirstQuestion (answer) {
function handleAnswerToSecondQuestion (answer) { function handleAnswerToSecondQuestion (answer) {
var tokens = answer.split(new RegExp(separators.join('|'), 'g')); var tokens = answer.split(new RegExp(separators.join('|'), 'g'));
questionTwoPoints = calculatePoints(tokens, QUESTION_TWO_ANSWERS); questionPoints[question] = calculatePoints(tokens, QUESTION_TWO_ANSWERS);
console.log('question 2 points: ' + questionPoints[question]);
startQuestion(3); startQuestion(3);
// state = 'detect' // state = 'detect'
} }
@ -341,7 +346,7 @@ function handleAnswerToThirdQuestion (query) {
strike = 0; strike = 0;
partTwo = false; partTwo = false;
questionThreeCount++; questionThreeCount++;
questionThreePoints = questionThreeCount + 1; questionPoints[question] = questionThreeCount + 1;
questionArray = QUESTION_THREE_QUESTIONS_PT1; questionArray = QUESTION_THREE_QUESTIONS_PT1;
} else { } else {
strike++; strike++;
@ -351,6 +356,7 @@ function handleAnswerToThirdQuestion (query) {
if (strike === 2 || questionThreeCount === 5) { if (strike === 2 || questionThreeCount === 5) {
speechsynth.rate = 1; speechsynth.rate = 1;
console.log('question 3 points: ' + questionPoints[question]);
skipRecording = true; skipRecording = true;
speak('weiter geht es mit der Nächsten Frage'); speak('weiter geht es mit der Nächsten Frage');
startQuestion(4); startQuestion(4);
@ -359,13 +365,14 @@ function handleAnswerToThirdQuestion (query) {
speak(questionArray[questionThreeCount]); speak(questionArray[questionThreeCount]);
console.log('count: ' + questionThreeCount + ', strike: ' + strike + ', points: ' + questionThreePoints); console.log('count: ' + questionThreeCount + ', strike: ' + strike + ', points: ' + questionPoints[question]);
} }
// #endregion // #endregion
// #region global functions // #region global functions
function startDemenzScreening () { function startDemenzScreening () {
ws.send('starte demenz test'); ws.send('starte demenz test');
// startQuestion(2);
testBtn.disabled = true; testBtn.disabled = true;
testBtn.textContent = 'Test in progress'; testBtn.textContent = 'Test in progress';
infoPara.textContent = 'wait...'; infoPara.textContent = 'wait...';
@ -378,16 +385,24 @@ function speak (sentence) {
} }
function testSpeechOut () { function testSpeechOut () {
console.log('click'); answerQuery = 'apfel wiese tisch apfel lampe pferd';
speechsynth.text = 'test 123'; question = 1;
speechsynth.volume = 1; for (let i = 0; i < 2; i++) {
speechsynth.rate = 1; var tokens = answerQuery.split(new RegExp(separators.join('|'), 'g'));
console.log(speechsynth); questionPoints[question] += calculatePoints(tokens, QUESTION_ONE_ANSWERS);
window.speechSynthesis.speak(speechsynth);
console.log(window.speechSynthesis);
} }
function recognizeSpeech () { console.log(questionPoints[question]);
// speechsynth.text = 'test 123';
// speechsynth.volume = 1;
// speechsynth.rate = 1;
// console.log(speechsynth);
// window.speechSynthesis.speak(speechsynth);
// console.log(window.speechSynthesis);
}
// function recognizeSpeech () {
// if (state === 'answer') { // if (state === 'answer') {
// var arr; // var arr;
// switch (question) { // switch (question) {
@ -411,8 +426,6 @@ function recognizeSpeech () {
// // speechRecognitionList.addFromString(grammar, 1); // // speechRecognitionList.addFromString(grammar, 1);
// // recognition.grammars = speechRecognitionList; // // recognition.grammars = speechRecognitionList;
// } // }
recognition.start();
console.log('reocgnition started. Question: ' + question);
recognition.onresult = function (event) { recognition.onresult = function (event) {
var last = event.results.length - 1; var last = event.results.length - 1;
@ -476,6 +489,7 @@ function recognizeSpeech () {
} }
} }
// #region speech recognition event
recognition.onspeechend = function () { recognition.onspeechend = function () {
// recognition.stop(); // recognition.stop();
// testBtn.disabled = false; // testBtn.disabled = false;
@ -525,13 +539,17 @@ function recognizeSpeech () {
// Fired when the speech recognition service has begun listening to incoming audio with intent to recognize grammars associated with the current SpeechRecognition. // Fired when the speech recognition service has begun listening to incoming audio with intent to recognize grammars associated with the current SpeechRecognition.
}; };
} // }
// #endregion
function calculatePoints (tokens, dict) { function calculatePoints (tokens, d) {
let points = 0; let points = 0;
let dict = {};
Object.assign(dict, d);
for (let word of tokens) { for (let word of tokens) {
if (dict[word] !== undefined) { if (dict[word] !== undefined) {
points += dict[word]; points += dict[word];
delete dict[word];
} }
} }
return points; return points;