output_matching.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { Decimal } from 'decimal.js';
  2. import { InputAssessment } from "../../util/input_assessment";
  3. import { OutputTest } from "../../util/outputTest";
  4. import { Config } from "../../util/config";
  5. import { levenshteinDistance } from "../../util/utils";
  6. import * as TypeParser from "./../../typeSystem/parsers";
  7. import * as LocalizedStringsService from "../../services/localizedStringsService";
  8. import * as OutputResult from "./output_result";
  9. const LocalizedStrings = LocalizedStringsService.getInstance();
  10. export class OutputMatching {
  11. static get NUM_REGEX () {
  12. return /^[0-9]+(\.[0-9]+)?$/;
  13. }
  14. static get NUM_IN_STRING_REGEX () {
  15. return /[0-9]+(\.[0-9]+)?/g;
  16. }
  17. static get BOOLEAN_REGEX () {
  18. const str = `^(${LocalizedStrings.getUI("logic_value_true")}|${LocalizedStrings.getUI("logic_value_false")})$`;
  19. return new RegExp(str);
  20. }
  21. static get BOOLEAN_IN_STRING_REGEX () {
  22. const str = `(${LocalizedStrings.getUI("logic_value_true")}|${LocalizedStrings.getUI("logic_value_false")})`;
  23. return new RegExp(str, 'g');
  24. }
  25. constructor (program, input_list, expected_output, test_name) {
  26. this.program = program;
  27. this.name = test_name;
  28. this.input_list = input_list;
  29. this.expected_output = expected_output;
  30. }
  31. eval () {
  32. const refThis = this;
  33. const input = new InputAssessment(this.input_list);
  34. const gen_output = new OutputTest();
  35. this.program.registerInput(input);
  36. this.program.registerOutput(gen_output);
  37. const start_time = Date.now();
  38. return this.program.interpretAST().then( sto => {
  39. const final_time = Date.now() - start_time;
  40. if(input.isInputAvailable()) {
  41. return {status:"failed", grade:0, error_msg: refThis.getErrorMessage('test_case_few_reads', this.name+1),
  42. store:sto, time: final_time};
  43. }
  44. const result = gen_output.list.map((g_out, i) => {
  45. if(i >= this.expected_output.length) {
  46. return new OutputResult.OutputMatchResult(null, g_out, 0, this.getPotentialOutputType(g_out));
  47. }
  48. return this.outputMatch(g_out, this.expected_output[i]);
  49. }, this);
  50. if(this.expected_output.length > gen_output.list.length) {
  51. for(let i = gen_output.list.length; i < this.expected_output.length; ++i) {
  52. const e_out = this.expected_output[i];
  53. result.push(new OutputResult.OutputMatchResult(e_out, null, 0, this.getPotentialOutputType(e_out)));
  54. }
  55. }
  56. const grade = result.reduce((prev, val) => prev + val.grade, 0) / result.length;
  57. return {status: "ok", grade: grade, error_msg: "", store: sto, time: final_time, results: result};
  58. }).catch(error => {
  59. return {status:"failed", grade:0, error_msg: refThis.getErrorMessage('test_case_failed_exception', this.name + 1, error.message),
  60. store:null, time: null};
  61. });
  62. }
  63. getPotentialOutputType (output) {
  64. if(OutputMatching.NUM_REGEX.test(output)) {
  65. return "number";
  66. } else if (OutputMatching.BOOLEAN_REGEX.test(output)) {
  67. return "bool";
  68. } else {
  69. return "string";
  70. }
  71. }
  72. outputMatch (g_output, e_output) {
  73. if(OutputMatching.NUM_REGEX.test(e_output)) {
  74. const g_num = new Decimal(g_output);
  75. const e_num = new Decimal(e_output);
  76. return this.checkNumbers(g_num, e_num);
  77. } else if (OutputMatching.BOOLEAN_REGEX.test(e_output)) {
  78. const g_bool = TypeParser.toBool(g_output);
  79. const e_bool = TypeParser.toBool(e_output);
  80. return this.checkBoolean(g_bool, e_bool);
  81. } else {
  82. return this.checkStrings(g_output, e_output);
  83. }
  84. }
  85. checkNumbers (g_num, e_num) {
  86. const decimalPlaces = Math.min(e_num.dp(), Config.decimalPlaces);
  87. g_num = new Decimal(g_num.toFixed(decimalPlaces, Decimal.ROUND_FLOOR));
  88. e_num = new Decimal(e_num.toFixed(decimalPlaces, Decimal.ROUND_FLOOR));
  89. const result = g_num.eq(e_num);
  90. const grade = result ? 1 : 0;
  91. return OutputResult.createNumberResult(e_num.toNumber(), g_num.toNumber(), grade);
  92. }
  93. checkBoolean (g_bool, e_bool) {
  94. const grade = g_bool == e_bool ? 1 : 0;
  95. const g_bool_text = TypeParser.convertBoolToString(g_bool);
  96. const e_bool_text = TypeParser.convertBoolToString(e_bool);
  97. return OutputResult.createBoolResult(e_bool_text, g_bool_text, grade);
  98. }
  99. checkStrings (g_output, e_ouput) {
  100. const assessmentList = []
  101. let e_output_clean = e_ouput;
  102. let g_output_clean = g_output;
  103. if (OutputMatching.NUM_IN_STRING_REGEX.test(e_ouput)) {
  104. const expected_numbers = e_ouput.match(OutputMatching.NUM_IN_STRING_REGEX);
  105. const generated_numbers = g_output.match(OutputMatching.NUM_IN_STRING_REGEX);
  106. const result = generated_numbers.map((val, i) => {
  107. if(i >= expected_numbers.length) {
  108. return OutputResult.createNumberResult(null, val, 0);
  109. }
  110. const g_val = new Decimal(val)
  111. const e_val = new Decimal(expected_numbers[i]);
  112. return this.checkNumbers(g_val, e_val);
  113. }, this);
  114. if(expected_numbers.length > generated_numbers.length) {
  115. for(let i = generated_numbers.length; i < expected_numbers.length; ++i) {
  116. result.push(OutputResult.createNumberResult(expected_numbers[i], null, 0));
  117. }
  118. }
  119. e_output_clean = e_output_clean.replace(OutputMatching.NUM_IN_STRING_REGEX, '').trim();
  120. g_output_clean = g_output_clean.replace(OutputMatching.NUM_IN_STRING_REGEX, '').trim();
  121. const numberGrade = result.reduce((prev, r) => prev + r.grade, 0) / result.length;
  122. assessmentList.push(numberGrade);
  123. }
  124. if(OutputMatching.BOOLEAN_IN_STRING_REGEX.test(e_ouput)) {
  125. const expected_bools = e_ouput.match(OutputMatching.BOOLEAN_IN_STRING_REGEX);
  126. const generated_bools = g_output.match(OutputMatching.BOOLEAN_IN_STRING_REGEX);
  127. const result = generated_bools.map((val, i) => {
  128. if(i >= expected_bools.length) {
  129. return OutputResult.createBoolResult(null, val, 0);
  130. }
  131. const g_bool = TypeParser.toBool(val);
  132. const e_bool = TypeParser.toBool(expected_bools[i]);
  133. return this.checkBoolean(g_bool, e_bool );
  134. }, this);
  135. if(expected_bools.length > generated_bools.length) {
  136. for(let i = generated_bools.length; i < expected_bools.length; ++i) {
  137. result.push(OutputResult.createBoolResult(expected_bools[i], null, 0));
  138. }
  139. }
  140. e_output_clean = e_output_clean.replace(OutputMatching.BOOLEAN_IN_STRING_REGEX, '').trim();
  141. g_output_clean = g_output_clean.replace(OutputMatching.BOOLEAN_IN_STRING_REGEX, '').trim();
  142. const boolGrade = result.reduce((prev, r) => prev + r.grade, 0) / result.length;
  143. assessmentList.push(boolGrade);
  144. }
  145. const dist = levenshteinDistance(g_output_clean, e_output_clean);
  146. const gradeDiff = Math.max(0, e_output_clean.length - dist)/e_output_clean.length;
  147. const assessment_size = assessmentList.length + 1;
  148. const gradeAcc = assessmentList.reduce((prev, val) => prev + val/assessment_size, 0);
  149. const finalGrade = 1 * (gradeDiff/assessment_size + gradeAcc);
  150. return OutputResult.createStringResult(e_ouput, g_output, finalGrade);
  151. }
  152. getErrorMessage (errorID, ...args) {
  153. return LocalizedStrings.getError(errorID, args);
  154. }
  155. }