Question string simplifying

This commit is contained in:
MrFry 2019-10-07 10:29:22 +02:00
parent 5b8ee3fbb8
commit e13d18ec9c

79
main.js
View file

@ -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 minResultMatchPercent = 99 /* Minimum ammount to consider that two questions match during saving */
const lengthDiffMultiplier = 10 /* Percent minus for length difference */ 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 descriptions {{{
class StringUtils { class StringUtils {
RemoveStuff (value, removableStrings) {
removableStrings.forEach((x) => {
var regex = new RegExp(x, 'g')
value = value.replace(regex, '')
})
return value
}
SimplifyQuery (q) { SimplifyQuery (q) {
assert(q) assert(q)
@ -104,22 +126,13 @@ class StringUtils {
assert(value) assert(value)
value = this.RemoveUnnecesarySpaces(value).toLowerCase() value = this.RemoveUnnecesarySpaces(value).toLowerCase()
var removableChars = [',', '.', ':', '!'] return this.RemoveStuff(value, commonUselessStringParts)
for (var i = 0; i < removableChars.length; i++) {
var regex = new RegExp(removableChars[i], 'g')
value = value.replace(regex, '')
}
return value
} }
RemoveSpecialChars (value) { RemoveSpecialChars (value) {
assert(value) assert(value)
let removableChars = ['&'] return this.RemoveStuff(value, ['&'])
return removableChars.reduce((res, x) => {
let regex = new RegExp(x, 'g')
return res.replace(regex, '')
}, value)
} }
// if the value is empty, or whitespace // if the value is empty, or whitespace
@ -152,13 +165,47 @@ class StringUtils {
if (percent < 0) { percent = 0 } if (percent < 0) { percent = 0 }
return percent 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 { class Question {
constructor (q, a, i) { constructor (q, a, i) {
this.Q = SUtils.RemoveSpecialChars(q) this.Q = SUtils.SimplifyQuestion(q)
this.A = SUtils.RemoveSpecialChars(a) this.A = SUtils.SimplifyQuestion(a)
this.I = SUtils.RemoveSpecialChars(i) this.I = i
} }
toString () { toString () {
@ -938,6 +985,7 @@ function ReadFile (cwith) {
} }
function ReadNetDB (cwith, useNetDB) { function ReadNetDB (cwith, useNetDB) {
// TODO: params what to get
function NewXMLHttpRequest () { function NewXMLHttpRequest () {
const url = serverAdress + 'data.json' const url = serverAdress + 'data.json'
xmlhttpRequest({ xmlhttpRequest({
@ -1461,9 +1509,6 @@ function GetQuiz () {
var img = GetImageFormResult(i) var img = GetImageFormResult(i)
question.i = img question.i = img
if (q !== undefined) { q = SUtils.ReplaceCharsWithSpace(q, '\n') }
if (a !== undefined) { a = SUtils.ReplaceCharsWithSpace(a, '\n') }
if (question.a !== undefined) { if (question.a !== undefined) {
quiz.push(new Question(question.q, question.a, question.i)) // adding current question to quiz quiz.push(new Question(question.q, question.a, question.i)) // adding current question to quiz
} else { } else {