Regular Expression in Quiz Question

Regular Expression in Quiz Question

Peter Broadfoot -
回帖数:9

I am impressed by the Joseph Rézeau plugin for the Regular Expression Short Answer question type. I was initially puzzled and surprised that it does not appear to support all standard regular expressions. 

I have only yesterday installed the plugin (Moodle 1.8.6) and read some of the forum threads – so apologies – I will not have a full grasp of the rationale behind the implementation.  My understanding is:  

  • The plugin is really aimed at providing appropriate feedback in the case of 'incorrect' answers.

  • Support for certain metacharacters (* and +) is not provided – because of a problem with providing hints and infinite matches.

Is there still a need, within core Moodle or via a plugin, for standard regular expressions? My instinct is Yes.  I would be very happy with a ShortAnswer / EmbeddedAnswer option to include a standard regular expression.  My concern is to provide flexible, correct or partially correct answers – it could be I have unrealistic expectations about what is achievable.

As an example, here's a regular expression provided by Joseph Rézeau for a maths question: What are the factors of 15?

See: http://moodle.org/mod/forum/discuss.php?d=79116

The factors are 1 3 5 15

(1 3 5 15|1,3,5,15|1,3,5 and 15|1, 3, 5 and 15)

That is probably similar to the Reg Ex that I would use and I would instruct students to write the list in increasing order, with numbers separated by a comma or space.  But how do you give marks for those who change the order as in 1 5 3 15, which is technically correct. I suspect that a full Reg Ex solution may not be possible – but it is, I think, certainly not possible using the Reg Ex Short Answer plugin – or if it is, please, show me, without using 48 permutations.

In order to achieve a compromise solution you may have to guess the likely incorrect answers.  It is not too difficult to check that 1, 3, 5 and 15 are all included in the answer – but how would you do that exclusively?  For example, the student's answer could be 1 3 5 6 9 10 12 15 (clearly wrong) and the Reg Ex must exclude the 6 9 10 12, otherwise the answer is marked 'correct'.

To exclude those numbers requires an expression something like:

(?!.*\b(6|9|10|12)\b)

 You could decide to filter other likely wrong answers. A possible common error is to list multiples such as 30, not factors, and so you could exclude every number from 1 to 30, except the correct values, 1, 3, 5 and 15.

I am no expert on regular expressions – maybe there is an easier way. Also, maybe this question on factors is not the best example, but the Reg Ex Short Answer question (which does not support ".*") does not seem to provide any possibility of implementing the sort of expression needed, where the keywords (in this example, the numbers) must all be present and order is not important.

Perhaps a better example is the flag colours question used by Joseph Rézeau.  You could ask for the three colours of a national flag, selected from a list of seven colours.  Eliminating the four incorrect colours using the regular expression is then feasible, in the event that an answer contains four or more colours, i.e. the correct three and one or more incorrect. Of course, you still need to check that the three correct colours are present (in any order). Also, I understand that in the original flag question, the order and the sentence construction were also important.

 

So, this is my question, which must have been asked before but I can find no answer. Is there a fairly simple way to modify the standard Short Answer code so that standard regular expressions are processed or, alternatively to modify the plugin code?  I am assuming (excuse my ignorance/ naivety) that the correct answer (in this case a regular expression) is held in the database and that perhaps only a few lines of code need to be changed.  One disadvantage would be the need to escape all metacharacters that form part of an answer – but I can live with that.  If anyone can point me in the right direction, I would appreciate.  Thank you.

 

 

 

 

 

 

 

 

 

回复Peter Broadfoot

本讨论区帖子已移除

本讨论区帖子的内容已移除,无法再访问。
回复Peter Broadfoot

Re: Regular Expression in Quiz Question

Joseph Rézeau -
Core developers的头像 Particularly helpful Moodlers的头像 Plugin developers的头像 Testers的头像 Translators的头像
Peter > I am impressed by the Joseph Rézeau plugin for the Regular Expression Short Answer question type.

Thanks.

> The plugin is really aimed at providing appropriate feedback in the case of 'incorrect' answers.

Not only, it is also aimed at detecting as many possible alternative correct answers as possible (and at providing Hints). Hence the decision to not use the metacharacters (* and +) since those would provide an infinite number of possible answers.

> My concern is to provide flexible, correct or partially correct answers – it could be I have unrealistic expectations about what is achievable.

It is also my concern to provide the best possible answer analysis and the most helpful feedback messages, but you are quite right that one must have realistic expectations. A lot can be achieved through careful use of regular expressions and a lot more can not realistically be achieved.

