Code Monkey home page Code Monkey logo

gift-grammar-peg.js's People

Contributors

dependabot[bot] avatar ethan-ou avatar fuhrmanator avatar sho-ki avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

gift-grammar-peg.js's Issues

Trouble with explicit newlines, using `\n`

Consider the following input:

[html]Blah\nblah $ 1 \\neq 0 $ {}

On peggyjs.org/online it parses as:

[
  {
    type: 'Essay',
    title: null,
    stem: {
      format: 'html',
      text: 'Blah\\nblah $ 1 \\neq 0 $ '
    },
    hasEmbeddedAnswers: false,
    globalFeedback: null,
    id: null,
    tags: null
  }
]

We can see that the \n is now escaped, which seems very much like a bug. The current editor tries to replace the \n with <br/>, but since the string is actually \\n (contains a real backslash rather than a newline), the display code breaks the \\neq string in the katex expression.

Probably we need more tests for these cases, as Moodle allows using \n in GIFT especially with [html] and [markdown] types.

Matching Questions

There is some strange behaiviour in matching questions, e.g.:

Please match the following\:
{
  =abc -> ...
  =xyz -> , . ,
}

Symbols like . and , lead to errors in the parser, however don't show where the error is (e.g. give line number), as done with other errors. I am not sure if this is "correct". I tried the Moodle Import for the above question and it works correctly.

Description questions can have a title

The current parser doesn't parse the title of this description question (exported from Moodle) into the title object (it gets parsed into the stem):

// question: 279223  name: Description Title
::Description Title::[html]Here's a description for the next question.

Title is missing in the grammar for Description.

Escaped colon "\:" not supported

The following question won't parse (:'s need to be escaped in GIFT).

// multiple choice with multiple good answer (checkbox)
Indicate two people are entombed in Grant's tomb\: {
~%-100%No one
~%50%Grant # yup its good
~%50%Grant's wife
~%-100%Grant's father
}

Short answer type not supported

Despite all the short-answer tests (that appear to pass), this format is producing "MC" type questions. The grammar doesn't define Short Answer anywhere.

According to this reference:

Answers in Short Answer question-type are all prefixed by an equal sign (=),
indicating that they are all correct answers. The answers must not contain a
(~). If there is only one correct Short Answer, it may be written without the
equal sign prefix, as long as it cannot be confused as True-False. For short
answer, feedback is shown only when students input the
corresponding correct answer.

Global feedback

Currently global feedback (Rétroaction générale) don't get parsed (in numerical questions at least).

GIFT:

// question: 229196  name: Test Numerical
::Test Numerical::Whats two plus 4?{#
	=%100%6:0#Good job its really 6!
	####2+4 \= 6
}

Result

