WeBWorK Main Forum

can't find bug in this custom grader

can't find bug in this custom grader

by Christian Seberino -
Number of replies: 7
The problem file below works with other words except "change".

Webwork interprets "change" as "chan ge" for some reason and all
replies are marked wrong.



DOCUMENT();
loadMacros(
"PGstandard.pl",
"PGML.pl",
"MathObjects.pl",
"PGcourse.pl",
"parserNumberWithUnits.pl",
"contextArbitraryString.pl",
"parserPopUp.pl",
"contextInequalities.pl",
);
TEXT(beginproblem());
$showPartialCorrectAnswers = 1;

$menu = PopUp(["select one", "yes", "no"], "yes");

@string_answers = ("change");
$strings = Compute($string_answers[0])->cmp(checker => sub {
my ($temp, $response, $temp) = @_;
$response = $response->value;
foreach (@string_answers) {
if (lc($response) eq lc($_)) {
return 1;
}
}

return 0;
});

######################################################################

Context("ArbitraryString");

BEGIN_PGML
What one word refers to triangles containing right angles?

[____________]{$strings}
END_PGML

######################################################################
ENDDOCUMENT();

In reply to Christian Seberino

Re: can't find bug in this custom grader

by Davide Cervone -
You are missing the
    Context("ArbitraryString");
in order to activate ArbitraryString context.

Davide

In reply to Davide Cervone

Re: can't find bug in this custom grader

by Christian Seberino -
Thanks. I added Context("ArbitraryString"); and it still interprets "change" as "chan ge".

Here is what Webwork says...

Entered Answer Preview Result
chan ge chan incorrect

I accessed the problem by viewing it with the "Library Browser". I ran
it under CAPA and Local Problems libraries.




DOCUMENT();
loadMacros(
"PGstandard.pl",
"PGML.pl",
"MathObjects.pl",
"PGcourse.pl",
"parserNumberWithUnits.pl",
"contextArbitraryString.pl",
"parserPopUp.pl",
"contextInequalities.pl",
);
TEXT(beginproblem());
$showPartialCorrectAnswers = 1;

@string_answers = ("change");
$strings = Compute(@string_answers[0])->cmp(checker => sub {
my ($answers, $response, $temp) = @_;
foreach (@string_answers) {
if (lc($response) eq lc($_)) {
return 1;
}
}

return 0;
});

Context("ArbitraryString");

BEGIN_PGML
What one word describes what calculus is about?

[____________]{$strings}
END_PGML

ENDDOCUMENT();

In reply to Christian Seberino

Re: can't find bug in this custom grader

by Davide Cervone -
You must put the Context() statement before you create the $strings, because MathObjects retain the context in which they were created.

Davide
In reply to Davide Cervone

Re: can't find bug in this custom grader

by Christian Seberino -
Davide

Wow thanks. I never would have caught that. It works! Thanks!

Can I ask you one last Perl question? I can convert to lowercase with lc
but when I try to remove whitespace from the ends of the student answer
it appears to be ignored...

Context("ArbitraryString");
@string_answers = ("change");
$strings = Compute($string_answers[0])->cmp(checker => sub {
my ($temp1, $response, $temp2) = @_;
$response =~ s/^\s+|\s+$//g;
$response = lc($response);
foreach (@string_answers) {
if ($response eq lc($_)) {
return 1;
}
}

return 0;
});


The line: "$response =~ s/^\s+|\s+$//g;" doesn't seem to change $response.
Do you know why that wouldn't work!

cs

In reply to Christian Seberino

Re: can't find bug in this custom grader

by Davide Cervone -
This is because of how PG handles backslashes. In order to make it easier to include TeX notation in your problem, PG automatically doubles all backslashes before evaluating the expression, so that you can write
   BEGIN_TEXT
   What is \(\lim_{x\to\infty} \frac{1}{x}\) ?
   END_TEXT
rather than having to use
   BEGIN_TEXT
   What is \\(\\lim_{x\\to\\infty} \\frac{1}{x}\\) ?
   END_TEXT
which would make TeX much harder to write. But it has the side effect of making regular expressions more difficult to write, because
    $response =~ s/^\s+|\s+$//g;
becomes
    $response =~ s/^\\s+|\\s+$//g;
without your knowing it. This is why it isn't matching anything.

PG does provide a means of getting a single backslash, but it is ugly: you must use ~~ if I recall correctly. So you would need your regular expression to be

    $response =~ s/^~~s+|~~s+$//g;
if you include it in a .pg file.

Davide

In reply to Davide Cervone

Re: can't find bug in this custom grader

by Michael Gage -
If you move the custom grader to a .pl macro file at some point the convention
changes back to pure perl.  You need to use  double backslashes in TeX expressions and you must use single backslashes in regular expressions and to 
create references such as \@array.  

This frequently gives me pause for a few minutes when I move a routine that 
was embedded in a .pg problem to a macro .pl file for general use. It stops working until I remember to use the appropriate syntax for that file.

The rationale is that in .pg files you write a lot of TeX but not a lot of fancy perl so not having to type double backslashes is convenient, whereas in the macro files you write more perl than TeX.
In reply to Davide Cervone

Re: can't find bug in this custom grader

by Christian Seberino -
THANKS!! Finally SUCCESS!!! With ~~'s I can now strip whitespace and handle
differences in capitalization. Hopefully others will find this thread and use my custom grader as well.

Thanks again,

Chris