mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Fixed some files
This commit is contained in:
parent
7fcb15da88
commit
b7ac485689
8 changed files with 250 additions and 199 deletions
|
@ -1,11 +1,6 @@
|
|||
const {
|
||||
Worker,
|
||||
isMainThread,
|
||||
parentPort,
|
||||
workerData,
|
||||
} = require('worker_threads')
|
||||
|
||||
const logger = require('./logger.js')
|
||||
import { Worker, isMainThread, parentPort, workerData } from 'worker_threads'
|
||||
import logger from './logger.js'
|
||||
import { Question, QuestionData, Subject } from '../types/basicTypes'
|
||||
|
||||
const searchDataWorkerFile = './src/utils/classes.js'
|
||||
|
||||
|
@ -42,8 +37,8 @@ const minMatchToNotSearchOtherSubjects = 90
|
|||
|
||||
// Exported
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
function getSubjNameWithoutYear(subjName) {
|
||||
let t = subjName.split(' - ')
|
||||
function getSubjNameWithoutYear(subjName: string) {
|
||||
const t = subjName.split(' - ')
|
||||
if (t[0].match(/^[0-9]{4}\/[0-9]{2}\/[0-9]{1}$/i)) {
|
||||
return t[1] || subjName
|
||||
} else {
|
||||
|
@ -53,16 +48,20 @@ function getSubjNameWithoutYear(subjName) {
|
|||
|
||||
// Not exported
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
function removeStuff(value, removableStrings, toReplace) {
|
||||
function removeStuff(
|
||||
value: string,
|
||||
removableStrings: Array<string>,
|
||||
toReplace: string
|
||||
) {
|
||||
removableStrings.forEach((removableString) => {
|
||||
var regex = new RegExp(removableString, 'g')
|
||||
const regex = new RegExp(removableString, 'g')
|
||||
value = value.replace(regex, toReplace || '')
|
||||
})
|
||||
return value
|
||||
}
|
||||
|
||||
// removes whitespace from begining and and, and replaces multiple spaces with one space
|
||||
function removeUnnecesarySpaces(toremove) {
|
||||
function removeUnnecesarySpaces(toremove: string) {
|
||||
assert(toremove)
|
||||
|
||||
toremove = normalizeSpaces(toremove)
|
||||
|
@ -73,27 +72,27 @@ function removeUnnecesarySpaces(toremove) {
|
|||
}
|
||||
|
||||
// simplifies a string for easier comparison
|
||||
function simplifyStringForComparison(value) {
|
||||
function simplifyStringForComparison(value: string) {
|
||||
assert(value)
|
||||
|
||||
value = removeUnnecesarySpaces(value).toLowerCase()
|
||||
return removeStuff(value, commonUselessStringParts)
|
||||
}
|
||||
|
||||
function removeSpecialChars(value) {
|
||||
function removeSpecialChars(value: string) {
|
||||
assert(value)
|
||||
|
||||
return removeStuff(value, specialChars, ' ')
|
||||
}
|
||||
|
||||
// damn nonbreaking space
|
||||
function normalizeSpaces(input) {
|
||||
function normalizeSpaces(input: string) {
|
||||
assert(input)
|
||||
|
||||
return input.replace(/\s/g, ' ')
|
||||
}
|
||||
|
||||
function compareString(s1, s2) {
|
||||
function compareString(s1: string, s2: string) {
|
||||
if (!s1 || !s2) {
|
||||
if (!s1 && !s2) {
|
||||
return 100
|
||||
|
@ -104,14 +103,14 @@ function compareString(s1, s2) {
|
|||
|
||||
s1 = simplifyStringForComparison(s1).split(' ')
|
||||
s2 = simplifyStringForComparison(s2).split(' ')
|
||||
var match = 0
|
||||
for (var i = 0; i < s1.length; i++) {
|
||||
let match = 0
|
||||
for (let i = 0; i < s1.length; i++) {
|
||||
if (s2.includes(s1[i])) {
|
||||
match++
|
||||
}
|
||||
}
|
||||
var percent = Math.round(((match / s1.length) * 100).toFixed(2)) // matched words percent
|
||||
var lengthDifference = Math.abs(s2.length - s1.length)
|
||||
let percent = Math.round(((match / s1.length) * 100).toFixed(2)) // matched words percent
|
||||
const lengthDifference = Math.abs(s2.length - s1.length)
|
||||
percent -= lengthDifference * lengthDiffMultiplier
|
||||
if (percent < 0) {
|
||||
percent = 0
|
||||
|
@ -119,19 +118,19 @@ function compareString(s1, s2) {
|
|||
return percent
|
||||
}
|
||||
|
||||
function answerPreProcessor(value) {
|
||||
function answerPreProcessor(value: string) {
|
||||
assert(value)
|
||||
|
||||
return removeStuff(value, commonUselessAnswerParts)
|
||||
}
|
||||
|
||||
// 'a. pécsi sör' -> 'pécsi sör'
|
||||
function removeAnswerLetters(value) {
|
||||
function removeAnswerLetters(value: string) {
|
||||
if (!value) {
|
||||
return
|
||||
}
|
||||
|
||||
let val = value.split('. ')
|
||||
const val = value.split('. ')
|
||||
if (val[0].length < 2 && val.length > 1) {
|
||||
val.shift()
|
||||
return val.join(' ')
|
||||
|
@ -140,7 +139,7 @@ function removeAnswerLetters(value) {
|
|||
}
|
||||
}
|
||||
|
||||
function simplifyQA(value, mods) {
|
||||
function simplifyQA(value: string, mods: Array<Function>) {
|
||||
if (!value) {
|
||||
return
|
||||
}
|
||||
|
@ -150,7 +149,7 @@ function simplifyQA(value, mods) {
|
|||
}, value)
|
||||
}
|
||||
|
||||
function simplifyAnswer(value) {
|
||||
function simplifyAnswer(value: string) {
|
||||
if (!value) {
|
||||
return value
|
||||
}
|
||||
|
@ -162,7 +161,7 @@ function simplifyAnswer(value) {
|
|||
])
|
||||
}
|
||||
|
||||
function simplifyQuestion(question) {
|
||||
function simplifyQuestion(question: Question) {
|
||||
if (!question) {
|
||||
return
|
||||
}
|
||||
|
@ -195,7 +194,7 @@ function simplifyQuestion(question) {
|
|||
// Question
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
function createQuestion(question, answer, data) {
|
||||
function createQuestion(question: string, answer: string, data: QuestionData) {
|
||||
return {
|
||||
Q: simplifyQuestion(question),
|
||||
A: simplifyAnswer(answer),
|
||||
|
@ -203,14 +202,14 @@ function createQuestion(question, answer, data) {
|
|||
}
|
||||
}
|
||||
|
||||
function compareImage(data, data2) {
|
||||
function compareImage(data: QuestionData, data2: QuestionData) {
|
||||
return compareString(data.images.join(' '), data2.images.join(' '))
|
||||
}
|
||||
|
||||
function compareData(q1, q2) {
|
||||
function compareData(q1: Question, q2: Question) {
|
||||
try {
|
||||
if (q1.data.type === q2.data.type) {
|
||||
let dataType = q1.data.type
|
||||
const dataType = q1.data.type
|
||||
if (dataType === 'simple') {
|
||||
return -1
|
||||
} else if (dataType === 'image') {
|
||||
|
@ -234,15 +233,21 @@ function compareData(q1, q2) {
|
|||
return 0
|
||||
}
|
||||
|
||||
function compareQuestion(q1, q2) {
|
||||
function compareQuestion(q1: Question, q2: Question) {
|
||||
return compareString(q1.Q, q2.Q)
|
||||
}
|
||||
|
||||
function compareAnswer(q1, q2) {
|
||||
function compareAnswer(q1: Question, q2: Question) {
|
||||
return compareString(q1.A, q2.A)
|
||||
}
|
||||
|
||||
function compareQuestionObj(q1, q1subjName, q2, q2subjName, data) {
|
||||
function compareQuestionObj(
|
||||
q1: Question,
|
||||
q1subjName: string,
|
||||
q2: Question,
|
||||
q2subjName: string,
|
||||
data: QuestionData
|
||||
) {
|
||||
assert(data)
|
||||
assert(q1)
|
||||
assert(typeof q1 === 'object')
|
||||
|
@ -287,7 +292,7 @@ function compareQuestionObj(q1, q1subjName, q2, q2subjName, data) {
|
|||
}
|
||||
}
|
||||
|
||||
function questionToString(question) {
|
||||
function questionToString(question: Question) {
|
||||
const { Q, A, data } = question
|
||||
|
||||
if (data.type !== 'simple') {
|
||||
|
@ -300,12 +305,17 @@ function questionToString(question) {
|
|||
// ---------------------------------------------------------------------------------------------------------
|
||||
// Subject
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
function searchQuestion(subj, question, questionData, subjName) {
|
||||
function searchQuestion(
|
||||
subj: Subject,
|
||||
question: Question,
|
||||
questionData: QuestionData,
|
||||
subjName: string
|
||||
) {
|
||||
assert(question)
|
||||
|
||||
var result = []
|
||||
const result = []
|
||||
subj.Questions.forEach((currentQuestion) => {
|
||||
let percent = compareQuestionObj(
|
||||
const percent = compareQuestionObj(
|
||||
currentQuestion,
|
||||
subjName,
|
||||
question,
|
||||
|
@ -335,10 +345,10 @@ function searchQuestion(subj, question, questionData, subjName) {
|
|||
return result
|
||||
}
|
||||
|
||||
function subjectToString(subj) {
|
||||
function subjectToString(subj: Subject) {
|
||||
const { Questions, Name } = subj
|
||||
|
||||
var result = []
|
||||
const result = []
|
||||
Questions.forEach((question) => {
|
||||
result.push(questionToString(question))
|
||||
})
|
||||
|
@ -349,7 +359,7 @@ function subjectToString(subj) {
|
|||
// ---------------------------------------------------------------------------------------------------------
|
||||
// QuestionDB
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
function addQuestion(data, subj, question) {
|
||||
function addQuestion(data: Array<Subject>, subj: string, question: Question) {
|
||||
logger.DebugLog('Adding new question with subjName: ' + subj, 'qdb add', 1)
|
||||
logger.DebugLog(question, 'qdb add', 3)
|
||||
assert(data)
|
||||
|
@ -357,7 +367,7 @@ function addQuestion(data, subj, question) {
|
|||
assert(question)
|
||||
assert(typeof question === 'object')
|
||||
|
||||
var i = 0
|
||||
let i = 0
|
||||
while (
|
||||
i < data.length &&
|
||||
!subj
|
||||
|
@ -379,7 +389,12 @@ function addQuestion(data, subj, question) {
|
|||
}
|
||||
}
|
||||
|
||||
function searchData(data, question, subjName, questionData) {
|
||||
function searchData(
|
||||
data: Array<Subject>,
|
||||
question: Question,
|
||||
subjName: string,
|
||||
questionData: QuestionData
|
||||
) {
|
||||
return new Promise((resolve, reject) => {
|
||||
assert(data)
|
||||
assert(question)
|
||||
|
@ -436,11 +451,11 @@ function searchData(data, question, subjName, questionData) {
|
|||
})
|
||||
}
|
||||
|
||||
function addSubject(data, subj) {
|
||||
function addSubject(data: Array<Subject>, subj) {
|
||||
assert(data)
|
||||
assert(subj)
|
||||
|
||||
var i = 0
|
||||
let i = 0
|
||||
while (i < length && subj.Name !== data[i].Name) {
|
||||
i++
|
||||
}
|
||||
|
@ -461,8 +476,8 @@ function addSubject(data, subj) {
|
|||
}
|
||||
}
|
||||
|
||||
function dataToString(data) {
|
||||
var result = []
|
||||
function dataToString(data: Array<Subject>) {
|
||||
const result = []
|
||||
data.forEach((subj) => {
|
||||
result.push(subjectToString(subj))
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue