reading-presentation.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { ReadingQuestion } from './reading.models';
  2. import { isCompleteReadingQuestionSet } from './reading.utils';
  3. const LABELS = 'ABCD';
  4. // Match explicit answer references, never every standalone English A/B/C/D.
  5. const REFERENCES = /(?:正确选项|选项|正确答案|答案)(?:应为|是|为)?\s*[::]?\s*[A-D](?:\s*[、,,/和与及或]\s*[A-D])*(?![A-Za-z])|(?:故选|应选|选择|选)\s*[A-D](?![A-Za-z])|\b(?:[Oo]ptions?|[Cc]hoices?|[Aa]nswer(?:\s+is)?)\s*[::]?\s*[A-D](?:\s*(?:,|and|or)\s*[A-D])*(?![A-Za-z])|(?<![A-Za-z\u4e00-\u9fff])(?:[A-D]\s*[、,,/和与及或]\s*)+[A-D](?=\s*[\u4e00-\u9fff])|(?<![A-Za-z])[A-D](?=\s*项)|(?:^|[。;;,,\n])\s*[A-D](?=\s*(?:[::]|正确|错误|为|是|把|将|最|则|均|都|不|属于|描述|提到|符合|没有|\s+(?:is|are)\s+(?:correct|incorrect|right|wrong)))/g;
  6. const POSITIONAL = /\b(?:both|either|neither)\s+[A-D]\s+(?:and|or|nor)\s+[A-D]\b|\b(?:all|none)\s+of\s+(?:(?:the|these|those)\s+)?(?:above|below|options|answers)\b|\b(?:first|second|third|fourth|last|previous|next)\s+(?:option|answer|choice)\b|以上(?:各项|选项|皆|都)|上述(?:选项|各项)|第[一二三四1234]个?选项/i;
  7. export function remapReadingExplanation(text: string, labels: Readonly<Record<string, string>>): string {
  8. return text.replace(REFERENCES, reference => reference.replace(/\b[A-D]\b|(?<![A-Za-z])[A-D](?![A-Za-z])/g, label => labels[label] || label));
  9. }
  10. export function hasReadingOptionReferences(text: string): boolean {
  11. return remapReadingExplanation(text, { A: '<A>', B: '<B>', C: '<C>', D: '<D>' }) !== text;
  12. }
  13. export function hasPositionDependentReadingContent(text: string): boolean {
  14. return hasReadingOptionReferences(text) || POSITIONAL.test(text);
  15. }
  16. export function presentReadingQuestions(questions: ReadingQuestion[], seed: string): ReadingQuestion[] {
  17. if (!isCompleteReadingQuestionSet(questions)) throw new Error('题目不完整或选项无效,无法生成试卷');
  18. if (questions.some(q => hasPositionDependentReadingContent(q.title) || q.options.some(o => hasPositionDependentReadingContent(o.value)))) {
  19. throw new Error('这份题目含依赖选项位置的内容,请联系老师核对后再打印或练习');
  20. }
  21. if (questions.some(q => POSITIONAL.test(q.answer || '') || q.options.some(o => POSITIONAL.test(o.answer || '')))) {
  22. throw new Error('这份解析含无法可靠转换的选项位置描述,请联系老师核对后再打印或练习');
  23. }
  24. // Stable input order and seeded PRNG make refresh, direct PDF and the exercise agree.
  25. let state = 2166136261;
  26. for (const char of seed) state = Math.imul(state ^ char.charCodeAt(0), 16777619);
  27. const random = () => {
  28. state += 0x6D2B79F5;
  29. let value = Math.imul(state ^ state >>> 15, 1 | state);
  30. value ^= value + Math.imul(value ^ value >>> 7, 61 | value);
  31. return ((value ^ value >>> 14) >>> 0) / 4294967296;
  32. };
  33. const permute = <T>(items: T[]): T[] => {
  34. const result = [...items];
  35. for (let i = result.length - 1; i > 0; i--) {
  36. const j = Math.floor(random() * (i + 1));
  37. [result[i], result[j]] = [result[j], result[i]];
  38. }
  39. return result;
  40. };
  41. const ordered = permute([...questions].sort((a, b) => a.index - b.index || a.objectId.localeCompare(b.objectId)));
  42. const positions = permute([0, 1, 2, 3, Math.floor(random() * 4)]);
  43. return ordered.map((question, index) => {
  44. const correct = question.options.findIndex(option => option.check);
  45. const indexes = permute([0, 1, 2, 3].filter(i => i !== correct));
  46. indexes.splice(positions[index], 0, correct);
  47. const labels = Object.fromEntries(indexes.map((oldIndex, newIndex) => [LABELS[oldIndex], LABELS[newIndex]]));
  48. return {
  49. ...question,
  50. answer: question.answer === undefined ? undefined : remapReadingExplanation(question.answer, labels),
  51. options: indexes.map(i => ({
  52. ...question.options[i],
  53. answer: question.options[i].answer === undefined ? undefined : remapReadingExplanation(question.options[i].answer!, labels),
  54. })),
  55. };
  56. });
  57. }