mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
added / fixed some types
This commit is contained in:
parent
5f12284bb8
commit
bc5c293539
41 changed files with 4378 additions and 8304 deletions
|
@ -8,21 +8,26 @@ import {
|
|||
QuestionDb,
|
||||
Subject,
|
||||
} from '../types/basicTypes'
|
||||
import { editDb } from './actions'
|
||||
import { editDb, Edits } from './actions'
|
||||
|
||||
interface SearchResultQuestion extends Question {
|
||||
export interface WorkerResult {
|
||||
msg: string
|
||||
workerIndex: number
|
||||
result: SearchResultQuestion[]
|
||||
}
|
||||
|
||||
interface DetailedMatch {
|
||||
qMatch: number
|
||||
aMatch: number
|
||||
dMatch: number
|
||||
matchedSubjName: string
|
||||
avg: number
|
||||
}
|
||||
|
||||
export interface SearchResultQuestion {
|
||||
q: Question
|
||||
match: number
|
||||
}
|
||||
|
||||
export interface SearchResult {
|
||||
result: Array<SearchResultQuestion>
|
||||
dbName: string
|
||||
}
|
||||
|
||||
const assert = (val) => {
|
||||
if (!val) {
|
||||
throw new Error('Assertion failed')
|
||||
}
|
||||
detailedMatch: DetailedMatch
|
||||
}
|
||||
|
||||
const commonUselessAnswerParts = [
|
||||
|
@ -65,7 +70,7 @@ function getSubjNameWithoutYear(subjName: string): string {
|
|||
// Not exported
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
function simplifyString(toremove) {
|
||||
function simplifyString(toremove: string): string {
|
||||
return toremove.replace(/\s/g, ' ').replace(/\s+/g, ' ').toLowerCase()
|
||||
}
|
||||
|
||||
|
@ -73,7 +78,7 @@ function removeStuff(
|
|||
value: string,
|
||||
removableStrings: Array<string>,
|
||||
toReplace?: string
|
||||
) {
|
||||
): string {
|
||||
removableStrings.forEach((removableString) => {
|
||||
const regex = new RegExp(removableString, 'g')
|
||||
value = value.replace(regex, toReplace || '')
|
||||
|
@ -82,11 +87,11 @@ function removeStuff(
|
|||
}
|
||||
|
||||
// damn nonbreaking space
|
||||
function normalizeSpaces(input) {
|
||||
function normalizeSpaces(input: string): string {
|
||||
return input.replace(/\s/g, ' ')
|
||||
}
|
||||
|
||||
function removeUnnecesarySpaces(toremove: string) {
|
||||
function removeUnnecesarySpaces(toremove: string): string {
|
||||
return normalizeSpaces(toremove).replace(/\s+/g, ' ')
|
||||
}
|
||||
|
||||
|
@ -141,7 +146,7 @@ export function compareString(
|
|||
return percent
|
||||
}
|
||||
|
||||
function answerPreProcessor(value: string) {
|
||||
function answerPreProcessor(value: string): string {
|
||||
if (!value) {
|
||||
return value
|
||||
}
|
||||
|
@ -150,9 +155,9 @@ function answerPreProcessor(value: string) {
|
|||
}
|
||||
|
||||
// 'a. pécsi sör' -> 'pécsi sör'
|
||||
function removeAnswerLetters(value: string) {
|
||||
function removeAnswerLetters(value: string): string {
|
||||
if (!value) {
|
||||
return
|
||||
return value
|
||||
}
|
||||
|
||||
const val = value.split('. ')
|
||||
|
@ -164,9 +169,9 @@ function removeAnswerLetters(value: string) {
|
|||
}
|
||||
}
|
||||
|
||||
function simplifyQA(value: string, mods: Array<Function>) {
|
||||
function simplifyQA(value: string, mods: Array<Function>): string {
|
||||
if (!value) {
|
||||
return
|
||||
return value
|
||||
}
|
||||
|
||||
return mods.reduce((res, fn) => {
|
||||
|
@ -174,7 +179,7 @@ function simplifyQA(value: string, mods: Array<Function>) {
|
|||
}, value)
|
||||
}
|
||||
|
||||
function simplifyAnswer(value: string) {
|
||||
function simplifyAnswer(value: string): string {
|
||||
if (!value) {
|
||||
return value
|
||||
}
|
||||
|
@ -185,27 +190,30 @@ function simplifyAnswer(value: string) {
|
|||
])
|
||||
}
|
||||
|
||||
function simplifyQuestion(question: Question | string) {
|
||||
function simplifyQuestion(question: string): string {
|
||||
if (!question) {
|
||||
return
|
||||
}
|
||||
if (typeof question === 'string') {
|
||||
return simplifyQA(question, [removeUnnecesarySpaces, removeAnswerLetters])
|
||||
} else {
|
||||
if (question.Q) {
|
||||
question.Q = simplifyQA(question.Q, [
|
||||
removeUnnecesarySpaces,
|
||||
removeAnswerLetters,
|
||||
])
|
||||
}
|
||||
if (question.A) {
|
||||
question.A = simplifyQA(question.A, [
|
||||
removeUnnecesarySpaces,
|
||||
removeAnswerLetters,
|
||||
])
|
||||
}
|
||||
return question
|
||||
}
|
||||
return simplifyQA(question, [removeUnnecesarySpaces, removeAnswerLetters])
|
||||
}
|
||||
|
||||
function simplifyQuestionObj(question: Question): Question {
|
||||
if (!question) {
|
||||
return question
|
||||
}
|
||||
if (question.Q) {
|
||||
question.Q = simplifyQA(question.Q, [
|
||||
removeUnnecesarySpaces,
|
||||
removeAnswerLetters,
|
||||
])
|
||||
}
|
||||
if (question.A) {
|
||||
question.A = simplifyQA(question.A, [
|
||||
removeUnnecesarySpaces,
|
||||
removeAnswerLetters,
|
||||
])
|
||||
}
|
||||
return question
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
|
@ -241,10 +249,11 @@ function createQuestion(
|
|||
logger.Log('Error creating question', logger.GetColor('redbg'))
|
||||
console.error(question, answer, data)
|
||||
console.error(err)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
function compareImage(data: QuestionData, data2: QuestionData) {
|
||||
function compareImage(data: QuestionData, data2: QuestionData): number {
|
||||
if (data.hashedImages && data2.hashedImages) {
|
||||
return compareString(
|
||||
data.hashedImages.join(' '),
|
||||
|
@ -266,7 +275,7 @@ function compareImage(data: QuestionData, data2: QuestionData) {
|
|||
}
|
||||
}
|
||||
|
||||
function compareData(q1: Question, q2: Question) {
|
||||
function compareData(q1: Question, q2: Question): number {
|
||||
try {
|
||||
if (q1.data.type === q2.data.type) {
|
||||
const dataType = q1.data.type
|
||||
|
@ -294,7 +303,7 @@ function compareData(q1: Question, q2: Question) {
|
|||
return 0
|
||||
}
|
||||
|
||||
function compareQuestion(q1: Question, q2: Question) {
|
||||
function compareQuestion(q1: Question, q2: Question): number {
|
||||
return compareString(q1.Q, q1.cache.Q, q2.Q, q2.cache.Q)
|
||||
// return compareString(
|
||||
// q1.Q,
|
||||
|
@ -304,7 +313,7 @@ function compareQuestion(q1: Question, q2: Question) {
|
|||
// )
|
||||
}
|
||||
|
||||
function compareAnswer(q1: Question, q2: Question) {
|
||||
function compareAnswer(q1: Question, q2: Question): number {
|
||||
return compareString(q1.A, q1.cache.A, q2.A, q2.cache.A)
|
||||
// return compareString(
|
||||
// q1.A,
|
||||
|
@ -316,15 +325,10 @@ function compareAnswer(q1: Question, q2: Question) {
|
|||
|
||||
function compareQuestionObj(
|
||||
q1: Question,
|
||||
q1subjName: string,
|
||||
_q1subjName: string,
|
||||
q2: Question,
|
||||
q2subjName: string
|
||||
): any {
|
||||
assert(q1)
|
||||
assert(typeof q1 === 'object')
|
||||
assert(q2)
|
||||
assert(typeof q2 === 'object')
|
||||
|
||||
): DetailedMatch {
|
||||
const qMatch = compareQuestion(q1, q2)
|
||||
const aMatch = q2.A ? compareAnswer(q1, q2) : 0
|
||||
// -1 if botth questions are simple
|
||||
|
@ -354,7 +358,7 @@ function compareQuestionObj(
|
|||
}
|
||||
}
|
||||
|
||||
function questionToString(question: Question) {
|
||||
function questionToString(question: Question): string {
|
||||
const { Q, A, data } = question
|
||||
|
||||
if (data.type !== 'simple') {
|
||||
|
@ -372,10 +376,8 @@ function searchSubject(
|
|||
question: Question,
|
||||
subjName: string,
|
||||
searchTillMatchPercent?: number
|
||||
) {
|
||||
assert(question)
|
||||
|
||||
let result = []
|
||||
): SearchResultQuestion[] {
|
||||
let result: SearchResultQuestion[] = []
|
||||
|
||||
let stopSearch = false
|
||||
let i = subj.Questions.length - 1
|
||||
|
@ -416,10 +418,10 @@ function searchSubject(
|
|||
return result
|
||||
}
|
||||
|
||||
function subjectToString(subj: Subject) {
|
||||
function subjectToString(subj: Subject): string {
|
||||
const { Questions, Name } = subj
|
||||
|
||||
const result = []
|
||||
const result: string[] = []
|
||||
Questions.forEach((question) => {
|
||||
result.push(questionToString(question))
|
||||
})
|
||||
|
@ -437,23 +439,14 @@ function addQuestion(
|
|||
): void {
|
||||
logger.DebugLog('Adding new question with subjName: ' + subj, 'qdb add', 1)
|
||||
logger.DebugLog(question, 'qdb add', 3)
|
||||
assert(data)
|
||||
assert(subj)
|
||||
assert(question)
|
||||
assert(typeof question === 'object')
|
||||
|
||||
let i = 0
|
||||
// FIXME: this only adds to the first matched subject name. Check if this doesnt cause any bugs
|
||||
while (
|
||||
i < data.length &&
|
||||
!subj
|
||||
const i = data.findIndex((x) => {
|
||||
return !subj
|
||||
.toLowerCase()
|
||||
.includes(getSubjNameWithoutYear(data[i].Name).toLowerCase())
|
||||
) {
|
||||
i++
|
||||
}
|
||||
.includes(getSubjNameWithoutYear(x.Name).toLowerCase())
|
||||
})
|
||||
|
||||
if (i < data.length) {
|
||||
if (i !== -1) {
|
||||
logger.DebugLog('Adding new question to existing subject', 'qdb add', 1)
|
||||
data[i].Questions.push(question)
|
||||
} else {
|
||||
|
@ -465,34 +458,12 @@ function addQuestion(
|
|||
}
|
||||
}
|
||||
|
||||
function prepareQuestion(
|
||||
question: string | Question,
|
||||
data: string | QuestionData
|
||||
): any {
|
||||
let preparedQuestion: Question
|
||||
|
||||
if (typeof question === 'object') {
|
||||
preparedQuestion = createQuestion(question)
|
||||
} else {
|
||||
let parsedData: any
|
||||
if (typeof data === 'string') {
|
||||
try {
|
||||
parsedData = JSON.parse(data)
|
||||
} catch (err) {
|
||||
// asd
|
||||
}
|
||||
}
|
||||
if (typeof data === 'object') {
|
||||
parsedData = data
|
||||
}
|
||||
preparedQuestion = createQuestion(question, null, parsedData)
|
||||
}
|
||||
|
||||
return simplifyQuestion(preparedQuestion)
|
||||
function prepareQuestion(question: Question): Question {
|
||||
return simplifyQuestionObj(createQuestion(question))
|
||||
}
|
||||
|
||||
function dataToString(data: Array<Subject>): string {
|
||||
const result = []
|
||||
const result: string[] = []
|
||||
data.forEach((subj) => {
|
||||
result.push(subjectToString(subj))
|
||||
})
|
||||
|
@ -502,16 +473,13 @@ function dataToString(data: Array<Subject>): string {
|
|||
function doSearch(
|
||||
data: Array<Subject>,
|
||||
subjName: string,
|
||||
question: Question | string,
|
||||
questionData?: QuestionData,
|
||||
question: Question,
|
||||
searchTillMatchPercent?: number,
|
||||
searchInAllIfNoResult?: Boolean
|
||||
): any {
|
||||
let result = []
|
||||
): SearchResultQuestion[] {
|
||||
let result: SearchResultQuestion[] = []
|
||||
|
||||
const questionToSearch = prepareQuestion(question, questionData)
|
||||
|
||||
assert(questionToSearch.data)
|
||||
const questionToSearch = prepareQuestion(question)
|
||||
|
||||
data.every((subj) => {
|
||||
if (
|
||||
|
@ -569,7 +537,7 @@ function doSearch(
|
|||
}
|
||||
|
||||
result = setNoPossibleAnswersPenalties(
|
||||
questionToSearch.possibleAnswers,
|
||||
questionToSearch.data.possibleAnswers,
|
||||
result
|
||||
)
|
||||
|
||||
|
@ -587,8 +555,8 @@ function doSearch(
|
|||
}
|
||||
|
||||
function setNoPossibleAnswersPenalties(
|
||||
possibleAnswers: any,
|
||||
result: any[]
|
||||
possibleAnswers: QuestionData['possibleAnswers'],
|
||||
result: SearchResultQuestion[]
|
||||
): any {
|
||||
if (!Array.isArray(possibleAnswers)) {
|
||||
return result
|
||||
|
@ -602,7 +570,8 @@ function setNoPossibleAnswersPenalties(
|
|||
const updated = result.map((result) => {
|
||||
const hasMatch = result.q.data.possibleAnswers.some((possibleAnswer) => {
|
||||
return possibleAnswers.some((questionPossibleAnswer) => {
|
||||
return questionPossibleAnswer.includes(questionPossibleAnswer)
|
||||
// FIXME: this could be object: questionPossibleAnswer
|
||||
return questionPossibleAnswer.text.includes(possibleAnswer.text)
|
||||
})
|
||||
})
|
||||
if (hasMatch) {
|
||||
|
@ -626,12 +595,24 @@ function setNoPossibleAnswersPenalties(
|
|||
// Multi threaded stuff
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
interface WorkData {
|
||||
subjName: string
|
||||
question: Question
|
||||
searchTillMatchPercent: number
|
||||
searchInAllIfNoResult: boolean
|
||||
searchIn: number[]
|
||||
index: number
|
||||
}
|
||||
|
||||
if (!isMainThread) {
|
||||
// os.setPriority(10)
|
||||
// logger.Log(`Worker thread priority set to ${os.getPriority()}`)
|
||||
|
||||
const { workerIndex } = workerData
|
||||
let qdbs: Array<QuestionDb> = workerData.initData
|
||||
const {
|
||||
workerIndex,
|
||||
initData,
|
||||
}: { workerIndex: number; initData: Array<QuestionDb> } = workerData
|
||||
let qdbs: Array<QuestionDb> = initData
|
||||
|
||||
logger.Log(
|
||||
`[THREAD #${workerIndex}]: Worker ${workerIndex} reporting for duty`
|
||||
|
@ -642,30 +623,21 @@ if (!isMainThread) {
|
|||
const {
|
||||
subjName,
|
||||
question,
|
||||
questionData,
|
||||
searchTillMatchPercent,
|
||||
searchInAllIfNoResult,
|
||||
searchIn,
|
||||
index,
|
||||
} = msg.data
|
||||
}: WorkData = msg.data
|
||||
|
||||
// console.log(
|
||||
// `[THREAD #${workerIndex}]: staring work${
|
||||
// !isNaN(index) ? ` on job index #${index}` : ''
|
||||
// }`
|
||||
// )
|
||||
|
||||
let searchResult = []
|
||||
let searchResult: SearchResultQuestion[] = []
|
||||
|
||||
try {
|
||||
qdbs.forEach((qdb) => {
|
||||
// FIXME: === 0?
|
||||
if (searchIn.length === 0 || searchIn.includes(qdb.index)) {
|
||||
if (searchIn.includes(qdb.index)) {
|
||||
const res = doSearch(
|
||||
qdb.data,
|
||||
subjName,
|
||||
question,
|
||||
questionData,
|
||||
searchTillMatchPercent,
|
||||
searchInAllIfNoResult
|
||||
)
|
||||
|
@ -686,34 +658,39 @@ if (!isMainThread) {
|
|||
} catch (err) {
|
||||
logger.Log('Error in worker thread!', logger.GetColor('redbg'))
|
||||
console.error(err)
|
||||
console.error('subjName', subjName)
|
||||
console.error('question', question)
|
||||
console.error('questionData', questionData)
|
||||
console.error('searchTillMatchPercent', searchTillMatchPercent)
|
||||
console.error('searchInAllIfNoResult', searchInAllIfNoResult)
|
||||
console.error('searchIn', searchIn)
|
||||
console.error('index', index)
|
||||
console.error({
|
||||
subjName: subjName,
|
||||
question: question,
|
||||
searchTillMatchPercent: searchTillMatchPercent,
|
||||
searchInAllIfNoResult: searchInAllIfNoResult,
|
||||
searchIn: searchIn,
|
||||
index: index,
|
||||
})
|
||||
}
|
||||
|
||||
// sorting
|
||||
const sortedResult = searchResult.sort((q1, q2) => {
|
||||
if (q1.match < q2.match) {
|
||||
return 1
|
||||
} else if (q1.match > q2.match) {
|
||||
return -1
|
||||
} else {
|
||||
return 0
|
||||
const sortedResult: SearchResultQuestion[] = searchResult.sort(
|
||||
(q1, q2) => {
|
||||
if (q1.match < q2.match) {
|
||||
return 1
|
||||
} else if (q1.match > q2.match) {
|
||||
return -1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
// ONDONE:
|
||||
parentPort.postMessage({
|
||||
const workerResult: WorkerResult = {
|
||||
msg: `From thread #${workerIndex}: job ${
|
||||
!isNaN(index) ? `#${index}` : ''
|
||||
}done`,
|
||||
workerIndex: workerIndex,
|
||||
result: sortedResult,
|
||||
})
|
||||
}
|
||||
|
||||
// ONDONE:
|
||||
parentPort.postMessage(workerResult)
|
||||
|
||||
// console.log(
|
||||
// `[THREAD #${workerIndex}]: Work ${
|
||||
|
@ -721,7 +698,7 @@ if (!isMainThread) {
|
|||
// }done!`
|
||||
// )
|
||||
} else if (msg.type === 'dbEdit') {
|
||||
const { dbIndex, edits } = msg.data
|
||||
const { dbIndex, edits }: { dbIndex: number; edits: Edits } = msg.data
|
||||
const { resultDb } = editDb(qdbs[dbIndex], edits)
|
||||
qdbs[dbIndex] = resultDb
|
||||
logger.DebugLog(`Worker db edit ${workerIndex}`, 'worker update', 1)
|
||||
|
@ -731,7 +708,16 @@ if (!isMainThread) {
|
|||
workerIndex: workerIndex,
|
||||
})
|
||||
} else if (msg.type === 'newQuestions') {
|
||||
const { subjName, qdbIndex, newQuestions } = msg.data
|
||||
const {
|
||||
subjName,
|
||||
qdbIndex,
|
||||
newQuestions,
|
||||
}: {
|
||||
subjName: string
|
||||
qdbIndex: number
|
||||
newQuestions: Question[]
|
||||
} = msg.data
|
||||
|
||||
let added = false
|
||||
qdbs = qdbs.map((qdb) => {
|
||||
if (qdb.index === qdbIndex) {
|
||||
|
@ -781,7 +767,8 @@ if (!isMainThread) {
|
|||
|
||||
// console.log(`[THREAD #${workerIndex}]: update`)
|
||||
} else if (msg.type === 'newdb') {
|
||||
qdbs.push(msg.newdb)
|
||||
const { data }: { data: QuestionDb } = msg
|
||||
qdbs.push(data)
|
||||
|
||||
parentPort.postMessage({
|
||||
msg: `From thread #${workerIndex}: new db add done`,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue