mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Declassified classes.js
This commit is contained in:
parent
bc0f77dc36
commit
db1976ecf3
2 changed files with 377 additions and 457 deletions
|
@ -16,6 +16,6 @@ module.exports = {
|
|||
eqeqeq: ['warn', 'smart'],
|
||||
'no-unused-vars': 'warn',
|
||||
'no-prototype-builtins': 'off',
|
||||
'id-length': ['warn', { exceptions: ['i', 'j'] }],
|
||||
'id-length': ['warn', { exceptions: ['i', 'j', 't', 'Q', 'A'] }],
|
||||
},
|
||||
}
|
||||
|
|
|
@ -31,8 +31,13 @@ const assert = (val) => {
|
|||
}
|
||||
}
|
||||
|
||||
class StringUtils {
|
||||
GetSubjNameWithoutYear(subjName) {
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
// String Utils
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
// Exported
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
function getSubjNameWithoutYear(subjName) {
|
||||
let t = subjName.split(' - ')
|
||||
if (t[0].match(/^[0-9]{4}\/[0-9]{2}\/[0-9]{1}$/i)) {
|
||||
return t[1] || subjName
|
||||
|
@ -41,7 +46,9 @@ class StringUtils {
|
|||
}
|
||||
}
|
||||
|
||||
RemoveStuff(value, removableStrings, toReplace) {
|
||||
// Not exported
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
function removeStuff(value, removableStrings, toReplace) {
|
||||
removableStrings.forEach((removableString) => {
|
||||
var regex = new RegExp(removableString, 'g')
|
||||
value = value.replace(regex, toReplace || '')
|
||||
|
@ -49,42 +56,11 @@ class StringUtils {
|
|||
return value
|
||||
}
|
||||
|
||||
SimplifyQuery(question) {
|
||||
assert(question)
|
||||
|
||||
var result = question.replace(/\n/g, ' ').replace(/\s/g, ' ')
|
||||
return this.RemoveUnnecesarySpaces(result)
|
||||
}
|
||||
|
||||
ShortenString(toShorten, ammount) {
|
||||
assert(toShorten)
|
||||
|
||||
var result = ''
|
||||
var i = 0
|
||||
while (i < toShorten.length && i < ammount) {
|
||||
result += toShorten[i]
|
||||
i++
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
ReplaceCharsWithSpace(val, char) {
|
||||
assert(val)
|
||||
assert(char)
|
||||
|
||||
var toremove = this.NormalizeSpaces(val)
|
||||
|
||||
var regex = new RegExp(char, 'g')
|
||||
toremove = toremove.replace(regex, ' ')
|
||||
|
||||
return this.RemoveUnnecesarySpaces(toremove)
|
||||
}
|
||||
|
||||
// removes whitespace from begining and and, and replaces multiple spaces with one space
|
||||
RemoveUnnecesarySpaces(toremove) {
|
||||
function removeUnnecesarySpaces(toremove) {
|
||||
assert(toremove)
|
||||
|
||||
toremove = this.NormalizeSpaces(toremove)
|
||||
toremove = normalizeSpaces(toremove)
|
||||
while (toremove.includes(' ')) {
|
||||
toremove = toremove.replace(/ {2}/g, ' ')
|
||||
}
|
||||
|
@ -92,41 +68,27 @@ class StringUtils {
|
|||
}
|
||||
|
||||
// simplifies a string for easier comparison
|
||||
SimplifyStringForComparison(value) {
|
||||
function simplifyStringForComparison(value) {
|
||||
assert(value)
|
||||
|
||||
value = this.RemoveUnnecesarySpaces(value).toLowerCase()
|
||||
return this.RemoveStuff(value, commonUselessStringParts)
|
||||
value = removeUnnecesarySpaces(value).toLowerCase()
|
||||
return removeStuff(value, commonUselessStringParts)
|
||||
}
|
||||
|
||||
RemoveSpecialChars(value) {
|
||||
function removeSpecialChars(value) {
|
||||
assert(value)
|
||||
|
||||
return this.RemoveStuff(value, specialChars, ' ')
|
||||
}
|
||||
|
||||
// if the value is empty, or whitespace
|
||||
EmptyOrWhiteSpace(value) {
|
||||
// replaces /n-s with "". then replaces spaces with "". if it equals "", then its empty, or only consists of white space
|
||||
if (value === undefined) {
|
||||
return true
|
||||
}
|
||||
return (
|
||||
value
|
||||
.replace(/\n/g, '')
|
||||
.replace(/ /g, '')
|
||||
.replace(/\s/g, ' ') === ''
|
||||
)
|
||||
return removeStuff(value, specialChars, ' ')
|
||||
}
|
||||
|
||||
// damn nonbreaking space
|
||||
NormalizeSpaces(input) {
|
||||
function normalizeSpaces(input) {
|
||||
assert(input)
|
||||
|
||||
return input.replace(/\s/g, ' ')
|
||||
}
|
||||
|
||||
CompareString(s1, s2) {
|
||||
function compareString(s1, s2) {
|
||||
if (!s1 || !s2) {
|
||||
if (!s1 && !s2) {
|
||||
return 100
|
||||
|
@ -135,8 +97,8 @@ class StringUtils {
|
|||
}
|
||||
}
|
||||
|
||||
s1 = this.SimplifyStringForComparison(s1).split(' ')
|
||||
s2 = this.SimplifyStringForComparison(s2).split(' ')
|
||||
s1 = simplifyStringForComparison(s1).split(' ')
|
||||
s2 = simplifyStringForComparison(s2).split(' ')
|
||||
var match = 0
|
||||
for (var i = 0; i < s1.length; i++) {
|
||||
if (s2.includes(s1[i])) {
|
||||
|
@ -152,14 +114,14 @@ class StringUtils {
|
|||
return percent
|
||||
}
|
||||
|
||||
AnswerPreProcessor(value) {
|
||||
function answerPreProcessor(value) {
|
||||
assert(value)
|
||||
|
||||
return this.RemoveStuff(value, commonUselessAnswerParts)
|
||||
return removeStuff(value, commonUselessAnswerParts)
|
||||
}
|
||||
|
||||
// 'a. pécsi sör' -> 'pécsi sör'
|
||||
RemoveAnswerLetters(value) {
|
||||
function removeAnswerLetters(value) {
|
||||
assert(value)
|
||||
|
||||
let val = value.split('. ')
|
||||
|
@ -171,7 +133,7 @@ class StringUtils {
|
|||
}
|
||||
}
|
||||
|
||||
SimplifyQA(value, mods) {
|
||||
function simplifyQA(value, mods) {
|
||||
if (!value) {
|
||||
return
|
||||
}
|
||||
|
@ -183,82 +145,61 @@ class StringUtils {
|
|||
return mods.reduce(reducer, value)
|
||||
}
|
||||
|
||||
SimplifyAnswer(value) {
|
||||
return this.SimplifyQA(value, [
|
||||
this.RemoveSpecialChars.bind(this),
|
||||
this.RemoveUnnecesarySpaces.bind(this),
|
||||
this.AnswerPreProcessor.bind(this),
|
||||
this.RemoveAnswerLetters.bind(this),
|
||||
// TODO: simplify answer before setting
|
||||
function simplifyAnswer(value) {
|
||||
return simplifyQA(value, [
|
||||
removeSpecialChars.bind(this),
|
||||
removeUnnecesarySpaces.bind(this),
|
||||
answerPreProcessor.bind(this),
|
||||
removeAnswerLetters.bind(this),
|
||||
])
|
||||
}
|
||||
|
||||
SimplifyQuestion(value) {
|
||||
return this.SimplifyQA(value, [
|
||||
this.RemoveSpecialChars.bind(this),
|
||||
this.RemoveUnnecesarySpaces.bind(this),
|
||||
function simplifyQuestion(question) {
|
||||
if (typeof q === 'string') {
|
||||
return simplifyQA(question, [
|
||||
removeSpecialChars.bind(this),
|
||||
removeUnnecesarySpaces.bind(this),
|
||||
])
|
||||
}
|
||||
|
||||
SimplifyStack(stack) {
|
||||
return this.SimplifyQuery(stack)
|
||||
}
|
||||
}
|
||||
|
||||
const SUtils = new StringUtils()
|
||||
|
||||
class Question {
|
||||
constructor(question, answer, data) {
|
||||
this.Q = SUtils.SimplifyQuestion(question)
|
||||
this.A = SUtils.SimplifyAnswer(answer)
|
||||
this.data = { ...data }
|
||||
}
|
||||
|
||||
toString() {
|
||||
if (this.data.type !== 'simple') {
|
||||
return '?' + this.Q + '\n!' + this.A + '\n>' + JSON.stringify(this.data)
|
||||
} else {
|
||||
return '?' + this.Q + '\n!' + this.A
|
||||
question.Q = simplifyQA(question.Q, [
|
||||
removeSpecialChars.bind(this),
|
||||
removeUnnecesarySpaces.bind(this),
|
||||
])
|
||||
question.A = simplifyQA(question.A, [
|
||||
removeSpecialChars.bind(this),
|
||||
removeUnnecesarySpaces.bind(this),
|
||||
])
|
||||
return question
|
||||
}
|
||||
}
|
||||
|
||||
HasQuestion() {
|
||||
return this.Q !== undefined
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
// Question
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
function createQuestion(question, answer, data) {
|
||||
return {
|
||||
Q: simplifyQuestion(question),
|
||||
A: simplifyAnswer(answer),
|
||||
data,
|
||||
}
|
||||
}
|
||||
|
||||
HasAnswer() {
|
||||
return this.A !== undefined
|
||||
function compareImage(data, data2) {
|
||||
return compareString(data.images.join(' '), data2.images.join(' '))
|
||||
}
|
||||
|
||||
HasImage() {
|
||||
return this.data.type === 'image'
|
||||
}
|
||||
|
||||
IsComplete() {
|
||||
return this.HasQuestion() && this.HasAnswer()
|
||||
}
|
||||
|
||||
CompareImage(data2) {
|
||||
return SUtils.CompareString(
|
||||
this.data.images.join(' '),
|
||||
data2.images.join(' ')
|
||||
)
|
||||
}
|
||||
|
||||
// returns -1 if botth is simple
|
||||
CompareData(qObj) {
|
||||
function compareData(data, qObj) {
|
||||
try {
|
||||
if (qObj.data.type === this.data.type) {
|
||||
if (qObj.data.type === data.type) {
|
||||
let dataType = qObj.data.type
|
||||
if (dataType === 'simple') {
|
||||
return -1
|
||||
} else if (dataType === 'image') {
|
||||
return this.CompareImage(qObj.data)
|
||||
return compareImage(qObj.data)
|
||||
} else {
|
||||
debugLog(
|
||||
`Unhandled data type ${dataType}`,
|
||||
'Compare question data',
|
||||
1
|
||||
)
|
||||
debugLog(`Unhandled data type ${dataType}`, 'Compare question data', 1)
|
||||
debugLog(qObj, 'Compare question data', 2)
|
||||
}
|
||||
} else {
|
||||
|
@ -272,16 +213,19 @@ class Question {
|
|||
return 0
|
||||
}
|
||||
|
||||
CompareQuestion(qObj) {
|
||||
return SUtils.CompareString(this.Q, qObj.Q)
|
||||
function compareQuestion(q1, q2) {
|
||||
return compareString(q1.Q, q2.Q)
|
||||
}
|
||||
|
||||
CompareAnswer(qObj) {
|
||||
return SUtils.CompareString(this.A, qObj.A)
|
||||
function compareAnswer(q1, q2) {
|
||||
return compareString(q1.A, q2.A)
|
||||
}
|
||||
|
||||
Compare(q2, data) {
|
||||
function compareQuestionObj(q1, q2, data) {
|
||||
assert(data)
|
||||
assert(q1)
|
||||
assert(q2)
|
||||
assert(typeof q2 === 'object')
|
||||
let qObj
|
||||
|
||||
if (typeof q2 === 'string') {
|
||||
|
@ -293,10 +237,10 @@ class Question {
|
|||
qObj = q2
|
||||
}
|
||||
|
||||
const qMatch = this.CompareQuestion(qObj)
|
||||
const aMatch = this.CompareAnswer(qObj)
|
||||
const qMatch = compareQuestion(q1, qObj.Q)
|
||||
const aMatch = compareAnswer(q1.A, qObj.A)
|
||||
// -1 if botth questions are simple
|
||||
const dMatch = this.CompareData(qObj)
|
||||
const dMatch = compareData(q1.data, qObj.data)
|
||||
|
||||
let avg = -1
|
||||
if (qObj.A) {
|
||||
|
@ -320,156 +264,123 @@ class Question {
|
|||
avg: avg,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Subject {
|
||||
constructor(name) {
|
||||
assert(name)
|
||||
function questionToString(question) {
|
||||
const { Q, A, data } = question
|
||||
|
||||
this.Name = name
|
||||
this.Questions = []
|
||||
}
|
||||
|
||||
setIndex(i) {
|
||||
this.index = i
|
||||
}
|
||||
|
||||
getIndex() {
|
||||
return this.index
|
||||
}
|
||||
|
||||
get length() {
|
||||
return this.Questions.length
|
||||
}
|
||||
|
||||
AddQuestion(question) {
|
||||
assert(question)
|
||||
|
||||
this.Questions.push(question)
|
||||
}
|
||||
|
||||
getSubjNameWithoutYear() {
|
||||
return SUtils.GetSubjNameWithoutYear(this.Name)
|
||||
}
|
||||
|
||||
getYear() {
|
||||
let t = this.Name.split(' - ')[0]
|
||||
if (t.match(/^[0-9]{4}\/[0-9]{2}\/[0-9]{1}$/i)) {
|
||||
return t
|
||||
if (data.type !== 'simple') {
|
||||
return '?' + Q + '\n!' + A + '\n>' + JSON.stringify(data)
|
||||
} else {
|
||||
return ''
|
||||
return '?' + Q + '\n!' + A
|
||||
}
|
||||
}
|
||||
|
||||
Search(question, data) {
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
// Subject
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
function searchQuestion(questions, question, questionData) {
|
||||
assert(question)
|
||||
|
||||
var result = []
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
let percent = this.Questions[i].Compare(question, data)
|
||||
questions.forEach((currentQuestion) => {
|
||||
let percent = compareQuestionObj(currentQuestion, question, questionData)
|
||||
if (percent.avg > minMatchAmmount) {
|
||||
result.push({
|
||||
question: this.Questions[i],
|
||||
question: currentQuestion,
|
||||
match: percent.avg,
|
||||
detailedMatch: percent,
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// TODO: check if sorting is correct!
|
||||
result.sort((q1, q2) => {
|
||||
return q1.match < q2.match
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
for (let i = 0; i < result.length; i++) {
|
||||
for (var j = i; j < result.length; j++) {
|
||||
if (result[i].match < result[j].match) {
|
||||
var tmp = result[i]
|
||||
result[i] = result[j]
|
||||
result[j] = tmp
|
||||
function subjectToString(subj) {
|
||||
const { Questions, Name } = subj
|
||||
|
||||
var result = []
|
||||
Questions.forEach((question) => {
|
||||
result.push(questionToString(question))
|
||||
})
|
||||
|
||||
return '+' + Name + '\n' + result.join('\n')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
// QuestionDB
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
function addQuestion(data, subj, question) {
|
||||
debugLog('Adding new question with subjName: ' + subj, 'qdb add', 1)
|
||||
debugLog(question, 'qdb add', 3)
|
||||
assert(data)
|
||||
assert(subj)
|
||||
assert(question)
|
||||
assert(typeof question === 'object')
|
||||
let result = []
|
||||
|
||||
var i = 0
|
||||
while (
|
||||
i < data.length &&
|
||||
!subj.toLowerCase().includes(getSubjNameWithoutYear(data[i]).toLowerCase())
|
||||
) {
|
||||
i++
|
||||
}
|
||||
|
||||
if (i < data.length) {
|
||||
debugLog('Adding new question to existing subject', 'qdb add', 1)
|
||||
result = [...data]
|
||||
result[i].Questions = {
|
||||
...data[i].Questions,
|
||||
question,
|
||||
}
|
||||
} else {
|
||||
debugLog('Creating new subject for question', 'qdb add', 1)
|
||||
result = [
|
||||
...data,
|
||||
{
|
||||
name: subj,
|
||||
Questions: [question],
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
toString() {
|
||||
var result = []
|
||||
for (var i = 0; i < this.Questions.length; i++) {
|
||||
result.push(this.Questions[i].toString())
|
||||
}
|
||||
return '+' + this.Name + '\n' + result.join('\n')
|
||||
}
|
||||
}
|
||||
|
||||
class QuestionDB {
|
||||
constructor() {
|
||||
this.Subjects = []
|
||||
}
|
||||
|
||||
get length() {
|
||||
return this.Subjects.length
|
||||
}
|
||||
|
||||
AddQuestion(subj, question) {
|
||||
debugLog('Adding new question with subjName: ' + subj, 'qdb add', 1)
|
||||
debugLog(question, 'qdb add', 3)
|
||||
assert(subj)
|
||||
|
||||
var i = 0
|
||||
while (
|
||||
i < this.Subjects.length &&
|
||||
!subj
|
||||
.toLowerCase()
|
||||
.includes(this.Subjects[i].getSubjNameWithoutYear().toLowerCase())
|
||||
) {
|
||||
i++
|
||||
}
|
||||
|
||||
if (i < this.Subjects.length) {
|
||||
debugLog('Adding new question to existing subject', 'qdb add', 1)
|
||||
this.Subjects[i].AddQuestion(question)
|
||||
} else {
|
||||
debugLog('Creating new subject for question', 'qdb add', 1)
|
||||
const newSubject = new Subject(subj)
|
||||
newSubject.AddQuestion(question)
|
||||
this.Subjects.push(newSubject)
|
||||
}
|
||||
}
|
||||
|
||||
SimplifyQuestion(question) {
|
||||
if (typeof q === 'string') {
|
||||
return SUtils.SimplifyQuestion(question)
|
||||
} else {
|
||||
question.Q = SUtils.SimplifyQuestion(question.Q)
|
||||
question.A = SUtils.SimplifyQuestion(question.A)
|
||||
return question
|
||||
}
|
||||
}
|
||||
|
||||
Search(question, subjName, data) {
|
||||
function searchData(data, question, subjName, questionData) {
|
||||
assert(data)
|
||||
assert(question)
|
||||
debugLog('Searching for question', 'qdb search', 1)
|
||||
debugLog('Question:', 'qdb search', 2)
|
||||
debugLog(question, 'qdb search', 2)
|
||||
debugLog(`Subject name: ${subjName}`, 'qdb search', 2)
|
||||
debugLog('Data:', 'qdb search', 2)
|
||||
debugLog(data || question.data, 'qdb search', 2)
|
||||
debugLog(questionData || question.data, 'qdb search', 2)
|
||||
|
||||
if (!data) {
|
||||
data = question.data || { type: 'simple' }
|
||||
if (!questionData) {
|
||||
questionData = question.data || { type: 'simple' }
|
||||
}
|
||||
if (!subjName) {
|
||||
subjName = ''
|
||||
debugLog('No subject name as param!', 'qdb search', 1)
|
||||
}
|
||||
question = this.SimplifyQuestion(question)
|
||||
question = simplifyQuestion(question)
|
||||
|
||||
var result = []
|
||||
this.Subjects.forEach((subj) => {
|
||||
let result = []
|
||||
data.forEach((subj) => {
|
||||
if (
|
||||
subjName
|
||||
.toLowerCase()
|
||||
.includes(subj.getSubjNameWithoutYear().toLowerCase())
|
||||
.includes(getSubjNameWithoutYear(subj.Name).toLowerCase())
|
||||
) {
|
||||
debugLog(`Searching in ${subj.Name} `, 2)
|
||||
result = result.concat(subj.Search(question, data))
|
||||
result = result.concat(subj.Search(question, questionData))
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -480,8 +391,10 @@ class QuestionDB {
|
|||
'qdb search',
|
||||
1
|
||||
)
|
||||
this.Subjects.forEach((subj) => {
|
||||
result = result.concat(subj.Search(question, data))
|
||||
data.forEach((subj) => {
|
||||
result = result.concat(
|
||||
searchQuestion(subj.Questions, question, questionData)
|
||||
)
|
||||
})
|
||||
if (result.length > 0) {
|
||||
debugLog(
|
||||
|
@ -493,48 +406,55 @@ class QuestionDB {
|
|||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < result.length; i++) {
|
||||
for (var j = i; j < result.length; j++) {
|
||||
if (result[i].match < result[j].match) {
|
||||
var tmp = result[i]
|
||||
result[i] = result[j]
|
||||
result[j] = tmp
|
||||
}
|
||||
}
|
||||
}
|
||||
// TODO: check if sorting is correct!
|
||||
result.sort((q1, q2) => {
|
||||
return q1.match < q2.match
|
||||
})
|
||||
|
||||
debugLog(`QDB search result length: ${result.length}`, 'qdb search', 1)
|
||||
return result
|
||||
}
|
||||
|
||||
AddSubject(subj) {
|
||||
function addSubject(data, subj) {
|
||||
assert(data)
|
||||
assert(subj)
|
||||
|
||||
var i = 0
|
||||
while (i < this.length && subj.Name !== this.Subjects[i].Name) {
|
||||
while (i < length && subj.Name !== data[i].Name) {
|
||||
i++
|
||||
}
|
||||
|
||||
if (i < this.length) {
|
||||
this.Subjects.concat(subj.Questions)
|
||||
if (i < length) {
|
||||
return data.map((currSubj, j) => {
|
||||
if (j === i) {
|
||||
return {
|
||||
...currSubj,
|
||||
Questions: [...currSubj.Questions, ...subj.Questions],
|
||||
}
|
||||
} else {
|
||||
this.Subjects.push(subj)
|
||||
return currSubj
|
||||
}
|
||||
})
|
||||
} else {
|
||||
return [...data, subj]
|
||||
}
|
||||
}
|
||||
|
||||
toString() {
|
||||
function dataToString(data) {
|
||||
var result = []
|
||||
for (var i = 0; i < this.Subjects.length; i++) {
|
||||
result.push(this.Subjects[i].toString())
|
||||
}
|
||||
data.forEach((subj) => {
|
||||
result.push(subjectToString(subj))
|
||||
})
|
||||
return result.join('\n\n')
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.StringUtils = StringUtils // TODO: export singleton string utils, remove nea StringUtils from other files
|
||||
module.exports.SUtils = SUtils
|
||||
module.exports.Question = Question
|
||||
module.exports.Subject = Subject
|
||||
module.exports.QuestionDB = QuestionDB
|
||||
module.exports.minMatchAmmount = minMatchAmmount
|
||||
module.exports.initLogger = initLogger
|
||||
module.exports = {
|
||||
minMatchAmmount,
|
||||
initLogger,
|
||||
getSubjNameWithoutYear,
|
||||
createQuestion,
|
||||
addQuestion,
|
||||
addSubject,
|
||||
searchData,
|
||||
dataToString,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue