searchData is now using workers, fixed random question letter in question

This commit is contained in:
mrfry 2020-10-14 12:44:31 +02:00
parent 63b08244dc
commit 6c781eba97
5 changed files with 52 additions and 95 deletions
src/utils

View file

@ -4,9 +4,16 @@ const {
parentPort,
workerData,
} = require('worker_threads')
const searchDataWorkerFile = './src/utils/searchData.js'
const logger = require('../../utils/logger.js')
const logger = require('./logger.js')
const searchDataWorkerFile = './src/utils/classes.js'
const assert = (val) => {
if (!val) {
throw new Error('Assertion failed')
}
}
const commonUselessAnswerParts = [
'A helyes válasz az ',
@ -18,17 +25,12 @@ const commonUselessAnswerParts = [
'The correct answer is:',
"'",
]
const commonUselessStringParts = [',', '\\.', ':', '!', '\\+', '\\s*\\.']
const specialChars = ['&', '\\+']
const lengthDiffMultiplier = 10 /* Percent minus for length difference */
const minMatchAmmount = 60 /* Minimum ammount to consider that two questions match during answering */
const assert = (val) => {
if (!val) {
throw new Error('Assertion failed')
}
}
// ---------------------------------------------------------------------------------------------------------
// String Utils
// ---------------------------------------------------------------------------------------------------------
@ -153,16 +155,22 @@ function simplifyAnswer(value) {
}
function simplifyQuestion(question) {
if (typeof q === 'string') {
return simplifyQA(question, [removeSpecialChars, removeUnnecesarySpaces])
if (typeof question === 'string') {
return simplifyQA(question, [
removeSpecialChars,
removeUnnecesarySpaces,
removeAnswerLetters,
])
} else {
question.Q = simplifyQA(question.Q, [
removeSpecialChars,
removeUnnecesarySpaces,
removeAnswerLetters,
])
question.A = simplifyQA(question.A, [
removeSpecialChars,
removeUnnecesarySpaces,
removeAnswerLetters,
])
return question
}
@ -374,6 +382,7 @@ function searchData(data, question, subjName, questionData) {
logger.DebugLog('No subject name as param!', 'qdb search', 1)
}
question = simplifyQuestion(question)
console.log('RESULT: ' + question)
const worker = new Worker(searchDataWorkerFile, {
workerData: { data, subjName, question, questionData },
@ -443,6 +452,7 @@ function dataToString(data) {
return result.join('\n\n')
}
// ------------------------------------------------------------------------
if (!isMainThread) {
const { data, subjName, question, questionData } = workerData
let result = []
@ -493,7 +503,9 @@ if (!isMainThread) {
})
parentPort.postMessage(result)
process.exit(0)
}
// ------------------------------------------------------------------------
module.exports = {
minMatchAmmount,