Merger finnish

This commit is contained in:
YourFriendlyNeighborhoodDealer 2018-12-12 18:10:58 +01:00
parent 848a9011ce
commit 147f660319

207
merger.js
View file

@ -18,109 +18,90 @@
------------------------------------------------------------------------- */
var utils = require('./utils.js');
var actions = require('./actions.js');
Main();
function Main() {
const params = GetParams();
console.log(params);
var oldData = "";
var sum = 0;
var r = ReadData(utils.ReadFile(params[0]));
console.log(r.result.toString());
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("Sum: " + sum);
console.log("Simplifying...");
var newData = Simplify(oldData);
var nr = actions.NLoad(newData);
console.log("New result: " + nr.count);
utils.WriteFile(newData, "newData");
console.log("File written!");*/
}
function GetParams() {
return process.argv.splice(2);
}
/*Question object
* q: question
* a: answer
* i: image
* */
function Question(q, a, i) {
class Question {
constructor(q, a, i) {
this.Q = q;
this.A = a;
this.I = i;
this.toString = function() {
}
toString() {
var r = "?" + this.Q + "\n!" + this.A;
if (this.I)
r += "\n>" + this.I;
return r;
};
this.HasQuestion = function() {
}
HasQuestion() {
return this.Q != undefined;
};
this.HasAnswer = function() {
}
HasAnswer() {
return this.A != undefined;
};
this.HasImage = function() {
}
HasImage() {
return this.I != undefined;
};
this.IsComplete = function() {
}
IsComplete() {
return this.HasQuestion() && this.HasAnswer();
};
this.Compare = function(q2) {
const qmatchpercent = CompareString(this.Q, q2.Q);
const amatchpercent = CompareString(this.A, q2.A);
const imatchpercent = I ? CompareString(this.I, q2.I) : 0;
}
Compare(q2) {
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, q2.I) : 0;
return (qmatchpercent + amatchpercent + imatchpercent) / 3;
};
const CompareString = function(s1, s2) {
} else {
return (qmatchpercent + amatchpercent) / 2;
}
}
static CompareString(s1, s2) {
//if (s1 == undefined || s2 == undefined)
// return 0;
s1 = SimplifyStringForComparison(s1).split(" ");
s2 = SimplifyStringForComparison(s2).split(" ");
var match = 0;
for (var i = 0; i < s1.length; i++) {
if (s2.includes(s1[i])) {
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 * 3;
if (percent < 0)
percent = 0;
return percent;
};
}
}
function Subject(n) {
class Subject {
constructor(n) {
this.Name = n;
this.Questions = [];
this.AddQuestion = function(q) {
}
get length() {
return this.Questions.length;
}
AddQuestion(q) {
this.Questions.push(q);
};
this.toString = function() {
}
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");
};
}
}
function QuestionDB() {
class QuestionDB {
constructor() {
this.Subjects = [];
this.AddQuestion = function(subj, q) {
}
get length() {
return this.Subjects.length;
}
AddQuestion(subj, q) {
var i = 0;
while (i < this.Subjects.length && this.Subjects[i].Name != subj)
i++;
@ -131,13 +112,60 @@ function QuestionDB() {
n.AddQuestion(q);
this.Subjects.push(n);
}
};
this.toString = function() {
}
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 utils = require('./utils.js');
var actions = require('./actions.js');
Main();
function Main() {
const params = GetParams();
console.log(params);
var dbs = [];
for (var i = 0; i < params.length; i++)
dbs.push(ReadData(utils.ReadFile(params[i])).result);
var db = MergeDatabases(dbs);
var r = RemoveDuplicates(db);
for (var i = 0; i < r.length; i++) {
console.log(r.Subjects[i].Name + ": " + r.Subjects[i].length + " darab kérdés.");
}
utils.WriteFile(JSON.stringify(r), "newData");
console.log("File written!");
}
function GetParams() {
return process.argv.splice(2);
}
function MergeDatabases(dbs) {
var db = new QuestionDB();
for (var i = 0; i < dbs.length; i++)
for (var j = 0; j < dbs[i].length; j++)
db.AddSubject(dbs[i].Subjects[j]);
return db;
}
/*
@ -162,18 +190,24 @@ function ReadData(data) {
if (skipped >= 1)
logs.push(i + ": " + d[i]);
i++;
if (i >= d.length)
if (i >= d.length) {
if (currQuestion.IsComplete())
r.AddQuestion(currSubj, currQuestion);
return {
result: r,
logs: logs
};
}
currIdentifier = d[i][0];
skipped++;
} while (!ExpectedIdentifier.includes(currIdentifier) && i < d.length);
let currData = d[i].substring(1);
let currData = d[i].substring(1).trim();
if (currIdentifier == '+') {
if (currQuestion.IsComplete())
r.AddQuestion(currSubj, currQuestion);
currQuestion = new Question();
currSubj = currData;
ExpectedIdentifier = ['?'];
continue;
@ -226,17 +260,34 @@ function ReadData(data) {
};
}
function SortQuestions(dataObj) {
/*
* Sort questions by something
* */
function RemoveDuplicates(dataObj) {
for (var i = 0; i < dataObj.length; i++)
RemoveDuplFromSubject(dataObj.Subjects[i]);
return dataObj;
}
function RemoveDuplicates(dataObj) {
// TODO: compare and delete
/*
* removes duplicates from a read data by comparing every question after sorting
* */
function RemoveDuplFromSubject(subj) {
var cp = subj.Questions;
subj.Questions = [];
for (var i = 0; i < cp.length; i++) {
var j = 0;
while (j < subj.length && cp[i].Compare(subj.Questions[j]) != 100) {
j++;
}
if (j < subj.length) {
//console.log("----------------------------------------------------------");
//console.log(cp[i].toString());
//console.log(" VS ");
//console.log(subj.Questions[j].toString());
//console.log(cp[i].Compare(subj.Questions[j]));
//console.log(j);
//console.log("removed:");
//console.log(subj.Questions.splice(j, 1).toString());
//console.log("----------------------------------------------------------");
} else {
subj.AddQuestion(cp[i]);
}
}
}
function SimplifyStringForComparison(value) {