| import { Question } from '../types'; |
|
|
| export class ParsingService { |
| static parseText(text: string): Question[] { |
| const lines = text.split('\n').map(l => l.trim()).filter(l => l.length > 0); |
| const questions: Question[] = []; |
| let currentQuestion: Partial<Question> | null = null; |
|
|
| lines.forEach((line) => { |
| |
| const qMatch = line.match(/^(\d+)[\.\)]\s*(.*)$/i) || line.match(/^Question\s*(\d+)[:\.]?\s*(.*)$/i) || line.match(/^Q(\d+)[:\.]?\s*(.*)$/i); |
| |
| if (qMatch) { |
| if (currentQuestion && currentQuestion.text) { |
| questions.push(currentQuestion as Question); |
| } |
| currentQuestion = { |
| id: qMatch[1], |
| text: qMatch[2], |
| options: [], |
| correctOptionId: '', |
| category: 'General', |
| difficulty: 'medium' |
| }; |
| return; |
| } |
|
|
| |
| const oMatch = line.match(/^([A-D])[\.\)]\s*(.*)$/i) || line.match(/^\(([A-D])\)\s*(.*)$/i); |
| if (oMatch && currentQuestion) { |
| currentQuestion.options?.push({ |
| id: `opt-${oMatch[1].toLowerCase()}`, |
| text: oMatch[2] |
| }); |
| return; |
| } |
|
|
| |
| const aMatch = line.match(/^Answer:\s*([A-D])/i); |
| if (aMatch && currentQuestion) { |
| currentQuestion.correctOptionId = `opt-${aMatch[1].toLowerCase()}`; |
| return; |
| } |
|
|
| |
| if (currentQuestion && !oMatch && !aMatch) { |
| currentQuestion.text += ' ' + line; |
| } |
| }); |
|
|
| if (currentQuestion && currentQuestion.text) { |
| questions.push(currentQuestion as Question); |
| } |
|
|
| return questions; |
| } |
| } |
|
|