regular expressions question

regular expressions question

by Jeroen Hollander -
Number of replies: 9

Hello,

I'm using the gap fill question type (fill in the blanc) to have students give the Spanish translation for some words.

I want to use regular expressions to check 2 things:
a. the article (el / la) is optional, but when entered it must be correct.
b. student can type the word with or without accents.

The first question works okay: the horse --> el cabello ==> (el )?caballo
I can type the answer with or without the article, and using "la" marks the answer as wrong.

The next question doesn't work okay: the bird --> el pájaro ==> (el )?p[á|a]jaro
The answers "pajaro" and "pájaro" are marked correct. That is okay.
Also "el pajaro" and "el pájaro" are marked correct. That is okay too.
But repacing "el" with ANY other wordt marks to answer as correct too. That is not okay.

So I assume the problem is in using both ()? and [|] expression in one question. 
How can I fix this?

Thank you in advance.

Best regards,

Jeroen




Average of ratings: -
In reply to Jeroen Hollander

Re: regular expressions question

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Hi Jeroen,

Here's the solution:

1.- In your gapfill question go to More Options -> Delimit characters and select a delimiter that is different from the default [ ], e.g. { }

2.- The following coding works:

the horse --> el cabello ==> {(el )?caballo}

the bird --> el pájaro ==> {(el )?p[á|a]jaro}

That is explained in the excellentwink documentation here: https://docs.moodle.org/38/en/Gapfill_question_type#Regular_Expressions

Average of ratings: Useful (2)
In reply to Joseph Rézeau

Re: regular expressions question

by Jeroen Hollander -

Thank you for your reply.

I was already using different delimiters smile

#(el )?caballo#
#(el )?p[á|a]jaro#

The main problem is that "el" can be replaced with any other word in the second question (although the syntax is quite similair like in the first question).

Hope you can help me.


Jeroen

In reply to Jeroen Hollander

Re: regular expressions question

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators
Sorry, Jeroen,
I had not tested my solution well enough. I will have another look as soon as possible.
In reply to Joseph Rézeau

Re: regular expressions question

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

There is definitely something wrong with that regular expression analysis. I seems to be the space in the first optional element that causes the problem.

I leave it to the plugin author, Marcus, to fix this bug.

Average of ratings: Useful (1)
In reply to Joseph Rézeau

Re: regular expressions question

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
It is a bug caused by a repurposing of the | operator.
The clue comes from this line of code
$answerparts = explode("|", $answer);
There is a bit of a work around like this
#(el )?p[áa]jaro#
But it means the | operator cannot be used as you would expect in regex sad
Not sure I can fix this without breaking a large number of existing questions.
Average of ratings: Useful (2)
In reply to Marcus Green

Re: regular expressions question

by Jeroen Hollander -

Hello Marcus,


Thank you for looking into this.

Your suggested solution is a okay for the moment.


Thanks again.


Jeroen

In reply to Jeroen Hollander

Re: regular expressions question

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
My apologies that this issue has taken up your time. Not many people use the regex capability so it doesn't get tested as much as it should. I will add a unit test that docments the effect of the way I repurpose the | operator and also add a note to the documentation of this issue.
In the documentation I mention two other question types that use regular expressions you might be interested in, see
https://docs.moodle.org/38/en/Gapfill_question_type#Regular_Expressions
One of which is maintained by Joseph
In reply to Marcus Green

Re: regular expressions question

by Jeroen Hollander -
Hallo Marcus,

I'm back again with another "problem".
We'd like our students to translate a sentence into Spanish.
The current answer is: #Veis [el|la] m[aá]s grande#
But it doesn't work. Both "mas" and "más" are accepted, but I can't get a check on "el" or "la" to work.
Do you have any suggestions?