mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Merger reading
This commit is contained in:
parent
f9361895f1
commit
848a9011ce
1 changed files with 132 additions and 49 deletions
173
merger.js
173
merger.js
|
@ -28,16 +28,15 @@ function Main() {
|
||||||
console.log(params);
|
console.log(params);
|
||||||
var oldData = "";
|
var oldData = "";
|
||||||
var sum = 0;
|
var sum = 0;
|
||||||
for (var i = 0; i < params.length; i++) {
|
|
||||||
const data = utils.ReadFile(params[i]);
|
|
||||||
var r = actions.NLoad(data);
|
|
||||||
if (r.count > 0) {
|
|
||||||
sum += r.count;
|
|
||||||
console.log(params[i] + ": " + r.count);
|
|
||||||
|
|
||||||
oldData += "\n" + data;
|
var r = ReadData(utils.ReadFile(params[0]));
|
||||||
} else
|
console.log(r.result.toString());
|
||||||
console.log("error loading: " + params[i]);
|
console.log("\n\n");
|
||||||
|
console.log("errors:");
|
||||||
|
console.log(r.logs.join("\n"));
|
||||||
|
|
||||||
|
/*for (var i = 0; i < params.length; i++) {
|
||||||
|
const data = utils.ReadFile(params[i]);
|
||||||
}
|
}
|
||||||
console.log("-------------");
|
console.log("-------------");
|
||||||
console.log("Sum: " + sum);
|
console.log("Sum: " + sum);
|
||||||
|
@ -46,7 +45,7 @@ function Main() {
|
||||||
var nr = actions.NLoad(newData);
|
var nr = actions.NLoad(newData);
|
||||||
console.log("New result: " + nr.count);
|
console.log("New result: " + nr.count);
|
||||||
utils.WriteFile(newData, "newData");
|
utils.WriteFile(newData, "newData");
|
||||||
console.log("File written!");
|
console.log("File written!");*/
|
||||||
}
|
}
|
||||||
|
|
||||||
function GetParams() {
|
function GetParams() {
|
||||||
|
@ -59,19 +58,31 @@ function GetParams() {
|
||||||
* i: image
|
* i: image
|
||||||
* */
|
* */
|
||||||
function Question(q, a, i) {
|
function Question(q, a, i) {
|
||||||
this.q = q;
|
this.Q = q;
|
||||||
this.a = a;
|
this.A = a;
|
||||||
this.i = i;
|
this.I = i;
|
||||||
this.toString = function() {
|
this.toString = function() {
|
||||||
var r = "?" + this.q + "\n!" + a;
|
var r = "?" + this.Q + "\n!" + this.A;
|
||||||
if (i)
|
if (this.I)
|
||||||
r += "\n>" + i;
|
r += "\n>" + this.I;
|
||||||
return r;
|
return r;
|
||||||
};
|
};
|
||||||
|
this.HasQuestion = function() {
|
||||||
|
return this.Q != undefined;
|
||||||
|
};
|
||||||
|
this.HasAnswer = function() {
|
||||||
|
return this.A != undefined;
|
||||||
|
};
|
||||||
|
this.HasImage = function() {
|
||||||
|
return this.I != undefined;
|
||||||
|
};
|
||||||
|
this.IsComplete = function() {
|
||||||
|
return this.HasQuestion() && this.HasAnswer();
|
||||||
|
};
|
||||||
this.Compare = function(q2) {
|
this.Compare = function(q2) {
|
||||||
const qmatchpercent = CompareString(this.q, q2.q);
|
const qmatchpercent = CompareString(this.Q, q2.Q);
|
||||||
const amatchpercent = CompareString(this.a, q2.a);
|
const amatchpercent = CompareString(this.A, q2.A);
|
||||||
const imatchpercent = i ? CompareString(this.i, q2.i) : 0;
|
const imatchpercent = I ? CompareString(this.I, q2.I) : 0;
|
||||||
|
|
||||||
return (qmatchpercent + amatchpercent + imatchpercent) / 3;
|
return (qmatchpercent + amatchpercent + imatchpercent) / 3;
|
||||||
};
|
};
|
||||||
|
@ -99,43 +110,120 @@ function Subject(n) {
|
||||||
this.AddQuestion = function(q) {
|
this.AddQuestion = function(q) {
|
||||||
this.Questions.push(q);
|
this.Questions.push(q);
|
||||||
};
|
};
|
||||||
|
this.toString = function() {
|
||||||
|
var r = [];
|
||||||
|
for (var i = 0; i < this.Questions.length; i++)
|
||||||
|
r.push(this.Questions[i].toString());
|
||||||
|
return "+" + this.Name + "\n" + r.join("\n");
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function QuestionDB () {
|
function QuestionDB() {
|
||||||
this.Subjects = [];
|
this.Subjects = [];
|
||||||
this.AddQuestion = function(subj, q) {
|
this.AddQuestion = function(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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
this.toString = function() {
|
||||||
|
var r = [];
|
||||||
|
for (var i = 0; i < this.Subjects.length; i++)
|
||||||
|
r.push(this.Subjects[i].toString());
|
||||||
|
return r.join("\n\n");
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Returns a question database from the given data.
|
* Returns a question database from the given data.
|
||||||
* Parameter should be raw read file in string
|
* Parameter should be raw read file in string with "\n"-s
|
||||||
|
* TODO: ??? -s are not listed as errors, tho works correctly
|
||||||
* */
|
* */
|
||||||
function ReadData(data) {
|
function ReadData(data) {
|
||||||
|
|
||||||
function AddQuestion(q) {
|
const d = data.split("\n");
|
||||||
|
const r = new QuestionDB();
|
||||||
|
var logs = [];
|
||||||
|
var currSubj = ""; // the current subjects name
|
||||||
|
var ExpectedIdentifier = ['+', '?'];
|
||||||
|
let currQuestion = new Question();
|
||||||
|
|
||||||
|
var i = -1;
|
||||||
|
while (i < d.length) {
|
||||||
|
let currIdentifier;
|
||||||
|
let skipped = 0;
|
||||||
|
do {
|
||||||
|
if (skipped >= 1)
|
||||||
|
logs.push(i + ": " + d[i]);
|
||||||
|
i++;
|
||||||
|
if (i >= d.length)
|
||||||
|
return {
|
||||||
|
result: r,
|
||||||
|
logs: logs
|
||||||
|
};
|
||||||
|
currIdentifier = d[i][0];
|
||||||
|
skipped++;
|
||||||
|
} while (!ExpectedIdentifier.includes(currIdentifier) && i < d.length);
|
||||||
|
|
||||||
|
let currData = d[i].substring(1);
|
||||||
|
|
||||||
|
if (currIdentifier == '+') {
|
||||||
|
currSubj = currData;
|
||||||
|
ExpectedIdentifier = ['?'];
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
var d = data.split("\n");
|
|
||||||
var result =
|
|
||||||
|
|
||||||
|
if (currIdentifier == '?') {
|
||||||
|
if (currQuestion.IsComplete()) {
|
||||||
|
r.AddQuestion(currSubj, currQuestion);
|
||||||
|
currQuestion = new Question();
|
||||||
|
}
|
||||||
|
// overwriting is allowed here, bcus:
|
||||||
|
// ?????!>
|
||||||
|
currQuestion.Q = currData;
|
||||||
|
ExpectedIdentifier = ['!', '?'];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
if (currIdentifier == '!') {
|
||||||
* sliding window
|
// if dont have question continue
|
||||||
* check if its a ?
|
if (!currQuestion.HasQuestion())
|
||||||
* then searches next !
|
throw "No question! (A)";
|
||||||
* then checks if next is ? or >
|
// dont allow overwriting
|
||||||
* adds to different subj on +
|
// ?!!!!
|
||||||
*
|
if (!currQuestion.HasAnswer()) {
|
||||||
* ERROR HANDLING:
|
currQuestion.A = currData;
|
||||||
* skips unexpected start characters
|
}
|
||||||
* resets curr question if there are multiple ?-s after each other
|
ExpectedIdentifier = ['?', '>', '+'];
|
||||||
*
|
continue;
|
||||||
* DOES NOT COMPARES
|
}
|
||||||
* */
|
|
||||||
|
|
||||||
|
if (currIdentifier == '>') {
|
||||||
|
// if dont have question or answer continue
|
||||||
|
if (!currQuestion.HasQuestion())
|
||||||
|
throw "No question! (I)";
|
||||||
|
if (!currQuestion.HasAnswer())
|
||||||
|
throw "No asnwer! (I)";
|
||||||
|
// dont allow overwriting
|
||||||
|
// ?!>>>
|
||||||
|
if (!currQuestion.HasImage()) {
|
||||||
|
currQuestion.I = currData;
|
||||||
|
}
|
||||||
|
ExpectedIdentifier = ['?', '+'];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
result: r,
|
||||||
|
logs: logs
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function SortQuestions(dataObj) {
|
function SortQuestions(dataObj) {
|
||||||
|
@ -145,17 +233,12 @@ function SortQuestions(dataObj) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function RemoveDuplicates(dataObj) {
|
function RemoveDuplicates(dataObj) {
|
||||||
|
// TODO: compare and delete
|
||||||
/*
|
/*
|
||||||
* removes duplicates from a read data by comparing every question after sorting
|
* removes duplicates from a read data by comparing every question after sorting
|
||||||
* */
|
* */
|
||||||
}
|
}
|
||||||
|
|
||||||
function QuestionDbToString(questionObj) {
|
|
||||||
/*
|
|
||||||
* Converst a question db object to string for writing out
|
|
||||||
* */
|
|
||||||
}
|
|
||||||
|
|
||||||
function SimplifyStringForComparison(value) {
|
function SimplifyStringForComparison(value) {
|
||||||
value = RemoveUnnecesarySpaces(value).toLowerCase();
|
value = RemoveUnnecesarySpaces(value).toLowerCase();
|
||||||
var removableChars = [",", ".", ":", "!"];
|
var removableChars = [",", ".", ":", "!"];
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue