| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- import { ReadingQuestion } from './reading.models';
- import { isCompleteReadingQuestionSet } from './reading.utils';
- const LABELS = 'ABCD';
- // Match explicit answer references, never every standalone English A/B/C/D.
- 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;
- 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;
- export function remapReadingExplanation(text: string, labels: Readonly<Record<string, string>>): string {
- return text.replace(REFERENCES, reference => reference.replace(/\b[A-D]\b|(?<![A-Za-z])[A-D](?![A-Za-z])/g, label => labels[label] || label));
- }
- export function hasReadingOptionReferences(text: string): boolean {
- return remapReadingExplanation(text, { A: '<A>', B: '<B>', C: '<C>', D: '<D>' }) !== text;
- }
- export function hasPositionDependentReadingContent(text: string): boolean {
- return hasReadingOptionReferences(text) || POSITIONAL.test(text);
- }
- export function presentReadingQuestions(questions: ReadingQuestion[], seed: string): ReadingQuestion[] {
- if (!isCompleteReadingQuestionSet(questions)) throw new Error('题目不完整或选项无效,无法生成试卷');
- if (questions.some(q => hasPositionDependentReadingContent(q.title) || q.options.some(o => hasPositionDependentReadingContent(o.value)))) {
- throw new Error('这份题目含依赖选项位置的内容,请联系老师核对后再打印或练习');
- }
- if (questions.some(q => POSITIONAL.test(q.answer || '') || q.options.some(o => POSITIONAL.test(o.answer || '')))) {
- throw new Error('这份解析含无法可靠转换的选项位置描述,请联系老师核对后再打印或练习');
- }
- // Stable input order and seeded PRNG make refresh, direct PDF and the exercise agree.
- let state = 2166136261;
- for (const char of seed) state = Math.imul(state ^ char.charCodeAt(0), 16777619);
- const random = () => {
- state += 0x6D2B79F5;
- let value = Math.imul(state ^ state >>> 15, 1 | state);
- value ^= value + Math.imul(value ^ value >>> 7, 61 | value);
- return ((value ^ value >>> 14) >>> 0) / 4294967296;
- };
- const permute = <T>(items: T[]): T[] => {
- const result = [...items];
- for (let i = result.length - 1; i > 0; i--) {
- const j = Math.floor(random() * (i + 1));
- [result[i], result[j]] = [result[j], result[i]];
- }
- return result;
- };
- const ordered = permute([...questions].sort((a, b) => a.index - b.index || a.objectId.localeCompare(b.objectId)));
- const positions = permute([0, 1, 2, 3, Math.floor(random() * 4)]);
- return ordered.map((question, index) => {
- const correct = question.options.findIndex(option => option.check);
- const indexes = permute([0, 1, 2, 3].filter(i => i !== correct));
- indexes.splice(positions[index], 0, correct);
- const labels = Object.fromEntries(indexes.map((oldIndex, newIndex) => [LABELS[oldIndex], LABELS[newIndex]]));
- return {
- ...question,
- answer: question.answer === undefined ? undefined : remapReadingExplanation(question.answer, labels),
- options: indexes.map(i => ({
- ...question.options[i],
- answer: question.options[i].answer === undefined ? undefined : remapReadingExplanation(question.options[i].answer!, labels),
- })),
- };
- });
- }
|