I am not at ease with maths examples, and I have originally created this plugin for my language teaching needs, so I'd rather stick to "everyday language" examples.

I do not think that what you want is easily achieved either by changing "a few lines" of code in the standard short answer question type or in the REGEXP plugin (which stands at around 1000 lines at the moment). If you could provide some "everyday language" examples of the kind of answer analysis you would like to be able to do, I might try to give it a go. Now would be the right time, because there are plans to make the REGEXP question type part of Moodle core in the future release (version 2.0). See Pierre Pichet's post in this thread.

All the best,

Joseph

回复Joseph Rézeau

Re: Regular Expression in Quiz Question

Peter Broadfoot -

Thank you to Pierre and Joseph for the replies.

Hi Joseph

Thank you for the detail.  I feel that I now understand more about your RegExp plugin, although there is still much to learn.

Here is an example question that requires an ordered response:

List the three primary colours in order of increasing wavelength.

The correct response is "blue, green and red".  It is similar to your flag colours question and can make use of the plugin's regular expression support to allow for different syntax.

It may be desirable to allocate 50% for an answer where the three colours are correctly identified but where the order is wrong.  That can be achieved using the plugin and the regular expression (blue, green and red|green, blue and red|green, red and blue|red, blue and green|red, green and blue).

There are many examples of questions that require a student to list key words in which the order does not matter.  For example, list the three distinct musical instruments used in a conventional string quartet.  This is similar to the primary colours question and, because there are only six permutations, the RegExp short answer is ideal.

An alternative solution is needed when the number of words increases.  For example, name the four bones of the human leg, including the thigh but not including the foot.  The answer is femur, patella, tibia and fibula.

It occurs to me that your Regular Expression Short Answer type is actually what could be called an Ordered List question type (not to be confused with an Ordering question type) that is enhanced by the possibility to include regular expressions in the answers.  As such, it is suited to answers in the form of a series of words or numbers in which the order is important.  I can see that it is ideal for sentence analysis.

It appears that my examples of unordered answers may require an Unordered List question type, which includes regular expression analysis and, probably, additional coding.  This may seem inconsistent with your RegExp Ordered List type.  It seems that there may be a need for the two question types to supplement the existing Short Answer.

There is, however, some common ground. The RegExp plugin appears to offer two possiblities:

  • support, through code, for special cases, such as the --.*word.* missing word expression and the provision of hints.
  • support through regular expressions.

Both ordered and unordered list questions therefore share these common features:

  • both types benefit from regular expressions in the answers.
  • the Unordered List type would benefit from additional coding to supplement the use of regular expressions, similar to the RegExp plugin additional --coding that, in conjunction with the Regex .*word.*, can test for a missing word.

Therefore, rather than having two question types, one for unordered and one for ordered answers, it would seem more logical (but also more problematic) to aim towards a full Regular Expression question type with additional code that supports both ordered list, with your extra features, and unordered lists.

 

In order to test my pure Regex solution to the unordered list problem, based on a 'lookahead' Regex, I have modified two lines of code in the plugin, one in expandregexp.php and one in questiontype.php.  This works. I can, for example, request an unordered list of the eight bones in the wrist.  Unlikely you would wish to do that, but it shows that it can be done with just eight Regex groupings, one for each bone, rather than all the possible (40,000) permutations.

 

In the first post I suggested the use of the regular expression (?!.*\b(orange|green|black|yellow)\b) as a way to prevent a match if the student answer contains one or more of those wrong words.  I can see that it would be preferable to include coding, as you have for the detection of missing words, to support this feature in the RegExp plugin.  Perhaps something similar to ++coding could be used to trigger the test for unwanted words.

 

An extension of this idea could be to check the number of words, so that if the correct answer is three key words, as in list the primary colours, and the student enters four words, the code provides a suitable response.

 

Therefore, my conclusion is that, although, as a temporary solution, I may pursue the pure Regex lookahead approach using a custom version of the RegExp plugin code, that allows the use of all metacharacters, a more satisfactory long-term solution would be the incorporation of code to process both unordered and ordered lists within your RegExp question type.

 

Regards, Peter

回复Peter Broadfoot

Re: Regular Expression in Quiz Question

Andreas Fink -
Hello together,

we use regex in Quiz Qestions to test configuration lines for our devices. This lines has commands and parameters.

The objective is to test if some mandatory parameters are used or not.

