mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Classes to seperate module, eslint formatting
This commit is contained in:
parent
b2b0e862dd
commit
f30f4d4fc5
8 changed files with 468 additions and 413 deletions
184
utils/merger.js
184
utils/merger.js
|
@ -22,127 +22,8 @@
|
|||
// join json datas, or raw datas
|
||||
// or something else
|
||||
|
||||
const minMatchAmmount = 55
|
||||
const minResultMatchPercent = 99
|
||||
const lengthDiffMultiplier = 10
|
||||
|
||||
class Question {
|
||||
constructor (q, a, i) {
|
||||
this.Q = q
|
||||
this.A = a
|
||||
this.I = i
|
||||
}
|
||||
toString () {
|
||||
var r = '?' + this.Q + '\n!' + this.A
|
||||
if (this.I) { r += '\n>' + this.I }
|
||||
return r
|
||||
}
|
||||
HasQuestion () {
|
||||
return this.Q != undefined
|
||||
}
|
||||
HasAnswer () {
|
||||
return this.A != undefined
|
||||
}
|
||||
HasImage () {
|
||||
return this.I != undefined
|
||||
}
|
||||
IsComplete () {
|
||||
return this.HasQuestion() && this.HasAnswer()
|
||||
}
|
||||
// TODO: TEST DIS
|
||||
Compare (q2, i) {
|
||||
if (typeof q2 === 'string') {
|
||||
var qmatchpercent = Question.CompareString(this.Q, q2)
|
||||
|
||||
if (i == undefined || i.length == 0) { return qmatchpercent } else {
|
||||
if (this.HasImage()) {
|
||||
const imatchpercent = this.HasImage() ? Question.CompareString(this.I.join(' '), i.join(' '))
|
||||
: 0
|
||||
return (qmatchpercent + imatchpercent) / 2
|
||||
} else {
|
||||
qmatchpercent -= 30
|
||||
if (qmatchpercent < 0) { return 0 } else { return qmatchpercent }
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const qmatchpercent = Question.CompareString(this.Q, q2.Q)
|
||||
const amatchpercent = Question.CompareString(this.A, q2.A)
|
||||
if (this.I != undefined) {
|
||||
const imatchpercent = this.I == undefined ? Question.CompareString(this.I.join(' '), q2.I.join(
|
||||
' ')) : 0
|
||||
return (qmatchpercent + amatchpercent + imatchpercent) / 3
|
||||
} else {
|
||||
return (qmatchpercent + amatchpercent) / 2
|
||||
}
|
||||
}
|
||||
}
|
||||
static CompareString (s1, s2) {
|
||||
s1 = SimplifyStringForComparison(s1).split(' ')
|
||||
s2 = SimplifyStringForComparison(s2).split(' ')
|
||||
var match = 0
|
||||
for (var i = 0; i < s1.length; i++) {
|
||||
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 }
|
||||
return percent
|
||||
}
|
||||
}
|
||||
|
||||
class Subject {
|
||||
constructor (n) {
|
||||
this.Name = n
|
||||
this.Questions = []
|
||||
}
|
||||
get length () {
|
||||
return this.Questions.length
|
||||
}
|
||||
AddQuestion (q) {
|
||||
this.Questions.push(q)
|
||||
}
|
||||
toString () {
|
||||
var r = []
|
||||
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 () {
|
||||
this.Subjects = []
|
||||
}
|
||||
get length () {
|
||||
return this.Subjects.length
|
||||
}
|
||||
AddQuestion (subj, q) {
|
||||
var i = 0
|
||||
while (i < this.Subjects.length && this.Subjects[i].Name != subj) { i++ }
|
||||
if (i < this.Subjects.length) { this.Subjects[i].AddQuestion(q) } else {
|
||||
const n = new Subject(subj)
|
||||
n.AddQuestion(q)
|
||||
this.Subjects.push(n)
|
||||
}
|
||||
}
|
||||
AddSubject (subj) {
|
||||
var i = 0
|
||||
while (i < this.length && subj.Name != this.Subjects[i].Name) { i++ }
|
||||
|
||||
if (i < this.length) {
|
||||
this.Subjects.concat(subj.Questions)
|
||||
} else {
|
||||
this.Subjects.push(subj)
|
||||
}
|
||||
}
|
||||
toString () {
|
||||
var r = []
|
||||
for (var i = 0; i < this.Subjects.length; i++) { r.push(this.Subjects[i].toString()) }
|
||||
return r.join('\n\n')
|
||||
}
|
||||
}
|
||||
var classes = require('./classes.js')
|
||||
var utils = require('./utils.js')
|
||||
var actions = require('./actions.js')
|
||||
|
||||
Main()
|
||||
|
||||
|
@ -171,7 +52,7 @@ function Main () {
|
|||
})
|
||||
|
||||
var olds = []
|
||||
if (dbs.length == 1) {
|
||||
if (dbs.length === 1) {
|
||||
for (let i = 0; i < dbs[0].length; i++) { olds.push(dbs[0].Subjects[i].length) }
|
||||
}
|
||||
|
||||
|
@ -198,13 +79,13 @@ function PrintLN () {
|
|||
function PrintDB (r, olds) {
|
||||
console.log('Data subject count: ' + r.length)
|
||||
var maxLength = 0
|
||||
for (var i = 0; i < r.length; i++) {
|
||||
for (let i = 0; i < r.length; i++) {
|
||||
if (maxLength < r.Subjects[i].Name.length) { maxLength = r.Subjects[i].Name.length }
|
||||
}
|
||||
|
||||
let qcount = 0
|
||||
|
||||
for (var i = 0; i < r.length; i++) {
|
||||
for (let i = 0; i < r.length; i++) {
|
||||
let line = i
|
||||
if (line < 10) { line += ' ' }
|
||||
|
||||
|
@ -213,7 +94,7 @@ function PrintDB (r, olds) {
|
|||
line += r.Subjects[i].Name
|
||||
|
||||
while (line.length < currLength) {
|
||||
if (i % 4 == 0) { line += '.' } else { line += ' ' }
|
||||
if (i % 4 === 0) { line += '.' } else { line += ' ' }
|
||||
}
|
||||
|
||||
if (olds && olds.length > 0) {
|
||||
|
@ -247,15 +128,15 @@ function GetParams () {
|
|||
|
||||
function ParseJSONData (data) {
|
||||
var d = JSON.parse(data)
|
||||
var r = new QuestionDB()
|
||||
var r = new classes.QuestionDB((x) => true, (x, y) => console.log(x, y))
|
||||
var rt = []
|
||||
|
||||
for (var i = 0; i < d.Subjects.length; i++) {
|
||||
let s = new Subject(d.Subjects[i].Name)
|
||||
let s = new classes.Subject(d.Subjects[i].Name)
|
||||
var j = 0
|
||||
for (j = 0; j < d.Subjects[i].Questions.length; j++) {
|
||||
var currQ = d.Subjects[i].Questions[j]
|
||||
s.AddQuestion(new Question(currQ.Q, currQ.A, currQ.I))
|
||||
s.AddQuestion(new classes.Question(currQ.Q, currQ.A, currQ.I))
|
||||
}
|
||||
rt.push({
|
||||
name: d.Subjects[i].Name,
|
||||
|
@ -267,7 +148,7 @@ function ParseJSONData (data) {
|
|||
}
|
||||
|
||||
function MergeDatabases (dbs) {
|
||||
var db = new QuestionDB()
|
||||
var db = new classes.QuestionDB((x) => true, (x, y) => console.log(x, y))
|
||||
for (var i = 0; i < dbs.length; i++) {
|
||||
for (var j = 0; j < dbs[i].length; j++) { db.AddSubject(dbs[i].Subjects[j]) }
|
||||
}
|
||||
|
@ -281,11 +162,11 @@ function MergeDatabases (dbs) {
|
|||
* */
|
||||
function ReadData (data) {
|
||||
const d = data.split('\n')
|
||||
const r = new QuestionDB()
|
||||
const r = new classes.QuestionDB((x) => true, (x, y) => console.log(x, y))
|
||||
var logs = []
|
||||
var currSubj = '' // the current subjects name
|
||||
var ExpectedIdentifier = ['+', '?']
|
||||
let currQuestion = new Question()
|
||||
let currQuestion = new classes.Question()
|
||||
|
||||
var i = -1
|
||||
while (i < d.length) {
|
||||
|
@ -307,18 +188,18 @@ function ReadData (data) {
|
|||
|
||||
let currData = d[i].substring(1).trim()
|
||||
|
||||
if (currIdentifier == '+') {
|
||||
if (currIdentifier === '+') {
|
||||
if (currQuestion.IsComplete()) { r.AddQuestion(currSubj, currQuestion) }
|
||||
currQuestion = new Question()
|
||||
currQuestion = new classes.Question()
|
||||
currSubj = currData
|
||||
ExpectedIdentifier = ['?']
|
||||
continue
|
||||
}
|
||||
|
||||
if (currIdentifier == '?') {
|
||||
if (currIdentifier === '?') {
|
||||
if (currQuestion.IsComplete()) {
|
||||
r.AddQuestion(currSubj, currQuestion)
|
||||
currQuestion = new Question()
|
||||
currQuestion = new classes.Question()
|
||||
}
|
||||
// overwriting is allowed here, bcus:
|
||||
// ?????!>
|
||||
|
@ -327,9 +208,9 @@ function ReadData (data) {
|
|||
continue
|
||||
}
|
||||
|
||||
if (currIdentifier == '!') {
|
||||
if (currIdentifier === '!') {
|
||||
// if dont have question continue
|
||||
if (!currQuestion.HasQuestion()) { throw 'No question! (A)' }
|
||||
if (!currQuestion.HasQuestion()) { throw new Error('No question! (A)') }
|
||||
// dont allow overwriting
|
||||
// ?!!!!
|
||||
if (!currQuestion.HasAnswer()) {
|
||||
|
@ -342,10 +223,10 @@ function ReadData (data) {
|
|||
continue
|
||||
}
|
||||
|
||||
if (currIdentifier == '>') {
|
||||
if (currIdentifier === '>') {
|
||||
// if dont have question or answer continue
|
||||
if (!currQuestion.HasQuestion()) { throw 'No question! (I)' }
|
||||
if (!currQuestion.HasAnswer()) { throw 'No asnwer! (I)' }
|
||||
if (!currQuestion.HasQuestion()) { throw new Error('No question! (I)') }
|
||||
if (!currQuestion.HasAnswer()) { throw new Error('No asnwer! (I)') }
|
||||
// dont allow overwriting
|
||||
// ?!>>>
|
||||
if (!currQuestion.HasImage()) {
|
||||
|
@ -377,7 +258,7 @@ function RemoveDuplFromSubject (subj) {
|
|||
for (var i = 0; i < cp.length; i++) {
|
||||
var j = 0
|
||||
// Only removes 100% match!
|
||||
while (j < subj.length && cp[i].Compare(subj.Questions[j]) != 100) {
|
||||
while (j < subj.length && cp[i].Compare(subj.Questions[j]) !== 100) {
|
||||
j++
|
||||
}
|
||||
if (j < subj.length) {
|
||||
|
@ -395,26 +276,3 @@ function RemoveDuplFromSubject (subj) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
function SimplifyStringForComparison (value) {
|
||||
value = RemoveUnnecesarySpaces(value).toLowerCase()
|
||||
var removableChars = [',', '.', ':', '!']
|
||||
for (var i = 0; i < removableChars.length; i++) {
|
||||
var regex = new RegExp(removableChars[i], 'g')
|
||||
value.replace(regex, '')
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
function RemoveUnnecesarySpaces (toremove) {
|
||||
toremove = NormalizeSpaces(toremove)
|
||||
while (toremove.includes(' ')) // while the text includes double spaces replaces all of them with a single one
|
||||
{
|
||||
toremove = toremove.replace(/ {2}/g, ' ')
|
||||
}
|
||||
return toremove.trim()
|
||||
}
|
||||
|
||||
function NormalizeSpaces (input) {
|
||||
return input.replace(/\s/g, ' ')
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue