Skip to content

Commit

Permalink
Remove backend code evaluation fuzzy rule, and add frontend code eval…
Browse files Browse the repository at this point in the history
…uation to pencil code editor
  • Loading branch information
wxyxinyu committed Dec 28, 2015
1 parent 02723b7 commit 4721f7a
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 215 deletions.
115 changes: 1 addition & 114 deletions data/explorations/fuzzy_exploration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,119 +10,6 @@ schema_version: 9
skin_customizations:
panels_contents: {}
states:
code:
content:
- type: text
value: <p>Implement a function in Python to compute the Fibonacci sequence up
to some iteration N.</p>
interaction:
answer_groups:
- outcome:
dest: code
feedback:
- <p>Implementing the Fibonacci sequence recursively is quite straightforward.
However, can you try implementing it iteratively?</p>
param_changes: []
rule_specs:
- inputs:
training_data:
- code: "# Type your code here.\ndef fibonacci(N):\n if N<=1:return 1\n\
\ return fibonacci(N-1)+fibonacci(N-2)\n"
error: ''
evaluation: ''
output: '1,1,2,3,5,8,13,21,34,55
'
- code: "# Type your code here.\ndef fibonacci(N):\n if N<=1:return 1\n\
\ return fibonacci(N-2)+fibonacci(N-1)\n"
error: ''
evaluation: ''
output: '1,1,2,3,5,8,13,21,34,55
'
rule_type: FuzzyMatches
- outcome:
dest: graph
feedback:
- <p>Nice iterative solution! You can also implement it recursively! Let's
move on.</p>
param_changes: []
rule_specs:
- inputs:
training_data:
- code: "# Type your code here.\ndef fibonacci(N):\n table=[1,1]\n for\
\ i in range(2, N + 1):\n table.append(table[i-2]+table[i-1])\n\
\ return table[N]\n"
error: ''
evaluation: ''
output: '1,1,2,3,5,8,13,21,34,55
'
- code: "# Type your code here.\ndef fibonacci(N):\n table=[1,1]\n for\
\ i in range(2, N + 1):\n table.append(table[i-1]+table[i-2])\n\
\ return table[N]\n"
error: ''
evaluation: ''
output: '1,1,2,3,5,8,13,21,34,55
'
rule_type: FuzzyMatches
- outcome:
dest: graph
feedback:
- <p>I'm not quite sure how you solved it, but you have the correct output!</p>
param_changes: []
rule_specs:
- inputs:
x: 1,1,2,3,5,8,13,21,34,55
rule_type: OutputEquals
- outcome:
dest: code
feedback:
- <p>You should try and implement the function!</p>
param_changes: []
rule_specs:
- inputs:
training_data:
- code: "# Type your code here.\ndef fibonacci(N):\n return 0\n"
error: ''
evaluation: ''
output: '0,0,0,0,0,0,0,0,0,0,0
'
- code: "# Type your code here.\ndef fibonacci(N):\n return 0\n"
error: ''
evaluation: ''
output: '0,0,0,0,0,0,0,0,0,0,0
'
- code: "# Type your code here.\ndef fibonacci(N):\n return 0\n"
error: ''
evaluation: ''
output: '0,0,0,0,0,0,0,0,0,0
'
rule_type: FuzzyMatches
confirmed_unclassified_answers: []
customization_args:
language:
value: python
placeholder:
value: "# Type your code here.\ndef fibonacci(N):\n return 0\n"
postCode:
value: "print \"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\" % (\n fibonacci(0), fibonacci(1),\
\ fibonacci(2),\n fibonacci(3), fibonacci(4), fibonacci(5),\n fibonacci(6),\
\ fibonacci(7), fibonacci(8),\n fibonacci(9))"
preCode:
value: ''
default_outcome:
dest: code
feedback:
- <p>You may have an error in your code. Try again.</p>
param_changes: []
fallbacks: []
id: CodeRepl
param_changes: []
final:
content:
- type: text
Expand Down Expand Up @@ -392,7 +279,7 @@ states:
rows:
value: 1
default_outcome:
dest: code
dest: graph
feedback:
- <p>Is {{answer}} actually happy? I'm not so sure. Let's move on!</p>
param_changes: []
Expand Down
2 changes: 1 addition & 1 deletion extensions/interactions/CodeRepl/CodeRepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CodeRepl(base.BaseInteraction):
name = 'Code Editor'
description = 'Allows learners to enter code and get it evaluated.'
display_mode = base.DISPLAY_MODE_SUPPLEMENTAL
is_trainable = True
is_trainable = False
_dependency_ids = ['skulpt', 'codemirror']
answer_type = 'CodeEvaluation'
instructions = 'Type code in the editor'
Expand Down
194 changes: 99 additions & 95 deletions extensions/interactions/PencilCodeEditor/PencilCodeEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,110 +26,114 @@ oppia.directive('oppiaInteractivePencilCodeEditor', [
scope: {},
templateUrl: 'interaction/PencilCodeEditor',
controller: [
'$scope', '$attrs', '$element', '$timeout', 'focusService',
function($scope, $attrs, $element, $timeout, focusService) {
$scope.initialCode = oppiaHtmlEscaper.escapedJsonToObj(
$attrs.initialCodeWithValue);

var pce = new PencilCodeEmbed($element[0].children[0]);
pce.beginLoad($scope.initialCode);
pce.on('load', function() {
// Hide the turtle, and redefine say() to also write the text on the
// screen.
pce.setupScript([{
code: [
'ht();',
'',
'oldsay = window.say',
'say = function(x) {',
' write(x);',
' oldsay(x);',
'};'
].join('\n'),
type: 'text/javascript'
}]);

pce.hideToggleButton();
pce.setEditable();
pce.showEditor();

// Pencil Code automatically takes the focus on load, so we clear it.
focusService.clearFocus();
});

$scope.reset = function() {
pce.setCode($scope.initialCode);
};

var getNormalizedCode = function() {
// Converts tabs to spaces.
return pce.getCode().replace(/\t/g, ' ');
};

var errorIsHappening = false;
var hasSubmittedAnswer = false;

pce.on('startExecute', function() {
hasSubmittedAnswer = false;
});

pce.on('execute', function() {
if (errorIsHappening || hasSubmittedAnswer) {
return;
}

pce.eval('document.body.innerHTML', function(pencilCodeHtml) {
var normalizedCode = getNormalizedCode();
'$scope', '$attrs', '$element', '$timeout', 'focusService',
'codeReplRulesService',
function($scope, $attrs, $element, $timeout, focusService,
codeReplRulesService) {
$scope.initialCode = oppiaHtmlEscaper.escapedJsonToObj(
$attrs.initialCodeWithValue);

var pce = new PencilCodeEmbed($element[0].children[0]);
pce.beginLoad($scope.initialCode);
pce.on('load', function() {
// Hide the turtle, and redefine say() to also write the text on the
// screen.
pce.setupScript([{
code: [
'ht();',
'',
'oldsay = window.say',
'say = function(x) {',
' write(x);',
' oldsay(x);',
'};'
].join('\n'),
type: 'text/javascript'
}]);

pce.hideToggleButton();
pce.setEditable();
pce.showEditor();

// Pencil Code automatically takes the focus on load, so we clear
// it.
focusService.clearFocus();
});

$scope.reset = function() {
pce.setCode($scope.initialCode);
};

var getNormalizedCode = function() {
// Converts tabs to spaces.
return pce.getCode().replace(/\t/g, ' ');
};

// Get all the divs, and extract their textual content.
var output = $.map($(pencilCodeHtml).filter('div'), function(elem) {
return $(elem).text();
}).join('\n');
var errorIsHappening = false;
var hasSubmittedAnswer = false;

pce.on('startExecute', function() {
hasSubmittedAnswer = false;
});

pce.on('execute', function() {
if (errorIsHappening || hasSubmittedAnswer) {
return;
}

pce.eval('document.body.innerHTML', function(pencilCodeHtml) {
var normalizedCode = getNormalizedCode();

// Get all the divs, and extract their textual content.
var output = $.map(
$(pencilCodeHtml).filter('div'), function(elem) {
return $(elem).text();
}).join('\n');

console.log('Code (normalized): ');
console.log(normalizedCode);
console.log('Output: ');
console.log(output);
console.log('------');

hasSubmittedAnswer = true;
$scope.$parent.submitAnswer({
code: normalizedCode,
output: output || '',
evaluation: '',
error: ''
}, codeReplRulesService);
}, true);
});

console.log('Code (normalized): ');
pce.on('error', function(error) {
if (hasSubmittedAnswer) {
return;
}

var normalizedCode = getNormalizedCode();

console.log('Code: ');
console.log(normalizedCode);
console.log('Output: ');
console.log(output);
console.log('Error: ');
console.log(error.message);
console.log('------');

errorIsHappening = true;
hasSubmittedAnswer = true;
$scope.$parent.$parent.submitAnswer({

$scope.$parent.submitAnswer({
code: normalizedCode,
output: output || '',
output: '',
evaluation: '',
error: ''
});
}, true);
});

pce.on('error', function(error) {
if (hasSubmittedAnswer) {
return;
}

var normalizedCode = getNormalizedCode();

console.log('Code: ');
console.log(normalizedCode);
console.log('Error: ');
console.log(error.message);
console.log('------');

errorIsHappening = true;
hasSubmittedAnswer = true;

$scope.$parent.$parent.submitAnswer({
code: normalizedCode,
output: '',
evaluation: '',
error: error.message
});
error: error.message
}, codeReplRulesService);

$timeout(function() {
errorIsHappening = false;
}, 1000);
});
}]
$timeout(function() {
errorIsHappening = false;
}, 1000);
});
}]
};
}
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PencilCodeEditor(base.BaseInteraction):
name = 'Pencil Code Editor'
description = 'Allows learners to edit code in Pencil Code.'
display_mode = base.DISPLAY_MODE_SUPPLEMENTAL
is_trainable = True
is_trainable = False
_dependency_ids = ['pencilcode']
answer_type = 'CodeEvaluation'
instructions = 'Edit the code. Click \'Play\' to check it!'
Expand Down
4 changes: 0 additions & 4 deletions extensions/rules/code_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,3 @@ class ResultsInError(base.CodeEvaluationRule):
class ErrorContains(base.CodeEvaluationRule):
description = (
'has error message that contains {{x|UnicodeString}}')


class FuzzyMatches(base.CodeEvaluationRule):
description = 'is similar to {{training_data|ListOfCodeEvaluation}}'

0 comments on commit 4721f7a

Please sign in to comment.