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

71
main.js
View file

@ -63,14 +63,31 @@ class Question {
IsComplete() {
return this.HasQuestion() && this.HasAnswer();
}
Compare(q2) {
Compare(q2, i) {
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 {
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;
const imatchpercent = this.I == undefined ? Question.CompareString(this.I.join(" "), q2.I.join(
" ")) : 0;
return (qmatchpercent + amatchpercent + imatchpercent) / 3;
} else {
return (qmatchpercent + amatchpercent) / 2;
@ -106,10 +123,10 @@ class Subject {
AddQuestion(q) {
this.Questions.push(q);
}
Search(q) {
Search(q, img) {
var r = [];
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)
r.push({
q: this.Questions[i],
@ -169,11 +186,11 @@ class QuestionDB {
this.Subjects.push(n);
}
}
Search(q) {
// TODO: image
Search(q, img) {
var r = [];
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 j = i; j < r.length; j++)
@ -183,6 +200,7 @@ class QuestionDB {
r[j] = tmp;
}
console.log(r); // TODO remove dis
return r;
}
AddSubject(subj) {
@ -694,34 +712,33 @@ function SearchInActiveSubjs(question, imgNodes, data) {
return result;
}
function PrepareAnswers(result, answers, j) {
function PrepareAnswers(result, j) {
if (result.length > 0) // if there are more than zero results
{
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
{
var msg = ""; // the current message
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
if (result[k].i) // and adding image, if it exists
msg += result[k].q.A; // adding answer
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({
m: msg,
p: result[k].p
p: result[k].match
});
}
answers.push(allMessages);
HighLightAnswer(result, j); // highlights the answer for the current result
return allMessages;
}
}
function ShowAnswers(answers) {
// TODO: alll / current counter
if (answers.length > 0) { // if there are more than 0 answer
ShowMessage(answers);
} else {
@ -743,10 +760,9 @@ function HandleQuiz(url, count) {
{
// TODO: images, and other stuffs
var question = RemoveUnnecesarySpaces(questions[j]); // simplifying question
console.log(question);
console.log(SimplifyImages(imgNodes));
var result = data.Search(question);
PrepareAnswers(result, answers, j);
var result = data.Search(question, SimplifyImages(imgNodes));
answers.push(PrepareAnswers(result, j));
HighLightAnswer(result, j); // highlights the answer for the current result
}
ShowAnswers(answers);
}
@ -774,8 +790,7 @@ function HandleUI(url, 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).";
console.log(data);
if (data.length > 0)
{
if (data.length > 0) {
var toAdd = [];
for (var i = 0; i < data.length; i++) {
if (data.GetIfActive(i)) {
@ -808,7 +823,7 @@ function HandleUI(url, count) {
"-re. Changelog:\n" + lastChangeLog;
GM_setValue("lastVerson", GM_info.script.version); // setting lastVersion
}
console.log("MOTD " + motd);
console.log("MOTD " + motd);
if (!EmptyOrWhiteSpace(motd)) {
var prevmotd = GM_getValue("motd");
@ -1345,6 +1360,7 @@ function RemoveLetterMarking(inp) {
// highlights the possible solutions to the current question
function HighLightAnswer(results, currQuestionNumber) {
// TODO
try {
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"
{
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
// 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
for (var i = 0; i < toColor.length; i++) // going through "toColor"
answers[toColor[i]].style.backgroundColor = "#8cff66"; // and coloring the correct index
@ -1706,7 +1722,8 @@ function ShowMenuList() {
var subjRow = tbl.insertRow();
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
td = subjRow.insertCell();