Browse Source

Fix output matching bug where numeric val would not be converted to decimal.js during string comparison

Fix output matching bug where bool value would not be removed from string after matchin
Lucas de Souza 4 years ago
parent
commit
2ffdab6ccf

+ 15 - 11
js/assessment/output_matching/output_matching.js

@@ -20,12 +20,12 @@ export class OutputMatching {
   }
 
   static get BOOLEAN_REGEX () {
-    const str = `^(${LocalizedStrings.getUI("logic_value_true")}|${LocalizedStrings.getUI("logic_value_true")})$`;
+    const str = `^(${LocalizedStrings.getUI("logic_value_true")}|${LocalizedStrings.getUI("logic_value_false")})$`;
     return new RegExp(str);
   }
 
   static get BOOLEAN_IN_STRING_REGEX () {
-    const str = `(${LocalizedStrings.getUI("logic_value_true")}|${LocalizedStrings.getUI("logic_value_true")})`;
+    const str = `(${LocalizedStrings.getUI("logic_value_true")}|${LocalizedStrings.getUI("logic_value_false")})`;
     return new RegExp(str, 'g');
   }
   
@@ -99,7 +99,7 @@ export class OutputMatching {
     e_num = new Decimal(e_num.toFixed(decimalPlaces, Decimal.ROUND_FLOOR));
     const result = g_num.eq(e_num);
     const grade = result ? 1 : 0;
-    return OutputResult.createNumberResult(e_num, g_num, grade);
+    return OutputResult.createNumberResult(e_num.toNumber(), g_num.toNumber(), grade);
   }
 
   checkBoolean (g_bool, e_bool) {
@@ -111,7 +111,7 @@ export class OutputMatching {
 
   checkStrings (g_output, e_ouput) {
     const assessmentList = []
-    let e_ouput_clean = e_ouput;
+    let e_output_clean = e_ouput;
     let g_output_clean = g_output;
     if (OutputMatching.NUM_IN_STRING_REGEX.test(e_ouput)) {
       const expected_numbers = e_ouput.match(OutputMatching.NUM_IN_STRING_REGEX);
@@ -120,14 +120,16 @@ export class OutputMatching {
         if(i >= expected_numbers.length) {
           return OutputResult.createNumberResult(null, val, 0);
         }
-        return this.checkNumbers(val, expected_numbers[i]);
+        const g_val = new Decimal(val)
+        const e_val = new Decimal(expected_numbers[i]);
+        return this.checkNumbers(g_val, e_val);
       }, this);
       if(expected_numbers.length > generated_numbers.length) {
         for(let i = generated_numbers.length; i < expected_numbers.length; ++i) {
           result.push(OutputResult.createNumberResult(expected_numbers[i], null, 0));
         }
       }
-      e_ouput_clean = e_ouput_clean.replace(OutputMatching.NUM_IN_STRING_REGEX, '').trim();
+      e_output_clean = e_output_clean.replace(OutputMatching.NUM_IN_STRING_REGEX, '').trim();
       g_output_clean = g_output_clean.replace(OutputMatching.NUM_IN_STRING_REGEX, '').trim();
       const numberGrade = result.reduce((prev, r) => prev + r.grade, 0) / result.length;
       assessmentList.push(numberGrade);
@@ -139,20 +141,22 @@ export class OutputMatching {
         if(i >= expected_bools.length) {
           return OutputResult.createBoolResult(null, val, 0);
         }
-        return this.checkBoolean(val, expected_bools[i]);
+        const g_bool = TypeParser.toBool(val);
+        const e_bool = TypeParser.toBool(expected_bools[i]);
+        return this.checkBoolean(g_bool, e_bool );
       }, this);
       if(expected_bools.length > generated_bools.length) {
         for(let i = generated_bools.length; i < expected_bools.length; ++i) {
           result.push(OutputResult.createBoolResult(expected_bools[i], null, 0));
         }
       }
-      e_ouput_clean = e_ouput_clean.replace(OutputMatching.NUM_IN_STRING_REGEX, '').trim();
-      g_output_clean = g_output_clean.replace(OutputMatching.NUM_IN_STRING_REGEX, '').trim();
+      e_output_clean = e_output_clean.replace(OutputMatching.BOOLEAN_IN_STRING_REGEX, '').trim();
+      g_output_clean = g_output_clean.replace(OutputMatching.BOOLEAN_IN_STRING_REGEX, '').trim();
       const boolGrade = result.reduce((prev, r) => prev + r.grade, 0) / result.length;
       assessmentList.push(boolGrade);
     }
-    const dist = levenshteinDistance(g_output_clean, e_ouput_clean);
-    const gradeDiff = Math.max(0, e_ouput_clean.length - dist);
+    const dist = levenshteinDistance(g_output_clean, e_output_clean);
+    const gradeDiff = Math.max(0, e_output_clean.length - dist)/e_output_clean.length;
     const assessment_size = assessmentList.length + 1;
     const gradeAcc = assessmentList.reduce((prev, val) => prev + val/assessment_size, 0);
     const finalGrade = 1 * (gradeDiff/assessment_size + gradeAcc);

+ 4 - 4
js/services/localizedStringsService.js

@@ -11,15 +11,15 @@ class IVProgLocalizedStrings extends line_i18n.LocalizedStrings {
   }
 
   translateType (type, dim) {
+    const type_string = this.getUI(`type_${type}`);
     switch (dim) {
       case 0:
-        return this.getUI(`type_${type}`);
+        return type_string;
       default:
-        const transType = this.getUI(`type_${type}`);
         if(dim === 1)
-          return this.getUI("matrix_info_string", [transType])
+          return this.getUI("matrix_info_string", [type_string])
         else
-          return this.getUI("vector_info_string", [transType])
+          return this.getUI("vector_info_string", [type_string])
     }
   }
   

+ 2 - 2
js/util/utils.js

@@ -246,8 +246,8 @@ export function levenshteinDistance (a, b) {
         matrix[i][j] = matrix[i-1][j-1];
       } else {
         matrix[i][j] = Math.min(matrix[i-1][j-1] + 1, // substitution
-                                Math.min(matrix[i][j-1] + 1, // insertion
-                                         matrix[i-1][j] + 1)); // deletion
+                        Math.min(matrix[i][j-1] + 1, // insertion
+                          matrix[i-1][j] + 1)); // deletion
       }
     }
   }