diff --git a/main.js b/main.js index 913297a..2b815d8 100644 --- a/main.js +++ b/main.js @@ -54,9 +54,31 @@ const minMatchAmmount = 60 /* Minimum ammount to consider that two questions mat const minResultMatchPercent = 99 /* Minimum ammount to consider that two questions match during saving */ const lengthDiffMultiplier = 10 /* Percent minus for length difference */ +// ------------------------------------------------------------------------------ +// Other constants +// ------------------------------------------------------------------------------ +const commonUselessAnswerParts = [ + 'A helyes válasz az ', + 'A helyes válasz a ', + 'A helyes válaszok: ', + 'A helyes válasz: ', + 'The correct answer is:', + '\'' +] +const commonUselessStringParts = [',', '.', ':', '!'] +// ------------------------------------------------------------------------------ + // : Class descriptions {{{ class StringUtils { + RemoveStuff (value, removableStrings) { + removableStrings.forEach((x) => { + var regex = new RegExp(x, 'g') + value = value.replace(regex, '') + }) + return value + } + SimplifyQuery (q) { assert(q) @@ -104,22 +126,13 @@ class StringUtils { assert(value) value = this.RemoveUnnecesarySpaces(value).toLowerCase() - var removableChars = [',', '.', ':', '!'] - for (var i = 0; i < removableChars.length; i++) { - var regex = new RegExp(removableChars[i], 'g') - value = value.replace(regex, '') - } - return value + return this.RemoveStuff(value, commonUselessStringParts) } RemoveSpecialChars (value) { assert(value) - let removableChars = ['&'] - return removableChars.reduce((res, x) => { - let regex = new RegExp(x, 'g') - return res.replace(regex, '') - }, value) + return this.RemoveStuff(value, ['&']) } // if the value is empty, or whitespace @@ -152,13 +165,47 @@ class StringUtils { if (percent < 0) { percent = 0 } return percent } + + QuestionPreProcessor (value) { + assert(value) + + return this.RemoveStuff( + value, commonUselessAnswerParts) + } + + // 'a. pécsi sör' -> 'pécsi sör' + RemoveAnswerLetters (value) { + assert(value) + + let s = value.split('. ') + if (s[0].length < 2 && s.length > 1) { + return s.shift() + } else { + return value + } + } + + SimplifyQuestion (value) { + if (!value) { return } + + let mods = [ + this.RemoveSpecialChars, + this.RemoveUnnecesarySpaces, + this.QuestionPreProcessor, + this.RemoveAnswerLetters + ] + + return mods.reduce((res, fn) => { + return fn(res) + }, value) + } } class Question { constructor (q, a, i) { - this.Q = SUtils.RemoveSpecialChars(q) - this.A = SUtils.RemoveSpecialChars(a) - this.I = SUtils.RemoveSpecialChars(i) + this.Q = SUtils.SimplifyQuestion(q) + this.A = SUtils.SimplifyQuestion(a) + this.I = i } toString () { @@ -938,6 +985,7 @@ function ReadFile (cwith) { } function ReadNetDB (cwith, useNetDB) { + // TODO: params what to get function NewXMLHttpRequest () { const url = serverAdress + 'data.json' xmlhttpRequest({ @@ -1461,9 +1509,6 @@ function GetQuiz () { var img = GetImageFormResult(i) question.i = img - if (q !== undefined) { q = SUtils.ReplaceCharsWithSpace(q, '\n') } - if (a !== undefined) { a = SUtils.ReplaceCharsWithSpace(a, '\n') } - if (question.a !== undefined) { quiz.push(new Question(question.q, question.a, question.i)) // adding current question to quiz } else {