mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Prettied all js in src/
This commit is contained in:
parent
3f081d8dff
commit
ee0f0a9f3b
17 changed files with 1012 additions and 688 deletions
|
@ -1,10 +1,10 @@
|
|||
var debugLogger = null
|
||||
|
||||
function initLogger (logger) {
|
||||
function initLogger(logger) {
|
||||
debugLogger = logger
|
||||
}
|
||||
|
||||
function debugLog (msg, name, lvl) {
|
||||
function debugLog(msg, name, lvl) {
|
||||
if (debugLogger) {
|
||||
debugLogger(msg, name, lvl)
|
||||
}
|
||||
|
@ -18,26 +18,21 @@ const commonUselessAnswerParts = [
|
|||
'A helyes válasz: ',
|
||||
'A helyes válasz:',
|
||||
'The correct answer is:',
|
||||
'\''
|
||||
"'",
|
||||
]
|
||||
const commonUselessStringParts = [
|
||||
',',
|
||||
'\\.',
|
||||
':',
|
||||
'!',
|
||||
'\\+',
|
||||
'\\s*\\.'
|
||||
]
|
||||
const specialChars = [ '&', '\\+' ]
|
||||
const commonUselessStringParts = [',', '\\.', ':', '!', '\\+', '\\s*\\.']
|
||||
const specialChars = ['&', '\\+']
|
||||
const lengthDiffMultiplier = 10 /* Percent minus for length difference */
|
||||
const minMatchAmmount = 60 /* Minimum ammount to consider that two questions match during answering */
|
||||
|
||||
const assert = (val) => {
|
||||
if (!val) { throw new Error('Assertion failed') }
|
||||
if (!val) {
|
||||
throw new Error('Assertion failed')
|
||||
}
|
||||
}
|
||||
|
||||
class StringUtils {
|
||||
GetSubjNameWithoutYear (subjName) {
|
||||
GetSubjNameWithoutYear(subjName) {
|
||||
let t = subjName.split(' - ')
|
||||
if (t[0].match(/^[0-9]{4}\/[0-9]{2}\/[0-9]{1}$/i)) {
|
||||
return t[1] || subjName
|
||||
|
@ -46,7 +41,7 @@ class StringUtils {
|
|||
}
|
||||
}
|
||||
|
||||
RemoveStuff (value, removableStrings, toReplace) {
|
||||
RemoveStuff(value, removableStrings, toReplace) {
|
||||
removableStrings.forEach((x) => {
|
||||
var regex = new RegExp(x, 'g')
|
||||
value = value.replace(regex, toReplace || '')
|
||||
|
@ -54,14 +49,14 @@ class StringUtils {
|
|||
return value
|
||||
}
|
||||
|
||||
SimplifyQuery (q) {
|
||||
SimplifyQuery(q) {
|
||||
assert(q)
|
||||
|
||||
var result = q.replace(/\n/g, ' ').replace(/\s/g, ' ')
|
||||
return this.RemoveUnnecesarySpaces(result)
|
||||
}
|
||||
|
||||
ShortenString (toShorten, ammount) {
|
||||
ShortenString(toShorten, ammount) {
|
||||
assert(toShorten)
|
||||
|
||||
var result = ''
|
||||
|
@ -73,7 +68,7 @@ class StringUtils {
|
|||
return result
|
||||
}
|
||||
|
||||
ReplaceCharsWithSpace (val, char) {
|
||||
ReplaceCharsWithSpace(val, char) {
|
||||
assert(val)
|
||||
assert(char)
|
||||
|
||||
|
@ -86,7 +81,7 @@ class StringUtils {
|
|||
}
|
||||
|
||||
// removes whitespace from begining and and, and replaces multiple spaces with one space
|
||||
RemoveUnnecesarySpaces (toremove) {
|
||||
RemoveUnnecesarySpaces(toremove) {
|
||||
assert(toremove)
|
||||
|
||||
toremove = this.NormalizeSpaces(toremove)
|
||||
|
@ -97,34 +92,41 @@ class StringUtils {
|
|||
}
|
||||
|
||||
// simplifies a string for easier comparison
|
||||
SimplifyStringForComparison (value) {
|
||||
SimplifyStringForComparison(value) {
|
||||
assert(value)
|
||||
|
||||
value = this.RemoveUnnecesarySpaces(value).toLowerCase()
|
||||
return this.RemoveStuff(value, commonUselessStringParts)
|
||||
}
|
||||
|
||||
RemoveSpecialChars (value) {
|
||||
RemoveSpecialChars(value) {
|
||||
assert(value)
|
||||
|
||||
return this.RemoveStuff(value, specialChars, ' ')
|
||||
}
|
||||
|
||||
// if the value is empty, or whitespace
|
||||
EmptyOrWhiteSpace (value) {
|
||||
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, ' ') === '')
|
||||
if (value === undefined) {
|
||||
return true
|
||||
}
|
||||
return (
|
||||
value
|
||||
.replace(/\n/g, '')
|
||||
.replace(/ /g, '')
|
||||
.replace(/\s/g, ' ') === ''
|
||||
)
|
||||
}
|
||||
|
||||
// damn nonbreaking space
|
||||
NormalizeSpaces (input) {
|
||||
NormalizeSpaces(input) {
|
||||
assert(input)
|
||||
|
||||
return input.replace(/\s/g, ' ')
|
||||
}
|
||||
|
||||
CompareString (s1, s2) {
|
||||
CompareString(s1, s2) {
|
||||
if (!s1 || !s2) {
|
||||
if (!s1 && !s2) {
|
||||
return 100
|
||||
|
@ -137,24 +139,27 @@ class StringUtils {
|
|||
s2 = this.SimplifyStringForComparison(s2).split(' ')
|
||||
var match = 0
|
||||
for (var i = 0; i < s1.length; i++) {
|
||||
if (s2.includes(s1[i])) { match++ }
|
||||
if (s2.includes(s1[i])) {
|
||||
match++
|
||||
}
|
||||
}
|
||||
var percent = Math.round(((match / s1.length) * 100).toFixed(2)) // matched words percent
|
||||
var lengthDifference = Math.abs(s2.length - s1.length)
|
||||
percent -= lengthDifference * lengthDiffMultiplier
|
||||
if (percent < 0) { percent = 0 }
|
||||
if (percent < 0) {
|
||||
percent = 0
|
||||
}
|
||||
return percent
|
||||
}
|
||||
|
||||
AnswerPreProcessor (value) {
|
||||
AnswerPreProcessor(value) {
|
||||
assert(value)
|
||||
|
||||
return this.RemoveStuff(
|
||||
value, commonUselessAnswerParts)
|
||||
return this.RemoveStuff(value, commonUselessAnswerParts)
|
||||
}
|
||||
|
||||
// 'a. pécsi sör' -> 'pécsi sör'
|
||||
RemoveAnswerLetters (value) {
|
||||
RemoveAnswerLetters(value) {
|
||||
assert(value)
|
||||
|
||||
let s = value.split('. ')
|
||||
|
@ -166,8 +171,10 @@ class StringUtils {
|
|||
}
|
||||
}
|
||||
|
||||
SimplifyQA (value, mods) {
|
||||
if (!value) { return }
|
||||
SimplifyQA(value, mods) {
|
||||
if (!value) {
|
||||
return
|
||||
}
|
||||
|
||||
const reducer = (res, fn) => {
|
||||
return fn(res)
|
||||
|
@ -176,27 +183,23 @@ 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)
|
||||
])
|
||||
SimplifyAnswer(value) {
|
||||
return this.SimplifyQA(value, [
|
||||
this.RemoveSpecialChars.bind(this),
|
||||
this.RemoveUnnecesarySpaces.bind(this),
|
||||
this.AnswerPreProcessor.bind(this),
|
||||
this.RemoveAnswerLetters.bind(this),
|
||||
])
|
||||
}
|
||||
|
||||
SimplifyQuestion (value) {
|
||||
return this.SimplifyQA(
|
||||
value,
|
||||
[
|
||||
this.RemoveSpecialChars.bind(this),
|
||||
this.RemoveUnnecesarySpaces.bind(this)
|
||||
])
|
||||
SimplifyQuestion(value) {
|
||||
return this.SimplifyQA(value, [
|
||||
this.RemoveSpecialChars.bind(this),
|
||||
this.RemoveUnnecesarySpaces.bind(this),
|
||||
])
|
||||
}
|
||||
|
||||
SimplifyStack (stack) {
|
||||
SimplifyStack(stack) {
|
||||
return this.SimplifyQuery(stack)
|
||||
}
|
||||
}
|
||||
|
@ -204,13 +207,13 @@ class StringUtils {
|
|||
const SUtils = new StringUtils()
|
||||
|
||||
class Question {
|
||||
constructor (q, a, data) {
|
||||
constructor(q, a, data) {
|
||||
this.Q = SUtils.SimplifyQuestion(q)
|
||||
this.A = SUtils.SimplifyAnswer(a)
|
||||
this.data = { ...data }
|
||||
}
|
||||
|
||||
toString () {
|
||||
toString() {
|
||||
if (this.data.type !== 'simple') {
|
||||
return '?' + this.Q + '\n!' + this.A + '\n>' + JSON.stringify(this.data)
|
||||
} else {
|
||||
|
@ -218,28 +221,31 @@ class Question {
|
|||
}
|
||||
}
|
||||
|
||||
HasQuestion () {
|
||||
HasQuestion() {
|
||||
return this.Q !== undefined
|
||||
}
|
||||
|
||||
HasAnswer () {
|
||||
HasAnswer() {
|
||||
return this.A !== undefined
|
||||
}
|
||||
|
||||
HasImage () {
|
||||
HasImage() {
|
||||
return this.data.type === 'image'
|
||||
}
|
||||
|
||||
IsComplete () {
|
||||
IsComplete() {
|
||||
return this.HasQuestion() && this.HasAnswer()
|
||||
}
|
||||
|
||||
CompareImage (data2) {
|
||||
return SUtils.CompareString(this.data.images.join(' '), data2.images.join(' '))
|
||||
CompareImage(data2) {
|
||||
return SUtils.CompareString(
|
||||
this.data.images.join(' '),
|
||||
data2.images.join(' ')
|
||||
)
|
||||
}
|
||||
|
||||
// returns -1 if botth is simple
|
||||
CompareData (qObj) {
|
||||
CompareData(qObj) {
|
||||
try {
|
||||
if (qObj.data.type === this.data.type) {
|
||||
let dataType = qObj.data.type
|
||||
|
@ -248,7 +254,11 @@ class Question {
|
|||
} else if (dataType === 'image') {
|
||||
return this.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 {
|
||||
|
@ -262,22 +272,22 @@ class Question {
|
|||
return 0
|
||||
}
|
||||
|
||||
CompareQuestion (qObj) {
|
||||
CompareQuestion(qObj) {
|
||||
return SUtils.CompareString(this.Q, qObj.Q)
|
||||
}
|
||||
|
||||
CompareAnswer (qObj) {
|
||||
CompareAnswer(qObj) {
|
||||
return SUtils.CompareString(this.A, qObj.A)
|
||||
}
|
||||
|
||||
Compare (q2, data) {
|
||||
Compare(q2, data) {
|
||||
assert(q2)
|
||||
let qObj
|
||||
|
||||
if (typeof q2 === 'string') {
|
||||
qObj = {
|
||||
Q: q2,
|
||||
data: data
|
||||
data: data,
|
||||
}
|
||||
} else {
|
||||
qObj = q2
|
||||
|
@ -307,42 +317,42 @@ class Question {
|
|||
qMatch: qMatch,
|
||||
aMatch: aMatch,
|
||||
dMatch: dMatch,
|
||||
avg: avg
|
||||
avg: avg,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Subject {
|
||||
constructor (n) {
|
||||
constructor(n) {
|
||||
assert(n)
|
||||
|
||||
this.Name = n
|
||||
this.Questions = []
|
||||
}
|
||||
|
||||
setIndex (i) {
|
||||
setIndex(i) {
|
||||
this.index = i
|
||||
}
|
||||
|
||||
getIndex () {
|
||||
getIndex() {
|
||||
return this.index
|
||||
}
|
||||
|
||||
get length () {
|
||||
get length() {
|
||||
return this.Questions.length
|
||||
}
|
||||
|
||||
AddQuestion (q) {
|
||||
AddQuestion(q) {
|
||||
assert(q)
|
||||
|
||||
this.Questions.push(q)
|
||||
}
|
||||
|
||||
getSubjNameWithoutYear () {
|
||||
getSubjNameWithoutYear() {
|
||||
return SUtils.GetSubjNameWithoutYear(this.Name)
|
||||
}
|
||||
|
||||
getYear () {
|
||||
getYear() {
|
||||
let t = this.Name.split(' - ')[0]
|
||||
if (t.match(/^[0-9]{4}\/[0-9]{2}\/[0-9]{1}$/i)) {
|
||||
return t
|
||||
|
@ -351,7 +361,7 @@ class Subject {
|
|||
}
|
||||
}
|
||||
|
||||
Search (q, data) {
|
||||
Search(q, data) {
|
||||
assert(q)
|
||||
|
||||
var r = []
|
||||
|
@ -361,7 +371,7 @@ class Subject {
|
|||
r.push({
|
||||
q: this.Questions[i],
|
||||
match: percent.avg,
|
||||
detailedMatch: percent
|
||||
detailedMatch: percent,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -379,30 +389,36 @@ class Subject {
|
|||
return r
|
||||
}
|
||||
|
||||
toString () {
|
||||
toString() {
|
||||
var r = []
|
||||
for (var i = 0; i < this.Questions.length; i++) { r.push(this.Questions[i].toString()) }
|
||||
for (var i = 0; i < this.Questions.length; i++) {
|
||||
r.push(this.Questions[i].toString())
|
||||
}
|
||||
return '+' + this.Name + '\n' + r.join('\n')
|
||||
}
|
||||
}
|
||||
|
||||
class QuestionDB {
|
||||
constructor () {
|
||||
constructor() {
|
||||
this.Subjects = []
|
||||
}
|
||||
|
||||
get length () {
|
||||
get length() {
|
||||
return this.Subjects.length
|
||||
}
|
||||
|
||||
AddQuestion (subj, q) {
|
||||
AddQuestion(subj, q) {
|
||||
debugLog('Adding new question with subjName: ' + subj, 'qdb add', 1)
|
||||
debugLog(q, 'qdb add', 3)
|
||||
assert(subj)
|
||||
|
||||
var i = 0
|
||||
while (i < this.Subjects.length &&
|
||||
!subj.toLowerCase().includes(this.Subjects[i].getSubjNameWithoutYear().toLowerCase())) {
|
||||
while (
|
||||
i < this.Subjects.length &&
|
||||
!subj
|
||||
.toLowerCase()
|
||||
.includes(this.Subjects[i].getSubjNameWithoutYear().toLowerCase())
|
||||
) {
|
||||
i++
|
||||
}
|
||||
|
||||
|
@ -417,7 +433,7 @@ class QuestionDB {
|
|||
}
|
||||
}
|
||||
|
||||
SimplifyQuestion (q) {
|
||||
SimplifyQuestion(q) {
|
||||
if (typeof q === 'string') {
|
||||
return SUtils.SimplifyQuestion(q)
|
||||
} else {
|
||||
|
@ -427,7 +443,7 @@ class QuestionDB {
|
|||
}
|
||||
}
|
||||
|
||||
Search (q, subjName, data) {
|
||||
Search(q, subjName, data) {
|
||||
assert(q)
|
||||
debugLog('Searching for question', 'qdb search', 1)
|
||||
debugLog('Question:', 'qdb search', 2)
|
||||
|
@ -447,7 +463,11 @@ class QuestionDB {
|
|||
|
||||
var r = []
|
||||
this.Subjects.forEach((subj) => {
|
||||
if (subjName.toLowerCase().includes(subj.getSubjNameWithoutYear().toLowerCase())) {
|
||||
if (
|
||||
subjName
|
||||
.toLowerCase()
|
||||
.includes(subj.getSubjNameWithoutYear().toLowerCase())
|
||||
) {
|
||||
debugLog(`Searching in ${subj.Name} `, 2)
|
||||
r = r.concat(subj.Search(q, data))
|
||||
}
|
||||
|
@ -455,12 +475,20 @@ class QuestionDB {
|
|||
|
||||
// FIXME: try to remove this? but this is also a good backup plan so idk
|
||||
if (r.length === 0) {
|
||||
debugLog('Reqults length is zero when comparing names, trying all subjects', 'qdb search', 1)
|
||||
debugLog(
|
||||
'Reqults length is zero when comparing names, trying all subjects',
|
||||
'qdb search',
|
||||
1
|
||||
)
|
||||
this.Subjects.forEach((subj) => {
|
||||
r = r.concat(subj.Search(q, data))
|
||||
})
|
||||
if (r.length > 0) {
|
||||
debugLog(`FIXME: '${subjName}' gave no result but '' did!`, 'qdb search', 1)
|
||||
debugLog(
|
||||
`FIXME: '${subjName}' gave no result but '' did!`,
|
||||
'qdb search',
|
||||
1
|
||||
)
|
||||
console.error(`FIXME: '${subjName}' gave no result but '' did!`)
|
||||
}
|
||||
}
|
||||
|
@ -479,11 +507,13 @@ class QuestionDB {
|
|||
return r
|
||||
}
|
||||
|
||||
AddSubject (subj) {
|
||||
AddSubject(subj) {
|
||||
assert(subj)
|
||||
|
||||
var i = 0
|
||||
while (i < this.length && subj.Name !== this.Subjects[i].Name) { i++ }
|
||||
while (i < this.length && subj.Name !== this.Subjects[i].Name) {
|
||||
i++
|
||||
}
|
||||
|
||||
if (i < this.length) {
|
||||
this.Subjects.concat(subj.Questions)
|
||||
|
@ -492,9 +522,11 @@ class QuestionDB {
|
|||
}
|
||||
}
|
||||
|
||||
toString () {
|
||||
toString() {
|
||||
var r = []
|
||||
for (var i = 0; i < this.Subjects.length; i++) { r.push(this.Subjects[i].toString()) }
|
||||
for (var i = 0; i < this.Subjects.length; i++) {
|
||||
r.push(this.Subjects[i].toString())
|
||||
}
|
||||
return r.join('\n\n')
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue