Browse Source

finished questions

question 4 not possible
small refactoring
master
Niko 4 years ago
parent
commit
42a704a460
1 changed files with 115 additions and 90 deletions
  1. 115
    90
      client/ws-client.js

+ 115
- 90
client/ws-client.js View File

// #region state management // #region state management
var state = ''; var state = '';
var question = 0; var question = 0;
var rePrompt = false;
// currently disabled feature
// var rePrompt = false;
var partTwo = false; var partTwo = false;
var questionThreeCount = 0; var questionThreeCount = 0;
var strike = 0; var strike = 0;
const QUESTION_THREE = 'Ich werde Ihnen jetzt eine Zahlenreihe nennen, die Sie mir dann bitte in umgekehrter Reihenfolge wiederholen sollen. Wenn ich beispielsweise, vier - fünf sage, dann sagen Sie bitte, fünf - vier.'; const QUESTION_THREE = 'Ich werde Ihnen jetzt eine Zahlenreihe nennen, die Sie mir dann bitte in umgekehrter Reihenfolge wiederholen sollen. Wenn ich beispielsweise, vier - fünf sage, dann sagen Sie bitte, fünf - vier.';
const QUESTION_FOUR = 'Wir kommen nun zur vierten Frage. Bitte zählen Sie von 76 an rückwärts. Nennen Sie die Zahlen abwechselnd als ganze Zahlen und als einzelne Ziffern. Zum Beispiel:'; const QUESTION_FOUR = 'Wir kommen nun zur vierten Frage. Bitte zählen Sie von 76 an rückwärts. Nennen Sie die Zahlen abwechselnd als ganze Zahlen und als einzelne Ziffern. Zum Beispiel:';
const QUESTION_FOUR_PT2 = ['34', '3, 3', '32', '3, 1']; const QUESTION_FOUR_PT2 = ['34', '3, 3', '32', '3, 1'];
const QUESTION_FIVE = 'Ganz am Anfang dieses Tests habe ich Ihnen 10 Worte genannt. Können Sie sich noch an diese Worte erinnern?';
// #endregion // #endregion


const FINISH = 'Sie sind jetzt fertig mit dem Test, Vielen Dank.';

// #region intents // #region intents
const WELCOME_INTENT = 'Default Welcome Intent'; const WELCOME_INTENT = 'Default Welcome Intent';
const WELCOME_FOLLOWUP_YES = 'Default Welcome Intent - yes'; const WELCOME_FOLLOWUP_YES = 'Default Welcome Intent - yes';
const WELCOME_FOLLOWUP_NO = 'Default Welcome Intent - no'; const WELCOME_FOLLOWUP_NO = 'Default Welcome Intent - no';
const MORE_TIME = 'Add Time Intent';
const MORE_TIME_YES = 'Add Time Intent - yes';
const MORE_TIME_NO = 'Add Time Intent - no';
// currently disabled feature
// const MORE_TIME = 'Add Time Intent';
// const MORE_TIME_YES = 'Add Time Intent - yes';
// const MORE_TIME_NO = 'Add Time Intent - no';
const QUIT_INTENT = 'Quit Intent'; const QUIT_INTENT = 'Quit Intent';
const FALLBACK_INTENT = 'Default Fallback Intent'; const FALLBACK_INTENT = 'Default Fallback Intent';
const HELP_INTENT = 'Help Intent'; const HELP_INTENT = 'Help Intent';
const CHOOSE_QUESTION = 'Frage_Starten'; const CHOOSE_QUESTION = 'Frage_Starten';
const NEXT_QUESTION = 'Nächste Frage';
// disabled feature
// const NEXT_QUESTION = 'Nächste Frage';
// #endregion // #endregion


// #region questions and expected results // #region questions and expected results
recognition.maxAlternatives = 1; recognition.maxAlternatives = 1;
recognition.continuous = true; recognition.continuous = true;
var answerQuery = ''; var answerQuery = '';
var skipRecording = false;
var proceedWithRecording = false;
// #endregion // #endregion


// #region speech synthesis initialization // #region speech synthesis initialization
}; };


