output_matching.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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_true")})$`;
  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_true")})`;
  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 outerThis = this;
  33. const input = new InputAssessment(this.input_list);
  34. const gen_output = new OutputTest();
  35. program.registerInput(input);
  36. program.registerOutput(gen_output);
  37. const start_time = Date.now();
  38. return this.program.interpretAST().then( sto => {
  39. }).catch(error => {
  40. });
  41. }
  42. outputMatch (g_output, e_output) {
  43. if(OutputMatching.NUM_REGEX.test(e_output)) {
  44. const g_num = new Decimal(g_output);
  45. const e_num = new Decimal(e_output);
  46. this.checkNumbers(g_num, e_num);
  47. } else if (OutputMatching.BOOLEAN_REGEX.test(e_output)) {
  48. const g_bool = TypeParser.toBool(g_output);
  49. const e_bool = TypeParser.toBool(e_output);
  50. this.checkBoolean(g_bool, e_bool);
  51. } else {
  52. this.checkStrings(g_output, e_output);
  53. }
  54. }
  55. checkNumbers (g_num, e_num) {
  56. const decimalPlaces = Math.min(e_num.dp(), Config.decimalPlaces);
  57. g_num = new Decimal(g_num.toFixed(decimalPlaces, Decimal.ROUND_FLOOR));
  58. e_num = new Decimal(e_num.toFixed(decimalPlaces, Decimal.ROUND_FLOOR));
  59. const result = g_num.eq(e_num);
  60. const grade = result ? 1 : 0;
  61. return OutputResult.createNumberResult(e_num, g_num, grade);
  62. }
  63. checkBoolean (g_bool, e_bool) {
  64. const grade = g_bool == e_bool ? 1 : 0;
  65. const g_bool_text = TypeParser.convertBoolToString(g_bool);
  66. const e_bool_text = TypeParser.convertBoolToString(e_bool);
  67. return OutputResult.createBoolResult(e_bool_text, g_bool_text, grade);
  68. }
  69. checkStrings (g_output, e_ouput) {
  70. const assessmentList = []
  71. let e_ouput_clean = e_ouput;
  72. let g_output_clean = g_output;
  73. if (OutputMatching.NUM_IN_STRING_REGEX.test(e_ouput)) {
  74. const expected_numbers = e_ouput.match(OutputMatching.NUM_IN_STRING_REGEX);
  75. const generated_numbers = g_output.match(OutputMatching.NUM_IN_STRING_REGEX);
  76. const result = generated_numbers.map((val, i) => {
  77. if(i >= expected_numbers.length) {
  78. return OutputResult.createNumberResult(null, val, 0);
  79. }
  80. return this.checkNumbers(val, expected_numbers[i]);
  81. }, this);
  82. if(expected_numbers.length > generated_numbers.length) {
  83. for(let i = generated_numbers.length; i < expected_numbers.length; ++i) {
  84. result.push(OutputResult.createNumberResult(expected_numbers[i], null, 0));
  85. }
  86. }
  87. e_ouput_clean = e_ouput_clean.replace(OutputMatching.NUM_IN_STRING_REGEX, '').trim();
  88. g_output_clean = g_output_clean.replace(OutputMatching.NUM_IN_STRING_REGEX, '').trim();
  89. const numberGrade = result.reduce((prev, r) => prev + r.grade, 0) / result.length;
  90. assessmentList.push(numberGrade);
  91. }
  92. if(OutputMatching.BOOLEAN_IN_STRING_REGEX.test(e_ouput)) {
  93. const expected_bools = e_ouput.match(OutputMatching.BOOLEAN_IN_STRING_REGEX);
  94. const generated_bools = g_output.match(OutputMatching.BOOLEAN_IN_STRING_REGEX);
  95. const result = generated_bools.map((val, i) => {
  96. if(i >= expected_bools.length) {
  97. return OutputResult.createBoolResult(null, val, 0);
  98. }
  99. return this.checkBoolean(val, expected_bools[i]);
  100. }, this);
  101. if(expected_bools.length > generated_bools.length) {
  102. for(let i = generated_bools.length; i < expected_bools.length; ++i) {
  103. result.push(OutputResult.createBoolResult(expected_bools[i], null, 0));
  104. }
  105. }
  106. e_ouput_clean = e_ouput_clean.replace(OutputMatching.NUM_IN_STRING_REGEX, '').trim();
  107. g_output_clean = g_output_clean.replace(OutputMatching.NUM_IN_STRING_REGEX, '').trim();
  108. const boolGrade = result.reduce((prev, r) => prev + r.grade, 0) / result.length;
  109. assessmentList.push(boolGrade);
  110. }
  111. const dist = levenshteinDistance(g_output_clean, e_ouput_clean);
  112. const gradeDiff = Math.max(0, e_ouput_clean.length - dist);
  113. const assessment_size = assessmentList.length + 1;
  114. const gradeAcc = assessmentList.reduce((prev, val) => prev + val/assessment_size, 0);
  115. const finalGrade = 1 * (gradeDiff/assessment_size + gradeAcc);
  116. return OutputResult.createStringResult(e_ouput, g_output, finalGrade);
  117. }
  118. }