output_matching.js 7.4 KB

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