Declassified classes.js

This commit is contained in:
mrfry 2020-10-02 13:18:43 +02:00
parent bc0f77dc36
commit db1976ecf3
2 changed files with 377 additions and 457 deletions

View file

@ -16,6 +16,6 @@ module.exports = {
eqeqeq: ['warn', 'smart'], eqeqeq: ['warn', 'smart'],
'no-unused-vars': 'warn', 'no-unused-vars': 'warn',
'no-prototype-builtins': 'off', 'no-prototype-builtins': 'off',
'id-length': ['warn', { exceptions: ['i', 'j'] }], 'id-length': ['warn', { exceptions: ['i', 'j', 't', 'Q', 'A'] }],
}, },
} }

View file

@ -31,102 +31,64 @@ const assert = (val) => {
} }
} }
class StringUtils { // ---------------------------------------------------------------------------------------------------------
GetSubjNameWithoutYear(subjName) { // String Utils
// ---------------------------------------------------------------------------------------------------------
// Exported
// ---------------------------------------------------------------------------------------------------------
function getSubjNameWithoutYear(subjName) {
let t = subjName.split(' - ') let t = subjName.split(' - ')
if (t[0].match(/^[0-9]{4}\/[0-9]{2}\/[0-9]{1}$/i)) { if (t[0].match(/^[0-9]{4}\/[0-9]{2}\/[0-9]{1}$/i)) {
return t[1] || subjName return t[1] || subjName
} else { } else {
return subjName return subjName
} }
} }
RemoveStuff(value, removableStrings, toReplace) { // Not exported
// ---------------------------------------------------------------------------------------------------------
function removeStuff(value, removableStrings, toReplace) {
removableStrings.forEach((removableString) => { removableStrings.forEach((removableString) => {
var regex = new RegExp(removableString, 'g') var regex = new RegExp(removableString, 'g')
value = value.replace(regex, toReplace || '') value = value.replace(regex, toReplace || '')
}) })
return value return value
} }
SimplifyQuery(question) { // removes whitespace from begining and and, and replaces multiple spaces with one space
assert(question) function removeUnnecesarySpaces(toremove) {
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) {
assert(toremove) assert(toremove)
toremove = this.NormalizeSpaces(toremove) toremove = normalizeSpaces(toremove)
while (toremove.includes(' ')) { while (toremove.includes(' ')) {
toremove = toremove.replace(/ {2}/g, ' ') toremove = toremove.replace(/ {2}/g, ' ')
} }
return toremove.trim() return toremove.trim()
} }
// simplifies a string for easier comparison // simplifies a string for easier comparison
SimplifyStringForComparison(value) { function simplifyStringForComparison(value) {
assert(value) assert(value)
value = this.RemoveUnnecesarySpaces(value).toLowerCase() value = removeUnnecesarySpaces(value).toLowerCase()
return this.RemoveStuff(value, commonUselessStringParts) return removeStuff(value, commonUselessStringParts)
} }
RemoveSpecialChars(value) { function removeSpecialChars(value) {
assert(value) assert(value)
return this.RemoveStuff(value, specialChars, ' ') return removeStuff(value, specialChars, ' ')
} }
// if the value is empty, or whitespace // damn nonbreaking space
EmptyOrWhiteSpace(value) { function normalizeSpaces(input) {
// 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, ' ') === ''
)
}
// damn nonbreaking space
NormalizeSpaces(input) {
assert(input) assert(input)
return input.replace(/\s/g, ' ') return input.replace(/\s/g, ' ')
} }
CompareString(s1, s2) { function compareString(s1, s2) {
if (!s1 || !s2) { if (!s1 || !s2) {
if (!s1 && !s2) { if (!s1 && !s2) {
return 100 return 100
@ -135,8 +97,8 @@ class StringUtils {
} }
} }
s1 = this.SimplifyStringForComparison(s1).split(' ') s1 = simplifyStringForComparison(s1).split(' ')
s2 = this.SimplifyStringForComparison(s2).split(' ') s2 = simplifyStringForComparison(s2).split(' ')
var match = 0 var match = 0
for (var i = 0; i < s1.length; i++) { for (var i = 0; i < s1.length; i++) {
if (s2.includes(s1[i])) { if (s2.includes(s1[i])) {
@ -150,16 +112,16 @@ class StringUtils {
percent = 0 percent = 0
} }
return percent return percent
} }
AnswerPreProcessor(value) { function answerPreProcessor(value) {
assert(value) assert(value)
return this.RemoveStuff(value, commonUselessAnswerParts) return removeStuff(value, commonUselessAnswerParts)
} }
// 'a. pécsi sör' -> 'pécsi sör' // 'a. pécsi sör' -> 'pécsi sör'
RemoveAnswerLetters(value) { function removeAnswerLetters(value) {
assert(value) assert(value)
let val = value.split('. ') let val = value.split('. ')
@ -169,9 +131,9 @@ class StringUtils {
} else { } else {
return value return value
} }
} }
SimplifyQA(value, mods) { function simplifyQA(value, mods) {
if (!value) { if (!value) {
return return
} }
@ -181,84 +143,63 @@ class StringUtils {
} }
return mods.reduce(reducer, value) return mods.reduce(reducer, value)
} }
SimplifyAnswer(value) { // TODO: simplify answer before setting
return this.SimplifyQA(value, [ function simplifyAnswer(value) {
this.RemoveSpecialChars.bind(this), return simplifyQA(value, [
this.RemoveUnnecesarySpaces.bind(this), removeSpecialChars.bind(this),
this.AnswerPreProcessor.bind(this), removeUnnecesarySpaces.bind(this),
this.RemoveAnswerLetters.bind(this), answerPreProcessor.bind(this),
removeAnswerLetters.bind(this),
]) ])
} }
SimplifyQuestion(value) { function simplifyQuestion(question) {
return this.SimplifyQA(value, [ if (typeof q === 'string') {
this.RemoveSpecialChars.bind(this), return simplifyQA(question, [
this.RemoveUnnecesarySpaces.bind(this), removeSpecialChars.bind(this),
removeUnnecesarySpaces.bind(this),
]) ])
} } else {
question.Q = simplifyQA(question.Q, [
SimplifyStack(stack) { removeSpecialChars.bind(this),
return this.SimplifyQuery(stack) removeUnnecesarySpaces.bind(this),
])
question.A = simplifyQA(question.A, [
removeSpecialChars.bind(this),
removeUnnecesarySpaces.bind(this),
])
return question
} }
} }
const SUtils = new StringUtils() // ---------------------------------------------------------------------------------------------------------
// Question
// ---------------------------------------------------------------------------------------------------------
class Question { function createQuestion(question, answer, data) {
constructor(question, answer, data) { return {
this.Q = SUtils.SimplifyQuestion(question) Q: simplifyQuestion(question),
this.A = SUtils.SimplifyAnswer(answer) A: simplifyAnswer(answer),
this.data = { ...data } data,
} }
}
toString() { function compareImage(data, data2) {
if (this.data.type !== 'simple') { return compareString(data.images.join(' '), data2.images.join(' '))
return '?' + this.Q + '\n!' + this.A + '\n>' + JSON.stringify(this.data) }
} else {
return '?' + this.Q + '\n!' + this.A
}
}
HasQuestion() { function compareData(data, qObj) {
return this.Q !== undefined
}
HasAnswer() {
return this.A !== undefined
}
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) {
try { try {
if (qObj.data.type === this.data.type) { if (qObj.data.type === data.type) {
let dataType = qObj.data.type let dataType = qObj.data.type
if (dataType === 'simple') { if (dataType === 'simple') {
return -1 return -1
} else if (dataType === 'image') { } else if (dataType === 'image') {
return this.CompareImage(qObj.data) return compareImage(qObj.data)
} else { } else {
debugLog( debugLog(`Unhandled data type ${dataType}`, 'Compare question data', 1)
`Unhandled data type ${dataType}`,
'Compare question data',
1
)
debugLog(qObj, 'Compare question data', 2) debugLog(qObj, 'Compare question data', 2)
} }
} else { } else {
@ -270,18 +211,21 @@ class Question {
debugLog(error, 'Compare question data', 2) debugLog(error, 'Compare question data', 2)
} }
return 0 return 0
} }
CompareQuestion(qObj) { function compareQuestion(q1, q2) {
return SUtils.CompareString(this.Q, qObj.Q) return compareString(q1.Q, q2.Q)
} }
CompareAnswer(qObj) { function compareAnswer(q1, q2) {
return SUtils.CompareString(this.A, qObj.A) return compareString(q1.A, q2.A)
} }
Compare(q2, data) { function compareQuestionObj(q1, q2, data) {
assert(data)
assert(q1)
assert(q2) assert(q2)
assert(typeof q2 === 'object')
let qObj let qObj
if (typeof q2 === 'string') { if (typeof q2 === 'string') {
@ -293,10 +237,10 @@ class Question {
qObj = q2 qObj = q2
} }
const qMatch = this.CompareQuestion(qObj) const qMatch = compareQuestion(q1, qObj.Q)
const aMatch = this.CompareAnswer(qObj) const aMatch = compareAnswer(q1.A, qObj.A)
// -1 if botth questions are simple // -1 if botth questions are simple
const dMatch = this.CompareData(qObj) const dMatch = compareData(q1.data, qObj.data)
let avg = -1 let avg = -1
if (qObj.A) { if (qObj.A) {
@ -319,157 +263,124 @@ class Question {
dMatch: dMatch, dMatch: dMatch,
avg: avg, avg: avg,
} }
}
function questionToString(question) {
const { Q, A, data } = question
if (data.type !== 'simple') {
return '?' + Q + '\n!' + A + '\n>' + JSON.stringify(data)
} else {
return '?' + Q + '\n!' + A
} }
} }
class Subject { // ---------------------------------------------------------------------------------------------------------
constructor(name) { // Subject
assert(name) // ---------------------------------------------------------------------------------------------------------
function searchQuestion(questions, question, questionData) {
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
} else {
return ''
}
}
Search(question, data) {
assert(question) assert(question)
var result = [] var result = []
for (let i = 0; i < this.length; i++) { questions.forEach((currentQuestion) => {
let percent = this.Questions[i].Compare(question, data) let percent = compareQuestionObj(currentQuestion, question, questionData)
if (percent.avg > minMatchAmmount) { if (percent.avg > minMatchAmmount) {
result.push({ result.push({
question: this.Questions[i], question: currentQuestion,
match: percent.avg, match: percent.avg,
detailedMatch: percent, detailedMatch: percent,
}) })
} }
} })
for (let i = 0; i < result.length; i++) { // TODO: check if sorting is correct!
for (var j = i; j < result.length; j++) { result.sort((q1, q2) => {
if (result[i].match < result[j].match) { return q1.match < q2.match
var tmp = result[i] })
result[i] = result[j]
result[j] = tmp
}
}
}
return result 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 { function subjectToString(subj) {
constructor() { const { Questions, Name } = subj
this.Subjects = []
}
get length() { var result = []
return this.Subjects.length Questions.forEach((question) => {
} result.push(questionToString(question))
})
AddQuestion(subj, question) { return '+' + Name + '\n' + result.join('\n')
}
// ---------------------------------------------------------------------------------------------------------
// QuestionDB
// ---------------------------------------------------------------------------------------------------------
function addQuestion(data, subj, question) {
debugLog('Adding new question with subjName: ' + subj, 'qdb add', 1) debugLog('Adding new question with subjName: ' + subj, 'qdb add', 1)
debugLog(question, 'qdb add', 3) debugLog(question, 'qdb add', 3)
assert(data)
assert(subj) assert(subj)
assert(question)
assert(typeof question === 'object')
let result = []
var i = 0 var i = 0
while ( while (
i < this.Subjects.length && i < data.length &&
!subj !subj.toLowerCase().includes(getSubjNameWithoutYear(data[i]).toLowerCase())
.toLowerCase()
.includes(this.Subjects[i].getSubjNameWithoutYear().toLowerCase())
) { ) {
i++ i++
} }
if (i < this.Subjects.length) { if (i < data.length) {
debugLog('Adding new question to existing subject', 'qdb add', 1) debugLog('Adding new question to existing subject', 'qdb add', 1)
this.Subjects[i].AddQuestion(question) result = [...data]
result[i].Questions = {
...data[i].Questions,
question,
}
} else { } else {
debugLog('Creating new subject for question', 'qdb add', 1) debugLog('Creating new subject for question', 'qdb add', 1)
const newSubject = new Subject(subj) result = [
newSubject.AddQuestion(question) ...data,
this.Subjects.push(newSubject) {
} name: subj,
Questions: [question],
},
]
} }
SimplifyQuestion(question) { return result
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) assert(question)
debugLog('Searching for question', 'qdb search', 1) debugLog('Searching for question', 'qdb search', 1)
debugLog('Question:', 'qdb search', 2) debugLog('Question:', 'qdb search', 2)
debugLog(question, 'qdb search', 2) debugLog(question, 'qdb search', 2)
debugLog(`Subject name: ${subjName}`, 'qdb search', 2) debugLog(`Subject name: ${subjName}`, 'qdb search', 2)
debugLog('Data:', 'qdb search', 2) debugLog('Data:', 'qdb search', 2)
debugLog(data || question.data, 'qdb search', 2) debugLog(questionData || question.data, 'qdb search', 2)
if (!data) { if (!questionData) {
data = question.data || { type: 'simple' } questionData = question.data || { type: 'simple' }
} }
if (!subjName) { if (!subjName) {
subjName = '' subjName = ''
debugLog('No subject name as param!', 'qdb search', 1) debugLog('No subject name as param!', 'qdb search', 1)
} }
question = this.SimplifyQuestion(question) question = simplifyQuestion(question)
var result = [] let result = []
this.Subjects.forEach((subj) => { data.forEach((subj) => {
if ( if (
subjName subjName
.toLowerCase() .toLowerCase()
.includes(subj.getSubjNameWithoutYear().toLowerCase()) .includes(getSubjNameWithoutYear(subj.Name).toLowerCase())
) { ) {
debugLog(`Searching in ${subj.Name} `, 2) 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', 'qdb search',
1 1
) )
this.Subjects.forEach((subj) => { data.forEach((subj) => {
result = result.concat(subj.Search(question, data)) result = result.concat(
searchQuestion(subj.Questions, question, questionData)
)
}) })
if (result.length > 0) { if (result.length > 0) {
debugLog( debugLog(
@ -493,48 +406,55 @@ class QuestionDB {
} }
} }
for (let i = 0; i < result.length; i++) { // TODO: check if sorting is correct!
for (var j = i; j < result.length; j++) { result.sort((q1, q2) => {
if (result[i].match < result[j].match) { return q1.match < q2.match
var tmp = result[i] })
result[i] = result[j]
result[j] = tmp
}
}
}
debugLog(`QDB search result length: ${result.length}`, 'qdb search', 1) debugLog(`QDB search result length: ${result.length}`, 'qdb search', 1)
return result return result
} }
AddSubject(subj) { function addSubject(data, subj) {
assert(data)
assert(subj) assert(subj)
var i = 0 var i = 0
while (i < this.length && subj.Name !== this.Subjects[i].Name) { while (i < length && subj.Name !== data[i].Name) {
i++ i++
} }
if (i < this.length) { if (i < length) {
this.Subjects.concat(subj.Questions) return data.map((currSubj, j) => {
if (j === i) {
return {
...currSubj,
Questions: [...currSubj.Questions, ...subj.Questions],
}
} else { } else {
this.Subjects.push(subj) return currSubj
} }
} })
} else {
toString() { return [...data, subj]
var result = []
for (var i = 0; i < this.Subjects.length; i++) {
result.push(this.Subjects[i].toString())
}
return result.join('\n\n')
} }
} }
module.exports.StringUtils = StringUtils // TODO: export singleton string utils, remove nea StringUtils from other files function dataToString(data) {
module.exports.SUtils = SUtils var result = []
module.exports.Question = Question data.forEach((subj) => {
module.exports.Subject = Subject result.push(subjectToString(subj))
module.exports.QuestionDB = QuestionDB })
module.exports.minMatchAmmount = minMatchAmmount return result.join('\n\n')
module.exports.initLogger = initLogger }
module.exports = {
minMatchAmmount,
initLogger,
getSubjNameWithoutYear,
createQuestion,
addQuestion,
addSubject,
searchData,
dataToString,
}