speechsynth.onend = function (event) { speechsynth.onend = function (event) {
switch (question) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
if (!skipRecording) {
if (proceedWithRecording) {
recognition.start(); recognition.start();
console.log('reocgnition started. Question: ' + question); console.log('reocgnition started. Question: ' + question);
} }
skipRecording = false;
proceedWithRecording = false;
diagnosticPara.textContent = ''; diagnosticPara.textContent = '';
console.log('global speech end'); console.log('global speech end');
}; };
ws.onmessage = function (payload) { ws.onmessage = function (payload) {
var dialogflowResult = JSON.parse(payload.data); var dialogflowResult = JSON.parse(payload.data);
checkIntent(dialogflowResult); checkIntent(dialogflowResult);
// document.querySelector('h1').innerHTML = dialogflowResult.intent.displayName;
}; };
// #endregion // #endregion


switch (result.intent.displayName) { switch (result.intent.displayName) {
case QUIT_INTENT: case QUIT_INTENT:
state = 'quit'; state = 'quit';
if (timerId !== undefined) {
clearTimeout(timerId);
}
skipRecording = true;
stopRecognitionFallback();
speak('Beende die Durchführung.'); speak('Beende die Durchführung.');
break; break;
case WELCOME_INTENT: case WELCOME_INTENT:
state = 'detect'; state = 'detect';
proceedWithRecording = true;
speak(result.fulfillmentText); speak(result.fulfillmentText);
break; break;
case WELCOME_FOLLOWUP_YES: case WELCOME_FOLLOWUP_YES:
startQuestion(1); startQuestion(1);
break; break;
case WELCOME_FOLLOWUP_NO: case WELCOME_FOLLOWUP_NO:
skipRecording = true;
speak('Okay, Danke fürs Benutzen.');
break;
case MORE_TIME:
state = 'detect';
speak('Brauchen Sie noch etwas Zeit?');
break;
case MORE_TIME_YES:
rePrompt = true;
state = 'answer';
speak('Alles klar');
speak('Alles klar. Danke fürs Benutzen.');
break; break;
case MORE_TIME_NO:
skipRecording = true;
state = 'answer';
speak('Verstanden');
/// /// /// currently disabled feature
// case MORE_TIME:
// state = 'detect';
// proceedWithRecording = true;
// speak('Brauchen Sie noch etwas Zeit?');
// break;
// case MORE_TIME_YES:
// rePrompt = true;
// state = 'answer';
// proceedWithRecording = true;
// speak('Alles klar');
// break;
// case MORE_TIME_NO:
// state = 'answer';
// speak('Verstanden');
// recognition.stop();
// ws.send(answerQuery);
// break;
// case CHOOSE_QUESTION:
// question = result.parameters.fields.num.numberValue;
// state = 'answer';
// handleQuestion();
// break;
/// /// ///
case HELP_INTENT:
stopRecognitionFallback();
recognition.stop(); recognition.stop();
ws.send(answerQuery);
break;
case CHOOSE_QUESTION:
question = result.parameters.fields.num.numberValue;
state = 'answer';
handleQuestion();
startQuestion(question);
break; break;
case FALLBACK_INTENT: case FALLBACK_INTENT:
// if (state === 'answer') {
// handleAnswer(result.queryText)
// }
// fallback what should happen if no intent is detected
// this is handled in the processSpeech() function
break; break;
default: default:
break; break;
function startQuestion (number) { function startQuestion (number) {
question = number; question = number;
state = 'answer'; state = 'answer';
answerQuery = '';
questionNumDisplay.textContent = 'Question: ' + question; questionNumDisplay.textContent = 'Question: ' + question;
handleQuestion(); handleQuestion();
} }
function handleQuestion () { function handleQuestion () {
switch (question) { switch (question) {
case 1: case 1:
skipRecording = true;
speak(QUESTION_ONE);
readQuestionOne();
readQuestionOne(QUESTION_ONE);
break; break;
case 2: case 2:
readQuestionTwo(); readQuestionTwo();
readQuestionFour(); readQuestionFour();
break; break;
case 5: case 5:
proceedWithRecording = true;
speak(QUESTION_FIVE);
break; break;
} }
} }


function readQuestionOne () {
function readQuestionOne (questionText) {
speak(questionText);
for (let i = 0; i < QUESTION_ONE_QUESTIONS.length; i++) { for (let i = 0; i < QUESTION_ONE_QUESTIONS.length; i++) {
let utterance = new SpeechSynthesisUtterance(); let utterance = new SpeechSynthesisUtterance();
utterance.voice = voices[2]; utterance.voice = voices[2];
} }


function readQuestionThree () { function readQuestionThree () {
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.rate = 0.88; utterance.rate = 0.88;
window.speechSynthesis.speak(utterance); window.speechSynthesis.speak(utterance);
utterance.onend = function (event) { utterance.onend = function (event) {
proceedWithRecording = true;
speak(QUESTION_THREE_QUESTIONS_PT1[questionThreeCount]); speak(QUESTION_THREE_QUESTIONS_PT1[questionThreeCount]);
}; };
} }


function readQuestionFour () { function readQuestionFour () {
// skipRecording = true;
// speak(QUESTION_FOUR);
speak(QUESTION_FOUR);
for (let i = 0; i < QUESTION_FOUR_PT2.length; i++) { for (let i = 0; i < QUESTION_FOUR_PT2.length; i++) {
let utterance = new SpeechSynthesisUtterance(); let utterance = new SpeechSynthesisUtterance();
utterance.voice = voices[2]; utterance.voice = voices[2];
utterance.text = QUESTION_FOUR_PT2[i]; utterance.text = QUESTION_FOUR_PT2[i];
window.speechSynthesis.speak(utterance); window.speechSynthesis.speak(utterance);
} }
// speak('Sie haben hierfür 1 Minute Zeit.');
speak('Sie haben hierfür 1 Minute Zeit.');
} }


function handleAnswer (query) { function handleAnswer (query) {
handleAnswerToThirdQuestion(query); handleAnswerToThirdQuestion(query);
break; break;
case 4: case 4:
// not possible with the current state of web speech api
// handleAnswerToFourthQuestion(query);
break; break;
case 5: case 5:
handleAnswerToFifthQuestion(query);
break; break;
} }
} }


function handleAnswerToFirstQuestion (answer) { function handleAnswerToFirstQuestion (answer) {
var tokens = answer.split(new RegExp(separators.join('|'), 'g'));
questionPoints[question] += calculatePoints(tokens, QUESTION_ONE_ANSWERS);
deconstructAndCalculateAnswer(answer);
if (partTwo) { if (partTwo) {
partTwo = false; partTwo = false;
console.log('question 1 points: ' + questionPoints[question]);
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);
// state = 'detect'
} else { } else {
rePrompt = false;
skipRecording = true;
// currently disabled feature
// rePrompt = false;
speak(QUESTION_ONE_PT2); speak(QUESTION_ONE_PT2);
readQuestionOne(QUESTION_ONE);
readQuestionOne(QUESTION_ONE_PT2);
partTwo = true; partTwo = true;
} }
} }


function handleAnswerToSecondQuestion (answer) { function handleAnswerToSecondQuestion (answer) {
var tokens = answer.split(new RegExp(separators.join('|'), 'g'));
questionPoints[question] = calculatePoints(tokens, QUESTION_TWO_ANSWERS);
console.log('question 2 points: ' + questionPoints[question]);
deconstructAndCalculateAnswer(answer);
startQuestion(3); startQuestion(3);
// state = 'detect'
} }


function handleAnswerToThirdQuestion (query) { function handleAnswerToThirdQuestion (query) {
if (strike === 2 || questionThreeCount === 5) { if (strike === 2 || questionThreeCount === 5) {
speechsynth.rate = 0.88; speechsynth.rate = 0.88;
console.log('question 3 points: ' + questionPoints[question]); console.log('question 3 points: ' + questionPoints[question]);
skipRecording = true;
speak('weiter geht es mit der Nächsten Frage'); speak('weiter geht es mit der Nächsten Frage');
startQuestion(4);
// Question 4 is not possible to take, because of technical limitation
// startQuestion(4);

startQuestion(5);
return; return;
} }

proceedWithRecording = true;
speak(questionArray[questionThreeCount]); speak(questionArray[questionThreeCount]);


console.log('count: ' + questionThreeCount + ', strike: ' + strike + ', points: ' + questionPoints[question]); console.log('count: ' + questionThreeCount + ', strike: ' + strike + ', points: ' + questionPoints[question]);
} }

// not possible with the current state of web speech api
// function handleAnswerToFourthQuestion (answer) {
// // In order to evaluate the answer, the recognizer has to distinguish between the number 77 spoken "seventy seven" and 7-7 spoken "seven seven", which it doesnt
// startQuestion(5);
// }

function handleAnswerToFifthQuestion (answer) {
deconstructAndCalculateAnswer(answer);
speak(FINISH);
}

// #endregion // #endregion


// NOT SUPPORTED
// function recognizeSpeech () { // function recognizeSpeech () {
// if (state === 'answer') {
// var arr; // var arr;
// switch (question) { // switch (question) {
// case 1: // case 1:
// arr = QUESTION_ONE_QUESTIONS; // arr = QUESTION_ONE_QUESTIONS;
// break; // break;
// case 2: // case 2:
// // arr = QUESTION_TWO_QUESTIONS;
// arr = QUESTION_TWO_QUESTIONS;
// break; // break;
// case 3: // case 3:
// arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; // arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
// break; // break;
// case 4: // case 4:
// arr = ['eins', 'zwei', 'drei', 'vier', 'fünf', 'sechs', 'sieben', 'acht', 'neun'];
// break; // break;
// case 5: // case 5:
// break; // break;
// } // }


// // var grammar = '#JSGF V1.0; grammar colors; public <color> = ' + arr.join(' | ') + ' ;';
// // var speechRecognitionList = new SpeechGrammarList();
// // speechRecognitionList.addFromString(grammar, 1);
// // recognition.grammars = speechRecognitionList;
// var grammar = '#JSGF V1.0; grammar arr; public <arr> = ' + arr.join(' | ') + ' ;';
// var speechRecognitionList = new SpeechGrammarList();
// speechRecognitionList.addFromString(grammar, 1);
// recognition.grammars = speechRecognitionList;

// recognition.start();
// } // }


// #region speech recognition event // #region speech recognition event
} }


