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,8 +31,13 @@ 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
@ -41,7 +46,9 @@ class StringUtils {
} }
} }
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 || '')
@ -49,42 +56,11 @@ class StringUtils {
return value 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 // removes whitespace from begining and and, and replaces multiple spaces with one space
RemoveUnnecesarySpaces(toremove) { function 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, ' ')
} }
@ -92,41 +68,27 @@ class StringUtils {
} }
// 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
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, ' ') === ''
)
} }
// damn nonbreaking space // damn nonbreaking space
NormalizeSpaces(input) { function 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])) {
@ -152,14 +114,14 @@ class StringUtils {
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('. ')
@ -171,7 +133,7 @@ class StringUtils {
} }
} }
SimplifyQA(value, mods) { function simplifyQA(value, mods) {
if (!value) { if (!value) {
return return
} }
@ -183,82 +145,61 @@ 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),
]) ])
}
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 { } 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() { function compareImage(data, data2) {
return this.A !== undefined return compareString(data.images.join(' '), data2.images.join(' '))
} }
HasImage() { function compareData(data, qObj) {
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 {
@ -272,16 +213,19 @@ class Question {
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) {
@ -320,156 +264,123 @@ class Question {
avg: avg, avg: avg,
} }
} }
}
class Subject { function questionToString(question) {
constructor(name) { const { Q, A, data } = question
assert(name)
this.Name = name if (data.type !== 'simple') {
this.Questions = [] return '?' + Q + '\n!' + A + '\n>' + JSON.stringify(data)
}
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 { } else {
return '' return '?' + Q + '\n!' + A
} }
} }
Search(question, data) { // ---------------------------------------------------------------------------------------------------------
// Subject
// ---------------------------------------------------------------------------------------------------------
function searchQuestion(questions, question, questionData) {
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,
}) })
} }
})
// 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++) { function subjectToString(subj) {
for (var j = i; j < result.length; j++) { const { Questions, Name } = subj
if (result[i].match < result[j].match) {
var tmp = result[i] var result = []
result[i] = result[j] Questions.forEach((question) => {
result[j] = tmp 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 return result
} }
toString() { function searchData(data, question, subjName, questionData) {
var result = [] assert(data)
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) {
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 {
return [...data, subj]
} }
} }
toString() { function dataToString(data) {
var result = [] var result = []
for (var i = 0; i < this.Subjects.length; i++) { data.forEach((subj) => {
result.push(this.Subjects[i].toString()) result.push(subjectToString(subj))
} })
return result.join('\n\n') return result.join('\n\n')
} }
}
module.exports.StringUtils = StringUtils // TODO: export singleton string utils, remove nea StringUtils from other files module.exports = {
module.exports.SUtils = SUtils minMatchAmmount,
module.exports.Question = Question initLogger,
module.exports.Subject = Subject getSubjNameWithoutYear,
module.exports.QuestionDB = QuestionDB createQuestion,
module.exports.minMatchAmmount = minMatchAmmount addQuestion,
module.exports.initLogger = initLogger addSubject,
searchData,
dataToString,
}