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 /*Question object
* q: question * q: question
* a: answer * a: answer
* i: image * i: image
* */ * */
function Question(q, a, i) { class Question {
constructor(q, a, i) {
this.Q = q; this.Q = q;
this.A = a; this.A = a;
this.I = i; this.I = i;
this.toString = function() { }
toString() {
var r = "?" + this.Q + "\n!" + this.A; var r = "?" + this.Q + "\n!" + this.A;
if (this.I) if (this.I)
r += "\n>" + this.I; r += "\n>" + this.I;
return r; return r;
}; }
this.HasQuestion = function() { HasQuestion() {
return this.Q != undefined; return this.Q != undefined;
}; }
this.HasAnswer = function() { HasAnswer() {
return this.A != undefined; return this.A != undefined;
}; }
this.HasImage = function() { HasImage() {
return this.I != undefined; return this.I != undefined;
}; }
this.IsComplete = function() { IsComplete() {
return this.HasQuestion() && this.HasAnswer(); return this.HasQuestion() && this.HasAnswer();
}; }
this.Compare = function(q2) { Compare(q2) {
const qmatchpercent = CompareString(this.Q, q2.Q); const qmatchpercent = Question.CompareString(this.Q, q2.Q);
const amatchpercent = CompareString(this.A, q2.A); const amatchpercent = Question.CompareString(this.A, q2.A);
const imatchpercent = I ? CompareString(this.I, q2.I) : 0; if (this.I != undefined) {
const imatchpercent = this.I == undefined ? Question.CompareString(this.I, q2.I) : 0;
return (qmatchpercent + amatchpercent + imatchpercent) / 3; return (qmatchpercent + amatchpercent + imatchpercent) / 3;
}; } else {
const CompareString = function(s1, s2) { return (qmatchpercent + amatchpercent) / 2;
}
}
static CompareString(s1, s2) {
//if (s1 == undefined || s2 == undefined)
// return 0;
s1 = SimplifyStringForComparison(s1).split(" "); s1 = SimplifyStringForComparison(s1).split(" ");
s2 = SimplifyStringForComparison(s2).split(" "); s2 = SimplifyStringForComparison(s2).split(" ");
var match = 0; var match = 0;
for (var i = 0; i < s1.length; i++) { for (var i = 0; i < s1.length; i++)
if (s2.includes(s1[i])) { if (s2.includes(s1[i]))
match++; match++;
}
}
var percent = Math.round(((match / s1.length) * 100).toFixed(2)); // matched words percent var percent = Math.round(((match / s1.length) * 100).toFixed(2)); // matched words percent
var lengthDifference = Math.abs(s2.length - s1.length); var lengthDifference = Math.abs(s2.length - s1.length);
percent -= lengthDifference * 3; percent -= lengthDifference * 3;
if (percent < 0) if (percent < 0)
percent = 0; percent = 0;
return percent; return percent;
}; }
} }
function Subject(n) { class Subject {
constructor(n) {
this.Name = n; this.Name = n;
this.Questions = []; this.Questions = [];
this.AddQuestion = function(q) { }
get length() {
return this.Questions.length;
}
AddQuestion(q) {
this.Questions.push(q); this.Questions.push(q);
}; }
this.toString = function() { toString() {
var r = []; var r = [];
for (var i = 0; i < this.Questions.length; i++) for (var i = 0; i < this.Questions.length; i++)
r.push(this.Questions[i].toString()); r.push(this.Questions[i].toString());
return "+" + this.Name + "\n" + r.join("\n"); return "+" + this.Name + "\n" + r.join("\n");
}; }
} }
function QuestionDB() { class QuestionDB {
constructor() {
this.Subjects = []; this.Subjects = [];
this.AddQuestion = function(subj, q) { }
get length() {
return this.Subjects.length;
}
AddQuestion(subj, q) {
var i = 0; var i = 0;
while (i < this.Subjects.length && this.Subjects[i].Name != subj) while (i < this.Subjects.length && this.Subjects[i].Name != subj)
i++; i++;
@ -131,13 +112,60 @@ function QuestionDB() {
n.AddQuestion(q); n.AddQuestion(q);
this.Subjects.push(n); 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 = []; var r = [];
for (var i = 0; i < this.Subjects.length; i++) for (var i = 0; i < this.Subjects.length; i++)
r.push(this.Subjects[i].toString()); r.push(this.Subjects[i].toString());
return r.join("\n\n"); 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) if (skipped >= 1)
logs.push(i + ": " + d[i]); logs.push(i + ": " + d[i]);
i++; i++;
if (i >= d.length) if (i >= d.length) {
if (currQuestion.IsComplete())
r.AddQuestion(currSubj, currQuestion);
return { return {
result: r, result: r,
logs: logs logs: logs
}; };
}
currIdentifier = d[i][0]; currIdentifier = d[i][0];
skipped++; skipped++;
} while (!ExpectedIdentifier.includes(currIdentifier) && i < d.length); } while (!ExpectedIdentifier.includes(currIdentifier) && i < d.length);
let currData = d[i].substring(1); let currData = d[i].substring(1).trim();
if (currIdentifier == '+') { if (currIdentifier == '+') {
if (currQuestion.IsComplete())
r.AddQuestion(currSubj, currQuestion);
currQuestion = new Question();
currSubj = currData; currSubj = currData;
ExpectedIdentifier = ['?']; ExpectedIdentifier = ['?'];
continue; continue;
@ -226,17 +260,34 @@ function ReadData(data) {
}; };
} }
function SortQuestions(dataObj) { function RemoveDuplicates(dataObj) {
/* for (var i = 0; i < dataObj.length; i++)
* Sort questions by something RemoveDuplFromSubject(dataObj.Subjects[i]);
* */ return dataObj;
} }
function RemoveDuplicates(dataObj) { function RemoveDuplFromSubject(subj) {
// TODO: compare and delete var cp = subj.Questions;
/* subj.Questions = [];
* removes duplicates from a read data by comparing every question after sorting 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) { function SimplifyStringForComparison(value) {