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'],
'no-unused-vars': 'warn',
'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(' - ')
if (t[0].match(/^[0-9]{4}\/[0-9]{2}\/[0-9]{1}$/i)) {
return t[1] || subjName
} else {
return subjName
}
}
}
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 || '')
})
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) {
// removes whitespace from begining and and, and replaces multiple spaces with one space
function removeUnnecesarySpaces(toremove) {
assert(toremove)
toremove = this.NormalizeSpaces(toremove)
toremove = normalizeSpaces(toremove)
while (toremove.includes(' ')) {
toremove = toremove.replace(/ {2}/g, ' ')
}
return toremove.trim()
}
}
// simplifies a string for easier comparison
SimplifyStringForComparison(value) {
// simplifies a string for easier comparison
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, ' ')
}
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
NormalizeSpaces(input) {
// damn nonbreaking space
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])) {
@ -150,16 +112,16 @@ class StringUtils {
percent = 0
}
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) {
// 'a. pécsi sör' -> 'pécsi sör'
function removeAnswerLetters(value) {
assert(value)
let val = value.split('. ')
@ -169,9 +131,9 @@ class StringUtils {
} else {
return value
}
}
}
SimplifyQA(value, mods) {
function simplifyQA(value, mods) {
if (!value) {
return
}
@ -181,84 +143,63 @@ 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)
} else {
question.Q = simplifyQA(question.Q, [
removeSpecialChars.bind(this),
removeUnnecesarySpaces.bind(this),
])
question.A = simplifyQA(question.A, [
removeSpecialChars.bind(this),
removeUnnecesarySpaces.bind(this),
])
return question
}
}
const SUtils = new StringUtils()
// ---------------------------------------------------------------------------------------------------------
// Question
// ---------------------------------------------------------------------------------------------------------
class Question {
constructor(question, answer, data) {
this.Q = SUtils.SimplifyQuestion(question)
this.A = SUtils.SimplifyAnswer(answer)
this.data = { ...data }
function createQuestion(question, answer, data) {
return {
Q: simplifyQuestion(question),
A: simplifyAnswer(answer),
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
}
}
function compareImage(data, data2) {
return compareString(data.images.join(' '), data2.images.join(' '))
}
HasQuestion() {
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) {
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 {
@ -270,18 +211,21 @@ class Question {
debugLog(error, 'Compare question data', 2)
}
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) {
@ -319,157 +263,124 @@ class Question {
dMatch: dMatch,
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) {
assert(name)
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) {
// ---------------------------------------------------------------------------------------------------------
// 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,
})
}
}
})
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
})
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 = []
}
function subjectToString(subj) {
const { Questions, Name } = subj
get length() {
return this.Subjects.length
}
var result = []
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(question, 'qdb add', 3)
assert(data)
assert(subj)
assert(question)
assert(typeof question === 'object')
let result = []
var i = 0
while (
i < this.Subjects.length &&
!subj
.toLowerCase()
.includes(this.Subjects[i].getSubjNameWithoutYear().toLowerCase())
i < data.length &&
!subj.toLowerCase().includes(getSubjNameWithoutYear(data[i]).toLowerCase())
) {
i++
}
if (i < this.Subjects.length) {
if (i < data.length) {
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 {
debugLog('Creating new subject for question', 'qdb add', 1)
const newSubject = new Subject(subj)
newSubject.AddQuestion(question)
this.Subjects.push(newSubject)
}
result = [
...data,
{
name: subj,
Questions: [question],
},
]
}
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
}
}
return result
}
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
}
}
toString() {
var result = []
for (var i = 0; i < this.Subjects.length; i++) {
result.push(this.Subjects[i].toString())
}
return result.join('\n\n')
})
} else {
return [...data, subj]
}
}
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
function dataToString(data) {
var result = []
data.forEach((subj) => {
result.push(subjectToString(subj))
})
return result.join('\n\n')
}
module.exports = {
minMatchAmmount,
initLogger,
getSubjNameWithoutYear,
createQuestion,
addQuestion,
addSubject,
searchData,
dataToString,
}