Basic question answering with new data structure

This commit is contained in:
YourFriendlyNeighborhoodDealer 2018-12-13 13:51:49 +01:00
parent b3389c2b46
commit 38185a3741

121
main.js
View file

@ -26,7 +26,7 @@ var serverAdress = "http://questionmining.tk/";
// forcing pages for testing. unless you test, do not set these to true! // forcing pages for testing. unless you test, do not set these to true!
// only one of these should be true for testing // only one of these should be true for testing
const forceTestPage = false; const forceTestPage = true; // TODO
const forceResultPage = false; const forceResultPage = false;
const forceDefaultPage = false; const forceDefaultPage = false;
const logElementGetting = false; const logElementGetting = false;
@ -34,6 +34,7 @@ const logElementGetting = false;
var motdShowCount = 3; var motdShowCount = 3;
var motd = ""; var motd = "";
var minMatchAmmount = 50; // TODO
// --------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------
// Class descriptions // Class descriptions
// --------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------
@ -63,6 +64,9 @@ class Question {
return this.HasQuestion() && this.HasAnswer(); return this.HasQuestion() && this.HasAnswer();
} }
Compare(q2) { Compare(q2) {
if (typeof q2 == 'string') {
return Question.CompareString(this.Q, q2);
} else {
const qmatchpercent = Question.CompareString(this.Q, q2.Q); const qmatchpercent = Question.CompareString(this.Q, q2.Q);
const amatchpercent = Question.CompareString(this.A, q2.A); const amatchpercent = Question.CompareString(this.A, q2.A);
if (this.I != undefined) { if (this.I != undefined) {
@ -72,6 +76,7 @@ class Question {
return (qmatchpercent + amatchpercent) / 2; return (qmatchpercent + amatchpercent) / 2;
} }
} }
}
static CompareString(s1, s2) { static CompareString(s1, s2) {
//if (s1 == undefined || s2 == undefined) //if (s1 == undefined || s2 == undefined)
// return 0; // return 0;
@ -101,6 +106,27 @@ class Subject {
AddQuestion(q) { AddQuestion(q) {
this.Questions.push(q); this.Questions.push(q);
} }
Search(q) {
var r = [];
for (var i = 0; i < this.length; i++) {
let percent = this.Questions[i].Compare(q);
if (percent > minMatchAmmount)
r.push({
q: this.Questions[i],
match: percent
});
}
for (var i = 0; i < r.length; i++)
for (var j = i; j < r.length; j++)
if (r[i].match < r[j].match) {
var tmp = r[i];
r[i] = r[j];
r[j] = tmp;
}
return r;
}
toString() { toString() {
var r = []; var r = [];
for (var i = 0; i < this.Questions.length; i++) for (var i = 0; i < this.Questions.length; i++)
@ -116,6 +142,21 @@ class QuestionDB {
get length() { get length() {
return this.Subjects.length; return this.Subjects.length;
} }
get activeIndexes() {
var r = [];
for (var i = 0; i < this.length; i++) {
if (GM_getValue("Is" + i + "Active")) {
r.push(i);
}
}
return r;
}
GetIfActive(ind) {
return GM_getValue("Is" + ind + "Active");
}
ChangeActive(i, value) {
GM_setValue("Is" + i + "Active", !!value);
}
AddQuestion(subj, q) { 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)
@ -128,6 +169,22 @@ class QuestionDB {
this.Subjects.push(n); this.Subjects.push(n);
} }
} }
Search(q) {
// TODO: image
var r = [];
for (var i = 0; i < this.length; i++)
r = r.concat(this.Subjects[i].Search(q));
for (var i = 0; i < r.length; i++)
for (var j = i; j < r.length; j++)
if (r[i].match < r[j].match) {
var tmp = r[i];
r[i] = r[j];
r[j] = tmp;
}
return r;
}
AddSubject(subj) { AddSubject(subj) {
var i = 0; var i = 0;
while (i < this.length && subj.Name != this.Subjects[i].Name) while (i < this.length && subj.Name != this.Subjects[i].Name)
@ -152,6 +209,7 @@ class QuestionDB {
// --------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------
function Main() { function Main() {
'use strict'; 'use strict';
console.clear();
Init(function(count) { Init(function(count) {
var url = location.href; var url = location.href;
try { try {
@ -628,9 +686,9 @@ function SearchInActiveSubjs(question, imgNodes, data) {
var result = []; // the results will be here var result = []; // the results will be here
for (var i = 0; i < data.length; i++) // going through data for (var i = 0; i < data.length; i++) // going through data
{ {
if (GM_getValue("Is" + i + "Active")) // if the current subject is marked as active if (data.GetIfActive(i)) // if the current subject is marked as active
{ {
result = result.concat(Search(question, imgNodes, data[i].questions)); result = result.concat(Search(question, imgNodes, data.Subjects[i]));
} }
} }
return result; return result;
@ -683,9 +741,11 @@ function HandleQuiz(url, count) {
var answers = []; var answers = [];
for (var j = 0; j < questions.length; j++) // going thru all answers for (var j = 0; j < questions.length; j++) // going thru all answers
{ {
// TODO: images, and other stuffs
var question = RemoveUnnecesarySpaces(questions[j]); // simplifying question var question = RemoveUnnecesarySpaces(questions[j]); // simplifying question
var result = SearchInActiveSubjs(question, imgNodes, data); console.log(question);
result = SortByPercent(result); // sorting result by percent console.log(SimplifyImages(imgNodes));
var result = data.Search(question);
PrepareAnswers(result, answers, j); PrepareAnswers(result, answers, j);
} }
ShowAnswers(answers); ShowAnswers(answers);
@ -713,25 +773,21 @@ function HandleUI(url, count) {
{ {
greetMsg = "Moodle/Elearning/KMOOC segéd v. " + GM_info.script.version + ". " + count + greetMsg = "Moodle/Elearning/KMOOC segéd v. " + GM_info.script.version + ". " + count +
" kérdés és " + data.length + " tárgy betöltve. (click for help)."; " kérdés és " + data.length + " tárgy betöltve. (click for help).";
if (data.length > 0 && data[0].name !== "NONAME") // if there is data, and its not named NONAME console.log(data);
if (data.length > 0)
{ {
var toAdd = []; // the subjects that will be displayed var toAdd = [];
for (var i = 0; i < data.length; i++) // going thru data for (var i = 0; i < data.length; i++) {
{ if (data.GetIfActive(i)) {
if (GM_getValue("Is" + i + "Active")) // if the i. item is active toAdd.push(data.Subjects[i].Name + " (" + data.Subjects[i].length + ")");
{
toAdd.push(data[i].name + " (" + data[i].count + ")"); // adds subject name and count to displayed items
} }
} }
if (toAdd.length != 0) // if there are active items if (toAdd.length != 0) {
{
greetMsg += "\nAktív tárgyak: " + toAdd.join(", ") + "."; greetMsg += "\nAktív tárgyak: " + toAdd.join(", ") + ".";
} else // if there are no active items selected } else {
{
greetMsg += "\nNincs aktív tárgyad. Menüből válassz ki eggyet!"; greetMsg += "\nNincs aktív tárgyad. Menüből válassz ki eggyet!";
} }
} else // if there are no subje loaded or it has no name } else {
{
greetMsg += " Az adatfájlban nem adtál meg nevet. Vagy nem elérhető a szerver. Katt a helpért!"; greetMsg += " Az adatfájlban nem adtál meg nevet. Vagy nem elérhető a szerver. Katt a helpért!";
// TODO if online mode print another error! // TODO if online mode print another error!
} }
@ -752,6 +808,7 @@ function HandleUI(url, count) {
"-re. Changelog:\n" + lastChangeLog; "-re. Changelog:\n" + lastChangeLog;
GM_setValue("lastVerson", GM_info.script.version); // setting lastVersion GM_setValue("lastVerson", GM_info.script.version); // setting lastVersion
} }
console.log("MOTD " + motd);
if (!EmptyOrWhiteSpace(motd)) { if (!EmptyOrWhiteSpace(motd)) {
var prevmotd = GM_getValue("motd"); var prevmotd = GM_getValue("motd");
@ -784,9 +841,10 @@ function HandleUI(url, count) {
function HandleResults(url) { function HandleResults(url) {
var allResults = []; var allResults = [];
// TODO fix this
if (data) if (data)
for (var i = 0; i < data.length; i++) // going trough all subjects for (var i = 0; i < data.length; i++) // going trough all subjects
if (GM_getValue("Is" + i + "Active")) // if the subject is active if (data.GetIfActive(i)) // if the subject is active
allResults = allResults.concat(data[i].questions); // searching for the answer, with the given question and image (if there is) allResults = allResults.concat(data[i].questions); // searching for the answer, with the given question and image (if there is)
SaveQuiz(GetQuiz(), allResults); // saves the quiz questions and answers SaveQuiz(GetQuiz(), allResults); // saves the quiz questions and answers
} }
@ -828,7 +886,6 @@ function ReadNetDB(cwith, useNetDB) {
synchronous: true, synchronous: true,
url: url, url: url,
onload: function(response) { onload: function(response) {
// console.log(response.responseText);
NLoad(response.responseText, cwith); NLoad(response.responseText, cwith);
}, },
onerror: function() { onerror: function() {
@ -859,7 +916,7 @@ function Load(cwith) {
function LoadMOTD(resource) { function LoadMOTD(resource) {
try { try {
motd = resource.substr; motd = resource.motd;
} catch (e) { } catch (e) {
console.log("Error loading motd :c"); console.log("Error loading motd :c");
console.log(e); console.log(e);
@ -871,21 +928,21 @@ function LoadMOTD(resource) {
function NLoad(resource, cwith) { function NLoad(resource, cwith) {
var count = -1; var count = -1;
try { try {
var data = JSON.parse(resource); var d = JSON.parse(resource);
var r = new QuestionDB(); var r = new QuestionDB();
var rt = []; var rt = [];
var allCount = -1; var allCount = -1;
LoadMOTD(resource); LoadMOTD(d);
for (var i = 0; i < data.Subjects.length; i++) { for (var i = 0; i < d.Subjects.length; i++) {
let s = new Subject(data.Subjects[i].Name); let s = new Subject(d.Subjects[i].Name);
var j = 0; var j = 0;
for (j = 0; j < data.Subjects[i].Questions.length; j++) { for (j = 0; j < d.Subjects[i].Questions.length; j++) {
var currQ = data.Subjects[i].Questions[j]; var currQ = d.Subjects[i].Questions[j];
s.AddQuestion(new Question(currQ.Q, currQ.A, currQ.I)); s.AddQuestion(new Question(currQ.Q, currQ.A, currQ.I));
} }
rt.push({ rt.push({
name: data.Subjects[i].Name, name: d.Subjects[i].Name,
count: j count: j
}); });
allCount += j; allCount += j;
@ -893,7 +950,7 @@ function NLoad(resource, cwith) {
} }
console.log(r); console.log(r);
console.log(rt); console.log(rt);
data = r;
count = allCount + 1; // couse starting with -1 to show errors count = allCount + 1; // couse starting with -1 to show errors
} catch (e) { } catch (e) {
console.log("------------------------------------------"); console.log("------------------------------------------");
@ -1649,7 +1706,7 @@ function ShowMenuList() {
var subjRow = tbl.insertRow(); var subjRow = tbl.insertRow();
var td = subjRow.insertCell(); var td = subjRow.insertCell();
var textBox = CreateNodeWithText(td, data[i].name + " (" + data[i].count + ")"); var textBox = CreateNodeWithText(td, data.Subjects[i].Name + " (" + data.Subjects[i].length + ")");
textBox.style.margin = fiveMargin; // fancy margin textBox.style.margin = fiveMargin; // fancy margin
td = subjRow.insertCell(); td = subjRow.insertCell();
@ -1659,14 +1716,14 @@ function ShowMenuList() {
checkbox.style.margin = "5px 5px 5px 5px"; // fancy margin checkbox.style.margin = "5px 5px 5px 5px"; // fancy margin
td.appendChild(checkbox); // adding text box to main td td.appendChild(checkbox); // adding text box to main td
var active = GM_getValue("Is" + i + "Active"); var active = data.GetIfActive(i);
checkbox.checked = active; checkbox.checked = active;
checkbox.setAttribute("id", "HelperTextNode" + i); checkbox.setAttribute("id", "HelperTextNode" + i);
checkbox.addEventListener("click", function() { checkbox.addEventListener("click", function() {
var checked = document.getElementById("HelperTextNode" + i).checked; var checked = document.getElementById("HelperTextNode" + i).checked;
GM_setValue("Is" + i + "Active", checked); data.ChangeActive(i, checked);
}); // adding click }); // adding click
} }
} else // if no data } else // if no data