From f9361895f1cf365ca696351bccc5a2dd041ce6bb Mon Sep 17 00:00:00 2001 From: YourFriendlyNeighborhoodDealer <3167982-YourFriendlyNeighborhoodDealer@users.noreply.gitlab.com> Date: Tue, 11 Dec 2018 19:56:00 +0100 Subject: [PATCH] Started merger.js, which simplifies, merges and removes duplicates from question data --- actions.js | 3 +- merger.js | 180 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 merger.js diff --git a/actions.js b/actions.js index 2420384..555e487 100755 --- a/actions.js +++ b/actions.js @@ -20,7 +20,8 @@ module.exports = { ProcessIncomingRequest: ProcessIncomingRequest, - CheckData: CheckData + CheckData: CheckData, + NLoad: NLoad }; var recievedFile = "stats/recieved"; diff --git a/merger.js b/merger.js new file mode 100644 index 0000000..0e3c6fe --- /dev/null +++ b/merger.js @@ -0,0 +1,180 @@ +/* ---------------------------------------------------------------------------- + + Question Server question file merger + GitLab: + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + ------------------------------------------------------------------------- */ + +var utils = require('./utils.js'); +var actions = require('./actions.js'); + +Main(); + +function Main() { + const params = GetParams(); + console.log(params); + var oldData = ""; + 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; + } else + console.log("error loading: " + 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) { + this.q = q; + this.a = a; + this.i = i; + this.toString = function() { + var r = "?" + this.q + "\n!" + a; + if (i) + r += "\n>" + i; + return r; + }; + 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; + + return (qmatchpercent + amatchpercent + imatchpercent) / 3; + }; + const CompareString = function(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 * 3; + if (percent < 0) + percent = 0; + return percent; + }; +} + +function Subject(n) { + this.Name = n; + this.Questions = []; + this.AddQuestion = function(q) { + this.Questions.push(q); + }; +} + +function QuestionDB () { + this.Subjects = []; + this.AddQuestion = function(subj, q) { + + + }; +} + +/* + * Returns a question database from the given data. + * Parameter should be raw read file in string + * */ +function ReadData(data) { + + function AddQuestion(q) { + + } + var d = data.split("\n"); + var result = + + + /* + * sliding window + * check if its a ? + * then searches next ! + * then checks if next is ? or > + * adds to different subj on + + * + * ERROR HANDLING: + * skips unexpected start characters + * resets curr question if there are multiple ?-s after each other + * + * DOES NOT COMPARES + * */ + +} + +function SortQuestions(dataObj) { + /* + * Sort questions by something + * */ +} + +function RemoveDuplicates(dataObj) { + /* + * 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) { + 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(/ /g, " "); + } + return toremove.trim(); +} + +function NormalizeSpaces(input) { + return input.replace(/\s/g, ' '); +}