Line 2, column 36: Expected {#... }, {= match1 -> Match1 ...}, {=correct choice ~incorrect choice ... }, or {T} or {F} or {True} or {False} but "{" found.

Expected

[
   {
      "type": "Numerical",
      "title": "Test Numerical",
      "stem": "Whats two plus 4?",
      "hasEmbeddedAnswers": false,
      "choices": [
         {
            "isCorrect": true,
            "weight": 100,
            "text": {
               "type": "range",
               "number": 6,
               "range": 0
            },
            "feedback": "Good job its really 6!",
            "globalFeedBack": "2+4 = 6"
         }
      ]
   }
]

On my side this issues don't need to be fixed. Just wanted to notify you.

Support GIFT format completely

Tests for each format

See http://nathansuniversity.com/pegs7.html for an explanation, but essentially:

var PEG = require('pegjs');
var assert = require('assert');
var fs = require('fs'); // for loading files

// Read file contents
var data = fs.readFileSync('my.peg', 'utf-8');
// Show the PEG grammar file
console.log(data);
// Create my parser
var parse = PEG.buildParser(data).parse;
// Do a test
assert.deepEqual( parse("(a b c)"), ["a", "b", "c"] );

Explode individual examples from https://github.com/fuhrmanator/GIFT-grammar-PEG.js/blob/master/Moodle%20test%20input%20examples.txt into separate files, and perform assert.deepEqual on each.

Parsing fails with negative value in numerical questions

When using a numerical question, having a negative value is considered an error where it shouldn't be.

Here is an example:

import { parse } from "gift-pegjs";

const question = "What is the value of -1? {#-1}."

const quiz = parse(question)

some parse problems

It breaks with this: <!-- -->

::EE_mod_05_T39_P079::[html]<p>¿En qué supuestos podría el Fiscal autorizar a la Policía Judicial la práctica de una diligencia de entrada y registro?</p>{
	~[moodle]En ausencia del Juez de Instrucción, titular de la actuaciones, y por razones de urgencia, debidamente acreditadas.
	~[moodle]Cuando se trate de delitos de terrorismo.
	=[moodle]Nunca.
	####<p><span style\="color\: \#ff6600;"><strong>Artículo 5 Estatuto Orgánico del Ministerio Fiscal\:</strong></span></p>\n<p><em><strong>Dos.</strong> Igualmente, y para el esclarecimiento de los hechos denunciados o que aparezcan en los atestados de los que conozca, <span style\="background-color\: \#ffcc99;">puede llevar a cabo u ordenar aquellas diligencias para las que esté legitimado según la <span style\="color\: \#000000;"><a href\="http\://www.formacioncnp.es/base_datos/Penal/lecr.html"><span style\="color\: \#000000; background-color\: \#ffcc99;"><!--<var>LE0000017566_20130224</var>-->Ley de Enjuiciamiento Criminal</span></a></span>, las cuales no podrán suponer la adopción de medidas cautelares o limitativas de derechos. No obstante, podrá ordenar el Fiscal la detención preventiva</span>.</em></p>\n<p> </p>
}

Bracket characters are not escaped when output from Moodle in GIFT

Consider the test file:

::GIFT range of non-escaped symbols::
Here's a question that has symbols inside,
such as `-\=\[\];',./\~!@\#$%^&*()_+\{\}|\:"<>? {TRUE}

When imported into Moodle, the text is different from:

"stem": {"format":"moodle", "text":"Here's a question that has symbols inside, such as `-=[];',./~!@#$%^&*()_+{}|:\"<>?"},

That is, the brackets [ and ] have \ before them in Moodle, implying they don't need escaping. Indeed, removing the \ and re-exporting to GIFT shows they don't take \ at all. This is a bad test file (and the grammar needs to be fixed).

Error handling

I just installed gift-pegjs in a Electron program and although it works perfectly, I miss being able to locate in which line or question parsing error occurs. I have searched but found no references to error management. Where could I find documentation?

Best regards

Alfonso

Validate acceptable weights for multiple-choice answers

Moodle will refuse GIFT imports of multiple choice questions if answer weights don't match ones on the list, e.g.:

image

So, it means that a question like the following will fail to import, because it's not using the precise weights of -33.33333:

Pick the best answers: {
~%-33%A
~%-33%B
=C
~%-34%D
}

We could add to the grammar options for the Moodle-specific values so as to flag the error before importing.

Bug-True or false with 2 feedback

Context

True or false with feedback for positive and negative answer dont get parse correctly:

Exemple:

Grant is buried in Grant's tomb.{FALSE#wrong answer feedback#right answer feedback}

Result JSON:

[
   {
      "type": "TF",
      "title": null,
      "stem": "Grant is buried in Grant's tomb.",
      "hasEmbeddedAnswers": false,
      "isTrue": false,
      "feedBack1": "right answer feedback",
      "feedBack2": undefined
   }
]

Expected JSON:

[
   {
      "type": "TF",
      "title": null,
      "stem": "Grant is buried in Grant's tomb.",
      "hasEmbeddedAnswers": false,
      "isTrue": false,
      "feedBack1": "right answer feedback",
      "feedBack2": "wrong answer feedback"
   }
]

Switching to Peggy

Since the original PEG.js hasn't been maintained in a while, a new fork of the project called Peggy was created by the community. It appears to be a drop-in replacement, but will require a bit of testing to see whether previous PEG plugins are still compatible with this fork.

some parse problems

->

// question: 882007  name: iebT26nuevo_P012
::iebT26nuevo_P012::¿Cómo se cambia la orientación de la página en Word 2003 y 2007?\:{
	~todas las versiones de Word cambian la orientación de página igual (archivo->diseño de página)
	~-archivo->configurar->orientación ó -archivo->configurar->diseño de página
	=-archivo-configurar página ó -diseño de página-orientación
}

Consider look-ahead approach to parsing

Alternative way to parse text, using look-ahead. This might ultimately allow for better syntax errors, as the capturing with "." is more generous.

Here's a prototype that works for T/F.

{
  function joinLookAheadCapture(o) {
	var s = "";
	for (var i = 0; i < o.length; i++) {
	  s += o[i][1];
	}
	return s;
  }
}

Question = Title? QuestionText Answer

Title "Title" 
  = TitleStart title:(!TitleEnd .)* TitleEnd { return joinLookAheadCapture(title) }
TitleStart = "::"
TitleEnd = "::"

QuestionText "Text" = answer:(!AnswerStart .)* { return joinLookAheadCapture(answer) }

Answer "Answer" 
  = AnswerStart answer:AnswerContent AnswerEnd { return answer}

AnswerContent "Answer variants" = AnswerTF

AnswerTF "T/F Answer"
  = answer:(TrueAnswer / FalseAnswer ) fb1:FeedbackIncorrectTF? fb2:FeedbackCorrectTF? { return [answer, fb1, fb2]}

TrueAnswer "TRUE" = "TRUE"i
FalseAnswer "FALSE" = "FALSE"i

FeedbackIncorrectTF = FeedbackStart feedback:(!(FeedbackStart / AnswerEnd) .)* { return joinLookAheadCapture(feedback) }
FeedbackCorrectTF = FeedbackStart feedback:(!AnswerEnd .)* { return joinLookAheadCapture(feedback) }
FeedbackStart "Feedback start" = "#"

AnswerStart = "{"
AnswerEnd = "}"

Transforming JSON to GIFT

I wanted to open an issue to start discussing how we can transform the parsed JSON back into GIFT text. Doing so would allow things like changing question types (like changing from multiple choice to matching questions in the editor) and even open the possibility of building a standalone editor that exports GIFT as a byproduct.

We're both pretty busy so I'm happy to just leave the idea here and we can work on it async when we've got some time.

Mocha tests fail under Linux because a \n (instead of a ' ') is in some strings

Here's an example of the output from https://travis-ci.org/fuhrmanator/GIFT-grammar-PEG.js/jobs/402803321

21 passing (197ms)
3 failing

  1. GIFT parser harness:
    parsing GIFT question: ./tests/questions/giftFormatPhpExamples.gift:
    AssertionError [ERR_ASSERTION]: Deep equal does not match

    • expected - actual
      }
      ]
      "stem": {
      "format": "moodle"
    •  "text": "Match the following countries with their corresponding\ncapitals."
      
    •  "text": "Match the following countries with their corresponding capitals."
      
      }
      "title": [null]
      "type": "Matching"
      }

    at Context. (tests/all-questions-harness.js:34:20)

  2. GIFT parser harness:
    parsing GIFT question: ./tests/questions/multiLineFeedback1.gift:
    AssertionError [ERR_ASSERTION]: Deep equal does not match

    • expected - actual
      }
      {
      "feedback": {
      "format": "moodle"
    •      "text": "feedback comment on this wrong answer\non multiple lines"
      
    •      "text": "feedback comment on this wrong answer on multiple lines"
         }
         "isCorrect": false
         "text": {
           "format": "moodle"
      

--
}
]
"globalFeedback": {
"format": "moodle"
- "text": "Global feedback split\non multiple lines"
+ "text": "Global feedback split on multiple lines"
}
"hasEmbeddedAnswers": false
"stem": {
"format": "moodle"

  at Context.<anonymous> (tests/all-questions-harness.js:34:20)
  1. GIFT parser harness:
    parsing GIFT question: ./tests/questions/options1.gift:
    AssertionError [ERR_ASSERTION]: Deep equal does not match
    • expected - actual
      "globalFeedback": [null]
      "hasEmbeddedAnswers": false
      "stem": {
      "format": "moodle"
    •  "text": "Japanese characters originally\ncame from what country?"
      
    •  "text": "Japanese characters originally came from what country?"
      
      }
      "title": "Kanji Origins"
      "type": "MC"
      }
      --
      "globalFeedback": [null]
      "hasEmbeddedAnswers": true
      "stem": {
      "format": "moodle"
    •  "text": "The American holiday of Thanksgiving is\ncelebrated on the _____ Thursday of November."
      
    •  "text": "The American holiday of Thanksgiving is celebrated on the _____ Thursday of November."
      
      }
      "title": "Thanksgiving Date"
      "type": "MC"
      }