if (state === 'answer') { if (state === 'answer') {
if (timerId != undefined) {
clearTimeout(timerId);
}
stopRecognitionFallback();
answerQuery += speechResult; answerQuery += speechResult;
timerId = window.setTimeout( timerId = window.setTimeout(
function () { function () {
// currently disabled
// if (!rePrompt) { // if (!rePrompt) {
// ws.send('ich brauche noch etwas Zeit') // ws.send('ich brauche noch etwas Zeit')
// } else { // } else {
console.log('recording end. Evaluate: ' + answerQuery); console.log('recording end. Evaluate: ' + answerQuery);
handleAnswer(answerQuery); handleAnswer(answerQuery);
answerQuery = '';
diagnosticPara.textContent = ''; diagnosticPara.textContent = '';
// } // }
recognition.stop(); recognition.stop();
} }


function testSpeechOut () { function testSpeechOut () {
readQuestionFour();

// speechsynth.text = 'test 123';
// speechsynth.volume = 1;
// speechsynth.rate = 1;
// console.log(speechsynth);
// window.speechSynthesis.speak(speechsynth);
// console.log(window.speechSynthesis);
question = 4;
} }


function speak (sentence) { function speak (sentence) {
window.speechSynthesis.speak(speechsynth); window.speechSynthesis.speak(speechsynth);
} }


function stopRecognitionFallback () {
if (timerId != undefined) {
clearTimeout(timerId);
}
}

function deconstructAndCalculateAnswer (answer) {
let questionAnswers;
switch (question) {
case 1:
questionAnswers = QUESTION_ONE_ANSWERS;
break;
case 2:
questionAnswers = QUESTION_TWO_ANSWERS;
break;
case 5:
questionAnswers = QUESTION_ONE_ANSWERS;
break;
}
var tokens = answer.split(new RegExp(separators.join('|'), 'g'));
questionPoints[question] += calculatePoints(tokens, questionAnswers);
}

function calculatePoints (tokens, d) { function calculatePoints (tokens, d) {
let points = 0; let points = 0;
let dict = {}; let dict = {};

Loading…
Cancel
Save