For this purpose a lookahead regexp works perfectly with out any changes in moodle 1.9.2+ since PHP uses Perl-Compatible Regular Expressions.


E.g. we want to test following config line:
config change -addr 1.2.3.4 -port 123 -mode basic

Answer 1: ^((?!-addr).)*$
Response: The param. -addr is missing in your config line. Try again.

Answer 2: ^((?!-port).)*$
Response: The param. -port is missing in your config line. Try again.

etc...

After all mandatory parameters are tested, we can match everything for a correct answer:

Answer10(the last one): .*
Response: The config line seems to be OK.


I used the reference for lookahead from this site: http://www.regular-expressions.info/completelines.html

Best Regards
Andreas Fink
回复Andreas Fink

Re: Regular Expression in Quiz Question

Joseph Rézeau -
Core developers的头像 Particularly helpful Moodlers的头像 Plugin developers的头像 Testers的头像 Translators的头像

Hello Andreas and thanks for your message...

When I first created the REGEXP question type I did not know about the lookahead possibility of regular expressions. So I added a special metacharacter (--) of my own for searching missing parts of speech in student's answer.

For example, when expecting the correct answer for the colors of the French flag "blue, white and red", to detect the absence of the word "red" I used:

--.*\bred\b.*/i

with lookahead you can use the equivalent:

((?!\bred\b).)*/i

From the point of view of advanced users who are very knowledgeable in regular expressions, the use of the lookahead is preferable ; but, from the point of view of the "ordinary" teacher, my ad hoc syntax is simpler, I think.

Joseph

回复Joseph Rézeau

Re: Regular Expression in Quiz Question

Jeff Forssell -
If I understand right, the big problems with keeping those wildcards are:
1) being able to give hints
2) being able to show the possible accepted answers

I think it would be good to have the way you have now as the default, but have the option to allow the wildcards (and at the same time disable 1 and 2 {or possibly just show a small sample of accepted patterns, if you could}).

I also felt like some possibilities were lost with those features.

At any rate it's great to hear that RegExp is going into core!
回复Jeff Forssell

Re: Regular Expression in Quiz Question

Joseph Rézeau -
Core developers的头像 Particularly helpful Moodlers的头像 Plugin developers的头像 Testers的头像 Translators的头像
Hi Jeff,
I have exchanged private e-mails with Peter and we agree that an "option" to switch on and off the wildcards may indeed be a solution. I will work on it (with a view of inclusion in 2.0)
Joseph
回复Peter Broadfoot

Re: Regular Expression in Quiz Question

Charles Krengel eLearn Solutions -
Here is an example of a regex question and answers where
(1) allowance is made for spelling mistakes
(2) >100% score is calculated based on one or two words appearing in the middle of a sentence.
Question
Name the city and country we live in:
Defined Answer 1: We live in Johannesburg, South Africa   (100%)
Defined Answer 2: .*j.*g.*South Africa.*   (90%)
Defined Answer 3: .*South Africa.*j.*g.*   (90%)
Defined Answer 4: .*j.*g .* (50%)
Defined Answer 5: .*S.*a.*  (50%)
 
Using the above question and defined answer settings the answer will be graded as follows:
 
Trainee answer 1: We live in Johannesburg, South Africa  (scores 100% based on Defined answer 1)
Trainee answer 2: I live in joohannneessg which is in South Africa (scores 90% based on Defined answer 2)
Trainee answer 3: its South Africa, johannesburg (scores 90% based on Defined answer 3)
Trainee answer 4: jahannesburg in africa (scores 50% - because South Africa is required and only achieves score based on Defined anser 4)
Trainee answer 5: its Souf addia (scores 50% - because it starts with S and ends with a as per defined answer 5).
回复Charles Krengel eLearn Solutions

Re: Regular Expression in Quiz Question

Joseph Rézeau -
Core developers的头像 Particularly helpful Moodlers的头像 Plugin developers的头像 Testers的头像 Translators的头像
Hi Charles,
I do not understand the point you are trying to make with those regex questions. As the creator of the REGEXP question type for Moodle I would not recommend using that kind of regular expressions, which can lead to all kind of "garbage answers" being accepted. From a pedagogical point of view I would not award a score of 90% to "I live in joohannneessg which is in South Africa" and certainly not award 50% to "its Souf addia" which is totally meaningless and deserves a plain 0%... If you have installed the REGEXP question type on your Moodle site, may I suggest you read the online help which gives a large number of examples showing how that question type can best be used?
All the best,
Joseph