some parse problems

it breacks with ::title\:::stem{

::Al concebido\:::[html]<p>Al concebido\:</p>{
	~[moodle]Se le tiene por nacido para todos los efectos que le sean favorables o desfavorables
	=[moodle]Se le tiene por nacido para todos los efectos que le sean favorables
	~[moodle]El Código Civil no se refiere a este supuesto
	####<p><span style\="color\: \#ff0000;"><strong>Artículo 29 Código Civil\:</strong></span></p>\n<p><em>El nacimiento determina la personalidad; pero <span style\="background-color\: \#ffcc99;">el concebido se tiene por nacido para todos los efectos que le sean favorables</span>, siempre que nazca con las condiciones que expresa el artículo siguiente.</em></p>\n<p> </p>
}

Publish a parser to npm

It would be handy to publish a parser generated from the grammar on npm. Maybe it could be named simply gift-parser or gift-js maybe a reference to peg.js should be included.

Including Typescript types in package.json

Hello hello. I believe the types aren't working at the moment since they aren't being included in the final NPM package. We'll need to copy them over to the final package for them to work.

It should be easy for us to do this by including the types in the files section of the package.json.

Mixed case True and False are not valid in Moodle

Importing the following question into Moodle will result in a short answer question, not a true/false question:

Question {True}

The current parser based on the grammar recognizes it as True/False.

The GIFT format supported by Moodle requires all capitals: {T} {TRUE} {F} {FALSE}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.