Advanced question answering with new data structure

This commit is contained in:
YourFriendlyNeighborhoodDealer 2018-12-13 15:32:48 +01:00
parent 38185a3741
commit 599fac2928

69
main.js
View file

@ -63,14 +63,31 @@ class Question {
IsComplete() { IsComplete() {
return this.HasQuestion() && this.HasAnswer(); return this.HasQuestion() && this.HasAnswer();
} }
Compare(q2) { Compare(q2, i) {
if (typeof q2 == 'string') { if (typeof q2 == 'string') {
return Question.CompareString(this.Q, q2); var qmatchpercent = Question.CompareString(this.Q, q2);
if (i)
return qmatchpercent;
else {
if (this.HasImage()) {
const imatchpercent = this.HasImage() ? Question.CompareString(this.I.join(" "), i.join(" ")) :
0;
return (qmatchpercent + imatchpercent) / 2;
} else {
qmatchpercent -= 30;
if (qmatchpercent < 0)
return 0;
else
return qmatchpercent;
}
}
} else { } 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) {
const imatchpercent = this.I == undefined ? Question.CompareString(this.I, q2.I) : 0; const imatchpercent = this.I == undefined ? Question.CompareString(this.I.join(" "), q2.I.join(
" ")) : 0;
return (qmatchpercent + amatchpercent + imatchpercent) / 3; return (qmatchpercent + amatchpercent + imatchpercent) / 3;
} else { } else {
return (qmatchpercent + amatchpercent) / 2; return (qmatchpercent + amatchpercent) / 2;
@ -106,10 +123,10 @@ class Subject {
AddQuestion(q) { AddQuestion(q) {
this.Questions.push(q); this.Questions.push(q);
} }
Search(q) { Search(q, img) {
var r = []; var r = [];
for (var i = 0; i < this.length; i++) { for (var i = 0; i < this.length; i++) {
let percent = this.Questions[i].Compare(q); let percent = this.Questions[i].Compare(q, img);
if (percent > minMatchAmmount) if (percent > minMatchAmmount)
r.push({ r.push({
q: this.Questions[i], q: this.Questions[i],
@ -169,11 +186,11 @@ class QuestionDB {
this.Subjects.push(n); this.Subjects.push(n);
} }
} }
Search(q) { Search(q, img) {
// TODO: image
var r = []; var r = [];
for (var i = 0; i < this.length; i++) for (var i = 0; i < this.length; i++)
r = r.concat(this.Subjects[i].Search(q)); if (this.GetIfActive(i))
r = r.concat(this.Subjects[i].Search(q, img));
for (var i = 0; i < r.length; i++) for (var i = 0; i < r.length; i++)
for (var j = i; j < r.length; j++) for (var j = i; j < r.length; j++)
@ -183,6 +200,7 @@ class QuestionDB {
r[j] = tmp; r[j] = tmp;
} }
console.log(r); // TODO remove dis
return r; return r;
} }
AddSubject(subj) { AddSubject(subj) {
@ -694,34 +712,33 @@ function SearchInActiveSubjs(question, imgNodes, data) {
return result; return result;
} }
function PrepareAnswers(result, answers, j) { function PrepareAnswers(result, j) {
if (result.length > 0) // if there are more than zero results if (result.length > 0) // if there are more than zero results
{ {
var allMessages = []; // preparing all messages var allMessages = []; // preparing all messages
// this should be a function but i dont use this anywhere else
for (var k = 0; k < result.length; k++) // going throuh all results for (var k = 0; k < result.length; k++) // going throuh all results
{ {
var msg = ""; // the current message var msg = ""; // the current message
if ((GM_getValue("showQuestions") == undefined) || GM_getValue("showQuestions")) // if the question should be shown if ((GM_getValue("showQuestions") == undefined) || GM_getValue("showQuestions")) // if the question should be shown
{ {
msg += result[k].q + "\n"; // adding the question if yes msg += result[k].q.Q + "\n"; // adding the question if yes
} }
msg += result[k].a; // adding answer msg += result[k].q.A; // adding answer
if (result[k].i) // and adding image, if it exists if (result[k].q.HasImage()) // and adding image, if it exists
{ {
msg += "\n" + result[k].i; // if it has image part, adding that too msg += "\n" + result[k].q.I; // if it has image part, adding that too
} }
allMessages.push({ allMessages.push({
m: msg, m: msg,
p: result[k].p p: result[k].match
}); });
} }
answers.push(allMessages); return allMessages;
HighLightAnswer(result, j); // highlights the answer for the current result
} }
} }
function ShowAnswers(answers) { function ShowAnswers(answers) {
// TODO: alll / current counter
if (answers.length > 0) { // if there are more than 0 answer if (answers.length > 0) { // if there are more than 0 answer
ShowMessage(answers); ShowMessage(answers);
} else { } else {
@ -743,10 +760,9 @@ function HandleQuiz(url, count) {
{ {
// TODO: images, and other stuffs // TODO: images, and other stuffs
var question = RemoveUnnecesarySpaces(questions[j]); // simplifying question var question = RemoveUnnecesarySpaces(questions[j]); // simplifying question
console.log(question); var result = data.Search(question, SimplifyImages(imgNodes));
console.log(SimplifyImages(imgNodes)); answers.push(PrepareAnswers(result, j));
var result = data.Search(question); HighLightAnswer(result, j); // highlights the answer for the current result
PrepareAnswers(result, answers, j);
} }
ShowAnswers(answers); ShowAnswers(answers);
} }
@ -774,8 +790,7 @@ 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).";
console.log(data); console.log(data);
if (data.length > 0) if (data.length > 0) {
{
var toAdd = []; var toAdd = [];
for (var i = 0; i < data.length; i++) { for (var i = 0; i < data.length; i++) {
if (data.GetIfActive(i)) { if (data.GetIfActive(i)) {
@ -1345,6 +1360,7 @@ function RemoveLetterMarking(inp) {
// highlights the possible solutions to the current question // highlights the possible solutions to the current question
function HighLightAnswer(results, currQuestionNumber) { function HighLightAnswer(results, currQuestionNumber) {
// TODO
try { try {
if (results.length > 0) // if there are items in the result if (results.length > 0) // if there are items in the result
{ {
@ -1355,7 +1371,7 @@ function HighLightAnswer(results, currQuestionNumber) {
{ {
if (answers[i].tagName && answers[i].tagName.toLowerCase() == "div") // if its not null and is "div" if (answers[i].tagName && answers[i].tagName.toLowerCase() == "div") // if its not null and is "div"
{ {
var correct = results[0].a.toLowerCase(); // getting current correct answer from data var correct = results[0].q.A.toLowerCase(); // getting current correct answer from data
var answer = answers[i].innerText.replace(/\n/g, "").toLowerCase(); // getting current answer var answer = answers[i].innerText.replace(/\n/g, "").toLowerCase(); // getting current answer
// removing stuff like "a." // removing stuff like "a."
@ -1368,7 +1384,7 @@ function HighLightAnswer(results, currQuestionNumber) {
} }
} }
} }
if (results[0].p == 100) // if the result is 100% correct if (results[0].match == 100) // if the result is 100% correct
if (type !== "radio" || toColor.length == 1) // if the type is not radio or there is exactly one correct solution if (type !== "radio" || toColor.length == 1) // if the type is not radio or there is exactly one correct solution
for (var i = 0; i < toColor.length; i++) // going through "toColor" for (var i = 0; i < toColor.length; i++) // going through "toColor"
answers[toColor[i]].style.backgroundColor = "#8cff66"; // and coloring the correct index answers[toColor[i]].style.backgroundColor = "#8cff66"; // and coloring the correct index
@ -1706,7 +1722,8 @@ function ShowMenuList() {
var subjRow = tbl.insertRow(); var subjRow = tbl.insertRow();
var td = subjRow.insertCell(); var td = subjRow.insertCell();
var textBox = CreateNodeWithText(td, data.Subjects[i].Name + " (" + data.Subjects[i].length + ")"); 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();