Spaces:
Sleeping
Sleeping
Add files via upload
Browse filesFiles changed (27)
- .envAdded+1
- PROJECT_REVIEW.mdAdded+250
- README.md+50-2
- app.pyAdded+497
- download_nltk_data.pyAdded+29
- exam_question_system.pyAdded+376
- generated_questions.jsonAdded+22
- keyword_extractor.pyAdded+138
- local_question_generator.pyAdded+69
- nltk_install.pyAdded+69
- nltk_setup.pyAdded+56
- option_generator.pyAdded+272
- question_generator.pyAdded+392
- question_generator_old.pyAdded+600
- requirements.txtAdded+13
- sample_text.txtAdded+9
- setup_nltk.pyAdded+40
- simple_nltk_test.pyAdded+32
- simple_question_generator.pyAdded+76
- syllabus_processor.pyAdded+175
- test_imports.pyAdded+113
- test_local_generator.pyAdded+27
- test_nltk.pyAdded+43
- test_question_generator.pyAdded+28
- test_syllabus.pyAdded+37
- text_processor.pyAdded+118
- verify_generation.pyAdded+124
1OPENAI_API_KEY=sk-proj-B9o3Rs3XkPBop8HDI9ZvxZ7PbjSr4ODywiLXTUguZu8rWyd3ixLVAytkMRohroJpiXs7kHNp7lT3BlbkFJODj6Wj8nyOltH3g-ACmJwRRpqE6ToPvQa8iGddJHo8bFh0IQ4ykFDx4MFo19zEETDxHhQAZkUA123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250# Project Review - AutoExamGen
## OverviewThis is a comprehensive **Exam Question Generator** system built with Python and Flask. The system automatically generates exam questions (MCQ, Short Answer, Long Answer) from input text using NLP techniques.
## Project Structure
### Core Modules
1. **`app.py`** - Flask web application (main entry point) - Handles file uploads (PDF, DOCX, TXT) - Multi-step form flow (Input → Configuration → Results) - Session management - Question paper generation and download
2. **`exam_question_system.py`** - Main orchestration module - Coordinates all components - Handles question generation pipeline - Supports syllabus-based generation
3. **`question_generator.py`** - Question generation engine - Rule-based question generation (default) - Optional transformer-based generation (T5 model) - Multiple question generation strategies
4. **`keyword_extractor.py`** - Keyword and concept extraction - RAKE algorithm for keyword extraction - Named entity recognition - Important sentence identification
5. **`text_processor.py`** - Text preprocessing - Text cleaning and normalization - Sentence and word tokenization - Stopword removal and lemmatization
6. **`option_generator.py`** - MCQ option generation - Distractor generation using WordNet - Synonym-based options - Answer extraction from context
7. **`syllabus_processor.py`** - Syllabus-based question generation - Parses syllabus structure - Topic-based question generation - Unit and topic extraction
8. **`local_question_generator.py`** - Alternative transformer-based generator - Uses T5-base model for question generation
## Issues Found and Fixed
### ✅ Fixed Issues
1. **`app.py` - Line 27: Duplicate Variable Assignment** - **Issue**: `system_loading = False` was declared twice - **Fix**: Removed duplicate assignment
2. **`app.py` - Lines 382-529: Unreachable Code** - **Issue**: Dead code after return statement (lines 374, 380) - **Fix**: Removed all unreachable code block - **Impact**: Cleaned up ~150 lines of dead code
3. **`option_generator.py` - Lines 175-184: Unreachable Code** - **Issue**: Code after return statement on line 174 - **Fix**: Removed unreachable exception handling block
4. **`exam_question_system.py` - Line 172: Syntax Error** - **Issue**: Missing proper indentation in multi-line print statement - **Fix**: Fixed indentation for string continuation
## Code Quality Assessment
### Strengths ✅
1. **Well-Structured Architecture** - Clear separation of concerns - Modular design with single responsibility - Good use of classes and methods
2. **Error Handling** - Try-except blocks throughout - Graceful fallbacks (rule-based when transformers fail) - User-friendly error messages
3. **Documentation** - Docstrings for classes and methods - Type hints in some modules - README with usage instructions
4. **Feature Completeness** - Multiple question types (MCQ, Short, Long) - File upload support (PDF, DOCX, TXT) - Web interface with multi-step flow - Session management - Download functionality
5. **NLP Integration** - Multiple NLTK components - RAKE for keyword extraction - WordNet for synonyms/distractors - Optional transformer models
### Areas for Improvement 🔧
1. **Code Duplication** - Some repeated patterns in question formatting - Similar error handling in multiple places - **Recommendation**: Extract common functions
2. **Configuration Management** - Hardcoded values scattered throughout - Secret key in code (`app.secret_key`) - **Recommendation**: Use config file or environment variables
3. **Testing** - No visible test files for core functionality - **Recommendation**: Add unit tests for each module
4. **Type Hints** - Inconsistent use of type hints - **Recommendation**: Add type hints throughout
5. **Logging** - Mix of `print()` and `logging` - **Recommendation**: Standardize on logging module
6. **Error Messages** - Some generic error messages - **Recommendation**: More specific error handling
7. **Session Management** - Large content stored in session - **Recommendation**: Consider database for production
8. **Security** - Secret key should be in environment variable - File upload validation could be stricter - **Recommendation**: Add file type validation, size limits
## Dependencies Review
### Current Dependencies (`requirements.txt`)- ✅ Well-maintained packages- ✅ Appropriate versions- ✅ Good coverage of NLP needs
### Recommendations- Consider pinning exact versions for production- Add `python-dotenv` for environment variable management- Consider adding `gunicorn` or `waitress` for production deployment
## Functionality Review
### Working Features ✅1. Text preprocessing and cleaning2. Keyword extraction (RAKE)3. Question generation (rule-based)4. MCQ option generation5. Web interface with file upload6. Session management7. Question paper download
### Potential Issues ⚠️
1. **Transformer Models** - Optional transformer loading may fail silently - Large model downloads on first use - **Recommendation**: Add model download progress indicator
2. **File Processing** - PDF extraction may have issues with complex layouts - DOCX parsing is basic - **Recommendation**: Add better error handling for file parsing
3. **Question Quality** - Rule-based questions may be simplistic - **Recommendation**: Add question quality scoring
4. **Performance** - Synchronous processing may timeout on large files - **Recommendation**: Consider async processing or background jobs
## Recommendations for Production
1. **Environment Configuration** ```python # Use environment variables app.secret_key = os.environ.get('SECRET_KEY', 'dev-secret-key') ```
2. **Database Integration** - Store generated questions in database - User session management - Question history
3. **Caching** - Cache NLTK data downloads - Cache processed text - Cache generated questions
4. **API Rate Limiting** - Add rate limiting for API endpoints - Prevent abuse
5. **Monitoring** - Add logging to file - Error tracking (e.g., Sentry) - Performance monitoring
6. **Testing** - Unit tests for each module - Integration tests for web flow - Test file uploads
7. **Documentation** - API documentation - Deployment guide - Configuration guide
## Overall Assessment
**Grade: B+**
The project is well-structured and functional. The codebase shows good understanding of NLP concepts and Flask web development. The main issues were code cleanup (unreachable code) and minor syntax errors, which have been fixed.
### Key Strengths- Comprehensive feature set- Good architecture- Error handling- User-friendly interface
### Key Weaknesses- Some code duplication- Missing tests- Configuration management- Production readiness concerns
## Next Steps
1. ✅ **Completed**: Fixed code issues2. 🔄 **Recommended**: Add unit tests3. 🔄 **Recommended**: Improve configuration management4. 🔄 **Recommended**: Add logging standardization5. 🔄 **Recommended**: Security improvements6. 🔄 **Recommended**: Performance optimization
---
**Review Date**: February 5, 2026**Reviewed By**: AI Code Reviewer**Status**: Issues Fixed ✅12# AutoExamGenAutoExamGen is an AI-powered system that automatically generates exam questions from text using NLP1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950# Exam Question GeneratorAn intelligent Python-based system that automatically generates exam questions from input text using NLP techniques.## Features- **Text Preprocessing**: Cleans and preprocesses input text using NLTK- **Keyword Extraction**: Identifies important concepts using RAKE algorithm- **Question Generation**: Uses HuggingFace T5 model to generate questions- **Option Generation**: Creates multiple-choice options with distractors- **Multiple Interfaces**: CLI and Web interface (Flask) support- **JSON Output**: Structured output format for easy integration## Installation1. Clone or download this project2. Install dependencies:```bashpip install -r requirements.txt```3. Download required NLTK data:```pythonpython -c "import nltk; nltk.download('punkt'); nltk.download('stopwords'); nltk.download('wordnet')"```## Usage### Web Interface```bashpython app.py```Then open http://localhost:5000 in your browser.### Python API```pythonfrom question_generator import QuestionGeneratorgenerator = QuestionGenerator()questions = generator.generate_questions("Your input text here")```## Project Structure- `question_generator.py` - Core question generation logic- `text_processor.py` - Text cleaning and preprocessing- `keyword_extractor.py` - Keyword and important sentence extraction- `option_generator.py` - Multiple choice option generation- `app.py` - Flask web application- `
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497from flask import Flask, render_template, request, jsonify, send_file, session, redirect, url_for, flashimport osimport jsonimport tempfilefrom werkzeug.utils import secure_filenamefrom exam_question_system import ExamQuestionSystemfrom datetime import datetimeimport uuidimport threadingimport timefrom docx import Documentimport PyPDF2app = Flask(__name__)app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max file sizeapp.config['UPLOAD_FOLDER'] = 'uploads'app.config['TEMP_INPUT_FOLDER'] = os.path.join(tempfile.gettempdir(), 'eqg_inputs')app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0 # Disable caching for developmentapp.secret_key = 'your-secret-key-change-this-in-production'
# Create necessary directories if they don't existos.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)os.makedirs(app.config['TEMP_INPUT_FOLDER'], exist_ok=True)
# Global variables for question systemquestion_system = Nonesystem_loading = Falsesystem_load_error = None
def read_file_content(filepath): """Read content from a file based on its extension.""" ext = os.path.splitext(filepath)[1].lower() try: if ext == '.pdf': text = "" with open(filepath, 'rb') as f: reader = PyPDF2.PdfReader(f) for page in reader.pages: text += page.extract_text() + "\n" return text elif ext == '.docx': doc = Document(filepath) return "\n".join([paragraph.text for paragraph in doc.paragraphs]) else: # Default to text file with open(filepath, 'r', encoding='utf-8', errors='replace') as f: return f.read() except Exception as e: raise Exception(f"Error reading {ext} file: {str(e)}")
def get_question_system(): """Get or initialize the question generation system.""" global question_system, system_loading, system_load_error if question_system is None and not system_loading: if system_load_error: raise Exception(f"System failed to load: {system_load_error}") system_loading = True try: print("Initializing question generation system...") question_system = ExamQuestionSystem() print("Question generation system loaded successfully!") except Exception as e: system_load_error = str(e) system_loading = False raise e finally: system_loading = False if system_loading: raise Exception("System is still loading, please wait...") return question_system
# Utility: parse human-readable duration text into minutes when possibledef parse_duration_to_minutes(duration_text): try: if not duration_text: return None text = duration_text.strip().lower() # Normalize text = text.replace('hrs', 'h').replace('hr', 'h').replace('hours', 'h').replace('hour', 'h') text = text.replace('minutes', 'm').replace('minute', 'm').replace('mins', 'm').replace('min', 'm') # Patterns like '2h 30m' import re hours = 0 minutes = 0 # Match hours h_match = re.search(r"(\d+)\s*h", text) if h_match: hours = int(h_match.group(1)) # Match minutes m_match = re.search(r"(\d+)\s*m", text) if m_match: minutes = int(m_match.group(1)) if h_match or m_match: return hours * 60 + minutes # If only a number, treat as minutes just_num = re.fullmatch(r"\s*(\d+)\s*", duration_text) if just_num: return int(just_num.group(1)) # If something like '3 hour' without m, captured above; if unparseable, return None return None except Exception: return None
@app.route('/')def index(): """Welcome page before the step flow.""" # Clear any existing session data session.clear() return render_template('welcome.html', project_name='AutoExamGen')
@app.route('/step1', methods=['GET'])def step1_input(): """Step 1: Syllabus input page.""" return render_template('step1_input.html')
@app.route('/step2', methods=['GET', 'POST'])def step2_configuration(): """Step 2: Question configuration page.""" if request.method == 'GET': # If user tries to access /step2 directly, redirect to step1 return redirect(url_for('step1_input')) # Handle POST request (form submission from step1) content = None try: # Get text input (from textarea) text_input = request.form.get('text_input', '').strip() # Check if file was uploaded if 'file' in request.files: file = request.files['file'] if file and file.filename != '': try: # Save the uploaded file filename = secure_filename(file.filename) if not os.path.exists(app.config['UPLOAD_FOLDER']): os.makedirs(app.config['UPLOAD_FOLDER']) filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) file.save(filepath) # Store file path in session session['content_file'] = filepath session.pop('content_text', None) # Clear any text content if it exists # Read the file content for processing content = read_file_content(filepath) print(f"File uploaded successfully: {filename}, Content length: {len(content)}") except Exception as e: error_msg = f'Error processing file: {str(e)}' print(error_msg) flash(error_msg, 'error') return redirect(url_for('step1_input')) # If no file but text content is provided if not content and text_input: # For small text content, store directly in session if len(text_input) < 2000: session['content_text'] = text_input content = text_input else: # For large content, save to a temporary file temp_file = os.path.join(app.config['UPLOAD_FOLDER'], f'temp_{int(time.time())}.txt') with open(temp_file, 'w', encoding='utf-8') as f: f.write(text_input) session['content_file'] = temp_file content = text_input print(f"Text input processed, Content length: {len(content)}") # Validate that we have content if not content or not content.strip(): flash('Please provide either a file or paste content', 'error') return redirect(url_for('step1_input')) # Process the content for question generation try: # Initialize question system if not already done print("Initializing question system...") question_system = get_question_system() print("Question system initialized successfully") # Store word count for progress display word_count = len(content.split()) session['word_count'] = word_count print(f"Content processed: {word_count} words") # Render the configuration page return render_template('step2_config.html', word_count=word_count, has_syllabus=bool(session.get('syllabus_text', ''))) except Exception as e: error_msg = f'Error initializing question system: {str(e)}' print(error_msg) import traceback traceback.print_exc() flash(error_msg, 'error') return redirect(url_for('step1_input')) except Exception as e: error_msg = f'An error occurred: {str(e)}' print(error_msg) import traceback traceback.print_exc() flash(error_msg, 'error') return redirect(url_for('step1_input'))
@app.route('/generate', methods=['POST'])def step3_generate(): """Step 3: Generate and display question paper.""" try: # Get form data num_questions = int(request.form.get('num_questions', 5)) question_types = request.form.getlist('question_types') # Get content from session or uploaded file content = None # Check for uploaded file first if 'content_file' in session and os.path.exists(session['content_file']): try: content = read_file_content(session['content_file']) except Exception as e: flash(f'Error reading uploaded file: {str(e)}', 'error') return redirect(url_for('step1_input')) # Check for direct text content elif 'content_text' in session and session['content_text']: content = session['content_text'] # If no content found, redirect to step 1 if not content: flash('No content found. Please provide content first.', 'error') return redirect(url_for('step1_input')) # Initialize question system try: question_system = get_question_system() # Helper function to safely get integer values from form def get_int(form, key, default=0): try: return int(form.get(key, default)) except (ValueError, TypeError): return default # Store configuration in session with all required fields and safe defaults config = { 'exam_name': request.form.get('exam_name', 'Sample Exam'), 'subject_name': request.form.get('subject_name', 'Subject'), 'duration': get_int(request.form, 'duration', 60), 'short_questions': get_int(request.form, 'short_questions', 2), 'short_marks': get_int(request.form, 'short_marks', 2), 'long_questions': get_int(request.form, 'long_questions', 1), 'long_marks': get_int(request.form, 'long_marks', 5), 'long_attempt': get_int(request.form, 'long_attempt', 1), 'mcq_questions': get_int(request.form, 'mcq_questions', 2), 'timestamp': datetime.now().strftime('%Y-%m-%d %H:%M:%S') } session['exam_config'] = config # Get the syllabus/content text content = "" if 'content_text' in session: content = session['content_text'] elif 'content_file' in session and os.path.exists(session['content_file']): content = read_file_content(session['content_file']) if not content.strip(): raise ValueError("No content available for question generation.") # Generate questions based on content and configuration print("Generating questions from content...") # Get number of questions for each type num_mcq = config['mcq_questions'] num_short = config['short_questions'] num_long = config['long_questions'] # Generate questions using the question system # We request enough questions for all sections, but without auto-MCQ generation # so we can handle categorization manually total_questions_needed = num_mcq + num_short + num_long results = question_system.generate_exam_questions( input_text=content, max_questions=total_questions_needed, include_mcq=False, # We'll generate options manually for specific questions syllabus_text=content ) all_questions = results.get('questions', []) # Initialize categories generated_questions = { 'mcq_questions': [], 'short_questions': [], 'long_questions': [] } # Distribute questions # We prioritize Short and Long questions first to ensure they get content, # as MCQs are easier to fallback/generate # Filter out questions that are too simple for Long answers long_candidates = [q for q in all_questions if len(q.get('context', '').split()) > 10] short_candidates = [q for q in all_questions if q not in long_candidates] # If we don't have enough long candidates, take from short if len(long_candidates) < num_long: needed = num_long - len(long_candidates) long_candidates.extend(short_candidates[:needed]) short_candidates = short_candidates[needed:] # 3. Process Long Questions (Prioritize these) for _ in range(num_long): if long_candidates: q = long_candidates.pop(0) q['type'] = 'long_answer' generated_questions['long_questions'].append(q) # Remove from all_questions so we don't reuse if q in all_questions: all_questions.remove(q) # 2. Process Short Questions for _ in range(num_short): if short_candidates: q = short_candidates.pop(0) q['type'] = 'short_answer' generated_questions['short_questions'].append(q) if q in all_questions: all_questions.remove(q) elif all_questions: # Fallback to any remaining q = all_questions.pop(0) q['type'] = 'short_answer' generated_questions['short_questions'].append(q)
# 1. Process MCQs (Use remaining questions) # Extract global keywords for distractors global_keywords = [k[1] for k in results.get('keywords', [])] for _ in range(num_mcq): if all_questions: q = all_questions.pop(0) # Generate options for this question try: mcq_data = question_system.option_generator.create_mcq_options( q['question'], q['context'], correct_answer=q.get('correct_answer'), global_keywords=global_keywords ) if mcq_data and 'options' in mcq_data: q.update(mcq_data) q['type'] = 'mcq' generated_questions['mcq_questions'].append(q) else: # Fallback if option generation fails q['type'] = 'short_answer' generated_questions['short_questions'].append(q) except Exception as e: print(f"Error generating options: {e}") q['type'] = 'short_answer' generated_questions['short_questions'].append(q) # Store the generated questions session['generated_questions'] = generated_questions # Calculate and store total marks total_marks = ( (len(session['generated_questions']['short_questions']) * config['short_marks']) + (len(session['generated_questions']['long_questions']) * config['long_marks']) + len(session['generated_questions']['mcq_questions']) # 1 mark per MCQ ) session['total_marks'] = total_marks # Redirect to results page return redirect(url_for('show_results')) except Exception as e: error_msg = f'Error generating questions: {str(e)}' print(error_msg) flash(error_msg, 'error') return redirect(url_for('step1_input')) except Exception as e: error_msg = f'An error occurred: {str(e)}' print(error_msg) flash(error_msg, 'error') return redirect(url_for('step1_input'))
@app.route('/download')def download_paper(): """Download the generated question paper as HTML.""" try: if 'question_paper' not in session: return redirect(url_for('index')) # Generate a unique filename filename = f"question_paper_{uuid.uuid4().hex[:8]}.html" # Render the printable version # Determine display duration similarly to step3 cfg = session['config'] display_duration = cfg.get('exam_duration') if cfg.get('exam_duration') else int(round(session['total_marks'] * 1.5))
html_content = render_template('printable_paper.html', question_paper=session['question_paper'], config=cfg, total_marks=session['total_marks'], exam_date=session['exam_date'], display_duration=display_duration) # Create a temporary file temp_file = os.path.join(tempfile.gettempdir(), filename) with open(temp_file, 'w', encoding='utf-8') as f: f.write(html_content) return send_file(temp_file, as_attachment=True, download_name=filename) except Exception as e: return jsonify({'error': f'Error downloading paper: {str(e)}'}), 500
@app.route('/results')def show_results(): """Display the generated questions.""" if 'generated_questions' not in session or 'exam_config' not in session: flash('No questions generated yet. Please start from the beginning.', 'error') return redirect(url_for('step1_input')) # Get config with defaults config = session.get('exam_config', {}) questions = session.get('generated_questions', {}) # Ensure all required question types exist in the questions dictionary for qtype in ['mcq_questions', 'short_questions', 'long_questions']: if qtype not in questions: questions[qtype] = [] # Calculate total marks total_marks = 0 if 'mcq_questions' in questions: total_marks += len(questions['mcq_questions']) if 'short_questions' in questions: total_marks += len(questions['short_questions']) * 2 # 2 marks per short question if 'long_questions' in questions: total_marks += len(questions['long_questions']) * 5 # 5 marks per long question # Get exam date from config or use current date exam_date = config.get('timestamp', datetime.now().strftime('%Y-%m-%d %H:%M:%S')) return render_template('step3_result.html', question_paper=questions, # Changed from questions to question_paper config=config, total_marks=total_marks, exam_date=datetime.strptime(exam_date, '%Y-%m-%d %H:%M:%S').strftime('%B %d, %Y'), display_duration=config.get('duration', 60)) # Use configured duration or default to 60 minutes
@app.route('/api/health')def health_check(): """Health check endpoint.""" return jsonify({'status': 'healthy', 'service': 'Exam Question Generator'})
@app.route('/api/warmup')def warmup(): """Warmup endpoint to initialize the system.""" try: system = get_question_system() return jsonify({ 'status': 'ready', 'message': 'Question generation system is ready', 'uses_transformers': system.question_generator.use_transformers }) except Exception as e: return jsonify({ 'status': 'loading' if 'still loading' in str(e) else 'error', 'message': str(e) }), 202 if 'still loading' in str(e) else 500
if __name__ == '__main__': print("🌐 Starting Flask Web Application...") print("📝 Exam Question Generator Web Interface") print("🔗 Access the application at: http://localhost:5000") print("💡 Using rule-based question generation for faster startup") print("⚡ System will initialize on first use")
# Configure Flask for better timeout handling app.config['PERMANENT_SESSION_LIFETIME'] = 1800 # 30 minutes
app.run(debug=True, host='0.0.0.0', port=5000, threaded=True)1234567891011121314151617181920212223242526272829import nltk
def download_nltk_data():
"""Download all required NLTK data packages."""
print("Downloading NLTK data packages...")
# List of NLTK data packages to download
packages = [
'punkt',
'averaged_perceptron_tagger',
'wordnet',
'stopwords',
'universal_tagset',
'tagsets',
'omw-1.4', # Open Multilingual WordNet
]
for package in packages:
try:
print(f"Downloading {package}...")
nltk.download(package, quiet=False)
print(f"Successfully downloaded {package}")
except Exception as e:
print(f"Error downloading {package}: {str(e)}")
print("\nNLTK data download complete!")
if __name__ == "__main__":
download_nltk_data()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376import jsonimport osfrom datetime import datetimefrom text_processor import TextProcessorfrom keyword_extractor import KeywordExtractorfrom question_generator import QuestionGeneratorfrom option_generator import OptionGeneratorfrom syllabus_processor import SyllabusProcessor
class ExamQuestionSystem: def __init__(self, use_transformers=True): """Initialize the complete exam question generation system. Args: use_transformers: Whether to use transformer models for question generation """ print("Initializing Exam Question Generation System...") self.text_processor = TextProcessor() self.keyword_extractor = KeywordExtractor() # Use rule-based generation by default for faster web deployment self.question_generator = QuestionGenerator(use_transformers=use_transformers) self.option_generator = OptionGenerator() self.syllabus_processor = SyllabusProcessor() print("System initialized successfully!") def process_text_file(self, file_path): """ Process a text file and return its content. Args: file_path (str): Path to the text file Returns: str: File content """ try: with open(file_path, 'r', encoding='utf-8') as file: return file.read() except Exception as e: raise Exception(f"Error reading file {file_path}: {e}") def generate_exam_questions(self, input_text, max_questions=5, include_mcq=True, syllabus_text=None): """ Complete pipeline to generate exam questions from input text. Args: input_text (str): Input text to generate questions from max_questions (int): Maximum number of questions to generate include_mcq (bool): Whether to include multiple choice options syllabus_text (str, optional): Syllabus text for topic-based question generation Returns: dict: Generated questions and metadata """ print("Starting question generation pipeline...") try: if not input_text or not input_text.strip(): raise ValueError("Input text cannot be empty") # If syllabus text is provided, try syllabus-based generation if syllabus_text and syllabus_text.strip(): syllabus_results = self._generate_syllabus_based_questions(input_text, syllabus_text, max_questions, include_mcq) if syllabus_results and syllabus_results.get('questions'): return syllabus_results print("Warning: Syllabus-based generation produced no questions. Falling back to standard generation.") # Otherwise use the standard generation approach if not input_text or not input_text.strip(): raise ValueError("Input text is empty or contains only whitespace") print(f"Input text length: {len(input_text)} characters") # Step 1: Text preprocessing print("1. Processing and cleaning text...") processed_data = self.text_processor.preprocess_text(input_text) if not processed_data or 'sentences' not in processed_data or not processed_data['sentences']: raise ValueError("Failed to process input text into sentences") print(f"Extracted {len(processed_data['sentences'])} sentences from input") # Step 2: Extract keywords and important sentences print("2. Extracting keywords and important sentences...") key_concepts = self.keyword_extractor.extract_key_concepts( processed_data['cleaned_text'], processed_data['sentences'], top_n_sentences=max(10, max_questions) ) if not key_concepts or 'important_sentences' not in key_concepts or not key_concepts['important_sentences']: # If no important sentences found, use the first few sentences print("Warning: No important sentences found, using first few sentences") key_concepts['important_sentences'] = processed_data['sentences'][:max_questions] print(f"Found {len(key_concepts.get('important_sentences', []))} important sentences") # Prepare sentences and keywords for generation generation_inputs = [] if key_concepts and 'important_sentences' in key_concepts: for item in key_concepts['important_sentences']: if isinstance(item, tuple) and len(item) >= 2: # item is (score, sentence, keyword) or (score, sentence) sentence = item[1] keyword = item[2] if len(item) > 2 else None generation_inputs.append({'context': sentence, 'answer': keyword}) elif isinstance(item, str): generation_inputs.append({'context': item, 'answer': None}) # Step 3: Generate questions print("3. Generating questions...") questions = [] # Generate more questions than requested to ensure we have enough valid ones # and to cover all sections (MCQ, Short, Long) generation_target = max(max_questions * 2, 10) try: questions = self.question_generator.generate_multiple_questions( generation_inputs, generation_target ) # Ensure we have a list of questions if not questions: raise ValueError("No questions were generated") # Convert string questions to proper format formatted_questions = [] for i, q in enumerate(questions): if isinstance(q, str): formatted_q = { 'question': q, 'context': 'Generated from input text', 'score': 1.0, 'type': 'short_answer', 'id': f'q_{i+1}' } formatted_questions.append(formatted_q) elif isinstance(q, dict): # Ensure required fields exist q['question'] = q.get('question', f'Question {i+1}') q['context'] = q.get('context', 'No context provided') q['score'] = q.get('score', 1.0) q['type'] = q.get('type', 'short_answer') q['id'] = q.get('id', f'q_{i+1}') formatted_questions.append(q) questions = formatted_questions # Step 4: Generate MCQ options if requested and we have enough questions if include_mcq and questions: print("4. Generating multiple choice options...") # Extract global keywords for distractors global_keywords = [k[1] for k in key_concepts.get('keywords', [])] for question_data in questions[:max_questions]: # Limit to max_questions try: mcq_data = self.option_generator.create_mcq_options( question_data['question'], question_data['context'], correct_answer=question_data.get('correct_answer'), global_keywords=global_keywords ) if mcq_data and 'options' in mcq_data and len(mcq_data['options']) >= 2: question_data.update(mcq_data) question_data['type'] = 'mcq' print(f"Generated {len(mcq_data['options'])} options for question") else: print("Not enough options generated, keeping as short answer") except Exception as e: print(f"Error generating MCQ options: {str(e)}" " (continuing with short answer)") except Exception as e: import traceback print(f"Error in question generation: {str(e)}\n{traceback.format_exc()}") # Create fallback questions questions = [{ 'question': f"Sample question {i+1} (error: {str(e)[:50]}...)", 'context': 'Error occurred during question generation', 'score': 0.0, 'type': 'error', 'id': f'error_{i}' } for i in range(min(3, max_questions))] # Compile results results = { 'metadata': { 'input_word_count': processed_data.get('word_count', 0), 'input_sentence_count': len(processed_data.get('sentences', [])), 'questions_generated': len(questions), 'keywords_extracted': len(key_concepts.get('keywords', [])), 'named_entities': len(key_concepts.get('named_entities', [])) }, 'keywords': key_concepts.get('keywords', [])[:10], 'named_entities': key_concepts.get('named_entities', [])[:10], 'questions': questions[:max_questions] # Ensure we don't return more than requested } print(f"Successfully generated {len(results['questions'])} questions") return results except Exception as e: import traceback error_msg = f"Error in generate_exam_questions: {str(e)}\n{traceback.format_exc()}" print(error_msg) # Return a minimal response with error information return { 'metadata': { 'error': str(e), 'input_length': len(input_text) if input_text else 0, 'questions_generated': 0 }, 'keywords': [], 'named_entities': [], 'questions': [{ 'question': f"Error generating questions: {str(e)[:100]}", 'context': 'An error occurred during question generation', 'score': 0.0, 'type': 'error', 'id': 'error_0' }] } print(f"✅ Generated {len(questions)} questions successfully!") return results def save_questions_to_json(self, questions_data, output_file): """ Save generated questions to a JSON file. Args: questions_data (dict): Generated questions data output_file (str): Output file path """ try: with open(output_file, 'w', encoding='utf-8') as file: json.dump(questions_data, file, indent=2, ensure_ascii=False) print(f"✅ Questions saved to {output_file}") except Exception as e: print(f"❌ Error saving to file: {e}") def display_questions_console(self, questions_data): """ Display generated questions in a formatted console output. Args: questions_data (dict): Generated questions data """ print("\n" + "="*80) print("GENERATED EXAM QUESTIONS") print("="*80) # Display metadata metadata = questions_data['metadata'] print(f"\n📊 STATISTICS:") print(f" • Input text: {metadata['input_word_count']} words, {metadata['input_sentence_count']} sentences") print(f" • Keywords extracted: {metadata['keywords_extracted']}") print(f" • Named entities found: {metadata['named_entities']}") print(f" • Questions generated: {metadata['questions_generated']}") # Display top keywords print(f"\n🔑 TOP KEYWORDS:") for score, keyword in questions_data['keywords'][:5]: print(f" • {keyword} (score: {score:.2f})") # Display questions print(f"\n❓ QUESTIONS:") for i, q in enumerate(questions_data['questions'], 1): print(f"\n{i}. {q['question']}") if 'options' in q: print(" Options:") for j, option in enumerate(q['options'], 1): marker = "✓" if j-1 == q['correct_index'] else " " print(f" {marker} {chr(64+j)}. {option}") print(f" Context: {q['context'][:100]}...") print(f" Confidence: {q['score']:.2f}") print("\n" + "="*80)
def _generate_syllabus_based_questions(self, content_text, syllabus_text, max_questions=10, include_mcq=True): """ Generate questions based on syllabus topics. Args: content_text (str): The content text to generate questions from syllabus_text (str): The syllabus text with units and topics max_questions (int): Maximum number of questions to generate include_mcq (bool): Whether to include multiple choice options Returns: dict: Generated questions and metadata """ print("Generating syllabus-based questions...") try: # Generate questions by topic questions_by_topic = self.syllabus_processor.generate_topic_based_questions( syllabus_text=syllabus_text, content_text=content_text, questions_per_topic=3 # Will be adjusted based on max_questions ) # Flatten questions from all topics all_questions = [] for topic, questions in questions_by_topic.items(): for q in questions: q['topic'] = topic all_questions.append(q) # Limit to max_questions all_questions = all_questions[:max_questions] # Generate options for MCQs if needed if include_mcq: for question in all_questions: if 'options' not in question and 'context' in question: try: mcq_data = self.option_generator.create_mcq_options( question['question'], question['context'], num_options=4 ) if mcq_data and 'options' in mcq_data and len(mcq_data['options']) >= 2: question.update(mcq_data) except Exception as e: print(f"Error generating options: {e}") # Prepare results results = { 'metadata': { 'total_questions': len(all_questions), 'topics_covered': list(questions_by_topic.keys()), 'generated_at': str(datetime.now()) }, 'questions': all_questions } return results except Exception as e: print(f"Error in syllabus-based question generation: {e}") raise
# Example usage and testingif __name__ == "__main__": # Sample text for testing sample_text = """ Artificial Intelligence (AI) is a branch of computer science that aims to create intelligent machines that work and react like humans. Machine learning is a subset of AI that provides systems the ability to automatically learn and improve from experience without being explicitly programmed. Deep learning is a subset of machine learning that uses neural networks with three or more layers. These neural networks attempt to simulate the behavior of the human brain to learn from large amounts of data. Python is one of the most popular programming languages for AI development due to its simplicity and extensive libraries like TensorFlow and PyTorch. """ try: # Initialize system system = ExamQuestionSystem() # Generate questions results = system.generate_exam_questions(sample_text, max_questions=3) # Display results system.display_questions_console(results) # Save to JSON system.save_questions_to_json(results, "sample_questions.json") except Exception as e: print(f"❌ Error: {e}")12345678910111213141516171819202122{
"metadata": {
"input_word_count": 1,
"input_sentence_count": 0,
"questions_generated": 0,
"keywords_extracted": 1,
"named_entities": 1
},
"keywords": [
[
1.0,
"text"
]
],
"named_entities": [
[
"Your",
1
]
],
"questions": []
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138import nltkfrom rake_nltk import Rakefrom collections import Counterimport re
class KeywordExtractor: def __init__(self): """Initialize the keyword extractor.""" self.rake = Rake() def extract_keywords_rake(self, text, max_keywords=10): """ Extract keywords using RAKE algorithm. Args: text (str): Input text max_keywords (int): Maximum number of keywords to extract Returns: list: List of keywords with scores """ self.rake.extract_keywords_from_text(text) keywords_with_scores = self.rake.get_ranked_phrases_with_scores() # Filter keywords clean_keywords = [] seen_keywords = set() for score, keyword in keywords_with_scores: # Remove keywords with digits or special chars if re.search(r'\d', keyword) or len(keyword) < 4: continue # Remove very short single words that are lowercase (likely noise) if ' ' not in keyword and keyword[0].islower() and len(keyword) < 5: continue # Remove duplicates if keyword.lower() in seen_keywords: continue clean_keywords.append((score, keyword)) seen_keywords.add(keyword.lower()) if len(clean_keywords) >= max_keywords: break return clean_keywords def extract_named_entities(self, text): """ Extract named entities (simple approach using capitalization patterns). Args: text (str): Input text Returns: list: List of potential named entities """ # Simple named entity extraction based on capitalization words = text.split() entities = [] for word in words: # Look for capitalized words that aren't at sentence start if word[0].isupper() and len(word) > 2: # Remove punctuation clean_word = re.sub(r'[^\w]', '', word) if clean_word and clean_word not in ['The', 'This', 'That', 'These', 'Those']: entities.append(clean_word) # Count occurrences and return most frequent entity_counts = Counter(entities) return entity_counts.most_common(10) def identify_important_sentences(self, sentences, keywords, top_n=5): """ Identify important sentences based on keyword density. Args: sentences (list): List of sentences keywords (list): List of important keywords top_n (int): Number of top sentences to return Returns: list: List of important sentences with scores """ keyword_phrases = [kw[1] for kw in keywords] # Extract phrases from (score, phrase) tuples sentence_scores = [] for sentence in sentences: score = 0 sentence_lower = sentence.lower() best_keyword = "" # Score based on keyword presence for keyword in keyword_phrases: if keyword.lower() in sentence_lower: score += 1 if not best_keyword or len(keyword) > len(best_keyword): best_keyword = keyword # Bonus for sentence length (not too short, not too long) word_count = len(sentence.split()) if 8 <= word_count <= 25: score += 0.5 # Bonus for sentences with numbers or specific terms if re.search(r'\d+', sentence): score += 0.3 if score > 0: sentence_scores.append((score, sentence, best_keyword)) # Sort by score and return top sentences sentence_scores.sort(key=lambda x: x[0], reverse=True) return sentence_scores[:top_n] def extract_key_concepts(self, text, sentences, top_n_sentences=5): """ Complete keyword and concept extraction pipeline. Args: text (str): Input text sentences (list): List of sentences Returns: dict: Extracted keywords, entities, and important sentences """ keywords = self.extract_keywords_rake(text) entities = self.extract_named_entities(text) important_sentences = self.identify_important_sentences(sentences, keywords, top_n=top_n_sentences) return { 'keywords': keywords, 'named_entities': entities, 'important_sentences': important_sentences }123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLMimport torch
class LocalQuestionGenerator: def __init__(self): print("Loading local question generation model...") self.device = 0 if torch.cuda.is_available() else -1 # Use GPU if available # Load the model and tokenizer model_name = "valhalla/t5-base-qa-qg-hl" self.tokenizer = AutoTokenizer.from_pretrained(model_name) self.model = AutoModelForSeq2SeqLM.from_pretrained(model_name) # Initialize the pipeline self.generator = pipeline( "text2text-generation", model=self.model, tokenizer=self.tokenizer, device=self.device ) print("Model loaded successfully!") def generate_questions(self, text, num_questions=5, max_length=64): """Generate questions from the given text.""" if not text.strip(): return [] try: # Prepare the input text input_text = f"generate questions: {text}" # Generate questions results = self.generator( input_text, max_length=max_length, num_return_sequences=num_questions, num_beams=5, early_stopping=True ) # Extract and clean the generated questions questions = [result['generated_text'].strip() for result in results] return questions except Exception as e: print(f"Error generating questions: {str(e)}") return []
# Example usageif __name__ == "__main__": # Initialize the generator qg = LocalQuestionGenerator() # Sample text sample_text = """ Machine learning is a branch of artificial intelligence that focuses on building systems that learn from data. These systems can improve their performance over time without being explicitly programmed. There are three main types of machine learning: supervised learning, unsupervised learning, and reinforcement learning. """ # Generate questions print("\nGenerating questions...") questions = qg.generate_questions(sample_text, num_questions=3) # Print the results print("\nGenerated Questions:") for i, q in enumerate(questions, 1): print(f"{i}. {q}")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869import osimport nltkimport sys
def setup_nltk(): # Set NLTK data directory nltk_dir = os.path.join(os.path.expanduser('~'), 'nltk_data') os.makedirs(nltk_dir, exist_ok=True) nltk.data.path.append(nltk_dir) print(f"NLTK data directory: {nltk_dir}") # List of required NLTK packages packages = [ 'punkt', 'stopwords', 'averaged_perceptron_tagger', 'wordnet', 'omw-1.4' ] # Download each package for package in packages: print(f"\nDownloading {package}...") try: nltk.download(package, download_dir=nltk_dir, quiet=False) print(f"✓ {package} downloaded successfully") except Exception as e: print(f"✗ Error downloading {package}: {str(e)}") # Verify installation print("\n=== Verifying NLTK Installation ===") try: from nltk.tokenize import word_tokenize, sent_tokenize from nltk import pos_tag from nltk.corpus import stopwords test_text = "NLTK is working correctly if you can read this." # Test tokenization print("Testing tokenization...") words = word_tokenize(test_text) print(f"Word tokens: {words}") # Test sentence tokenization sentences = sent_tokenize(test_text) print(f"Sentences: {sentences}") # Test POS tagging print("\nTesting POS tagging...") tags = pos_tag(words) print(f"POS tags: {tags}") # Test stopwords print("\nTesting stopwords...") stop_words = stopwords.words('english') print(f"Sample stopwords: {stop_words[:5]}...") print("\n✅ NLTK is working correctly!") except Exception as e: print(f"\n❌ Error verifying NLTK: {str(e)}") print("\nPlease try running these commands manually:") print("import nltk") for package in packages: print(f"nltk.download('{package}')")
if __name__ == "__main__": setup_nltk()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import nltkimport os
def setup_nltk(): try: # Set NLTK data path to a local directory nltk_data = os.path.join(os.getcwd(), 'nltk_data') os.makedirs(nltk_data, exist_ok=True) nltk.data.path.append(nltk_data) # List of NLTK packages to download packages = [ ('punkt', 'tokenizers/punkt'), ('stopwords', 'corpora/stopwords'), ('averaged_perceptron_tagger', 'taggers/averaged_perceptron_tagger'), ('averaged_perceptron_tagger_eng', 'taggers/averaged_perceptron_tagger_eng'), ('wordnet', 'corpora/wordnet'), ('omw-1.4', 'corpora/omw-1.4') ] print("\n=== Setting up NLTK data ===") # Download each package for package, path in packages: try: nltk.data.find(path) print(f"✓ {package} is already available") except LookupError: print(f"Downloading {package}...") nltk.download(package, download_dir=nltk_data) print(f"✓ Downloaded {package}") # Test NLTK components print("\n=== Testing NLTK Components ===") from nltk.tokenize import sent_tokenize, word_tokenize from nltk.tag import pos_tag from nltk.corpus import stopwords sent_tokenize("This is a test.") word_tokenize("This is a test.") pos_tag(["test", "this", "is", "a", "sentence"]) stopwords.words('english') print("\n=== NLTK Setup Completed Successfully ===\n") return True except Exception as e: print(f"\n⚠ Error during NLTK setup: {str(e)}") print("\nPlease try running these commands manually in a Python shell:") print("import nltk") for package, _ in packages: print(f"nltk.download('{package}')") return False
if __name__ == "__main__": setup_nltk()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272import randomimport nltkfrom nltk.corpus import wordnetfrom nltk.tokenize import word_tokenizefrom nltk.tag import pos_tag
class OptionGenerator: def __init__(self): """Initialize the option generator with NLTK resources.""" try: # Download required NLTK data with explicit resource names nltk.download('punkt', quiet=True) nltk.download('averaged_perceptron_tagger', quiet=True) nltk.download('wordnet', quiet=True) nltk.download('stopwords', quiet=True) nltk.download('universal_tagset', quiet=True) nltk.download('tagsets', quiet=True) self.stop_words = set(nltk.corpus.stopwords.words('english')) self.word_net_lemmatizer = nltk.WordNetLemmatizer() # POS tag mapping for WordNet self.pos_mapping = { 'NN': 'n', 'NNS': 'n', 'NNP': 'n', 'NNPS': 'n', 'VB': 'v', 'VBD': 'v', 'VBG': 'v', 'VBN': 'v', 'VBP': 'v', 'VBZ': 'v', 'JJ': 'a', 'JJR': 'a', 'JJS': 'a', 'RB': 'r', 'RBR': 'r', 'RBS': 'r' } except Exception as e: print(f"Error initializing OptionGenerator: {str(e)}") raise def _get_synonyms(self, word, pos=None): """Get synonyms for a word using WordNet.""" synonyms = set() # Skip if word is too short or a stop word if len(word) < 3 or word.lower() in self.stop_words: return [] try: wordnet_pos = self.pos_mapping.get(pos, None) if pos else None # Try with the provided POS tag first if wordnet_pos: for syn in wordnet.synsets(word, pos=wordnet_pos): for lemma in syn.lemmas(): synonym = lemma.name().replace('_', ' ').lower() if synonym != word and len(synonym.split()) == 1: synonyms.add(synonym) # If no synonyms found, try without POS tag if not synonyms: for syn in wordnet.synsets(word): for lemma in syn.lemmas(): synonym = lemma.name().replace('_', ' ').lower() if synonym != word and len(synonym.split()) == 1: synonyms.add(synonym) # If still no synonyms, try with lemmatization if not synonyms and pos and pos.startswith('VB'): lemma = self.word_net_lemmatizer.lemmatize(word, pos='v') if lemma != word: for syn in wordnet.synsets(lemma, pos='v'): for l in syn.lemmas(): synonym = l.name().replace('_', ' ').lower() if synonym != word and len(synonym.split()) == 1: synonyms.add(synonym) except Exception as e: print(f"Error getting synonyms for '{word}': {str(e)}") return list(synonyms)[:10] # Return at most 10 synonyms def _get_distractors(self, word, pos=None, num=3): """Generate distractors for a given word.""" distractors = set() try: # Get synonyms first synonyms = self._get_synonyms(word, pos) distractors.update(synonyms[:num]) # If not enough synonyms, add similar words if len(distractors) < num: wordnet_pos = self.pos_mapping.get(pos, None) if pos else None similar_words = [] for syn in wordnet.synsets(word, pos=wordnet_pos): for lemma in syn.lemmas(): if lemma.name() != word: similar_words.append(lemma.name().replace('_', ' ').lower()) # Add similar words that aren't already in distractors for w in similar_words: if w not in distractors and w != word: distractors.add(w) if len(distractors) >= num: break except Exception as e: print(f"Error generating distractors for '{word}': {str(e)}") return list(distractors)[:num] def extract_answer_from_context(self, question, context): """ Extract the most likely answer from the context based on the question. This version uses simple string matching instead of POS tagging. Args: question (str): Generated question context (str): Source sentence/context Returns: str: Extracted answer """ try: q_lower = question.lower() context_lower = context.lower() # Common patterns for answers patterns = [ ('what is', 'is'), ('what are', 'are'), ('what was', 'was'), ('what were', 'were'), ('who is', 'is'), ('who are', 'are'), ('who was', 'was'), ('who were', 'were'), ('where is', 'is'), ('where are', 'are'), ('when is', 'is'), ('when was', 'was') ] # Try to find a direct answer using common patterns for q_pattern, verb in patterns: if q_lower.startswith(q_pattern): # Look for the pattern "[verb] [answer]" in the context verb_pos = context_lower.find(verb) if verb_pos != -1: # Get the text after the verb answer_part = context[verb_pos + len(verb):].strip(' ,.?!') # Return the first word or phrase return answer_part.split(',')[0].split('.')[0].strip() # Fallback: return the first proper noun or capitalized word not in the question words = context.split() for word in words: # Skip short words and words that are in the question if (len(word) > 2 and word[0].isupper() and word.lower() not in q_lower and word.lower() not in self.stop_words): return word.strip(',.!?;:') # Last resort: return the first noun-like word for word in words: if len(word) > 3 and word.lower() not in q_lower and word.lower() not in self.stop_words: return word.strip(',.!?;:') # If all else fails, return the first word that's not a stop word for word in words: if word.lower() not in self.stop_words and len(word) > 2: return word.strip(',.!?;:') # Final fallback return context.split()[0] if context else "Unknown" except Exception as e: print(f"Error extracting answer: {str(e)}") # Return the first word as fallback return context.split()[0] if context else "Unknown" def create_mcq_options(self, question, context, num_options=4, correct_answer=None, global_keywords=None): """ Create multiple choice options for a given question and context. Args: question (str): The question text context (str): The context from which the question was generated num_options (int): Number of options to generate (including correct answer) correct_answer (str, optional): The correct answer if known global_keywords (list, optional): List of keywords from the entire document to use as distractors Returns: dict: Dictionary containing options and correct index """ try: # Extract the correct answer from context if not provided if not correct_answer: correct_answer = self.extract_answer_from_context(question, context) # If we couldn't extract a good answer, use a fallback if not correct_answer or correct_answer == "Unknown": return { 'options': ['Option A', 'Option B', 'Option C', 'Option D'], 'correct_index': 0, 'correct_answer': 'Option A' } # Generate distractors distractors = self._get_distractors( correct_answer, num=min(10, num_options * 2) # Generate more than needed to filter ) # Ensure we have unique distractors distractors = list(set(d for d in distractors if d.lower() != correct_answer.lower())) # If we don't have enough distractors, try using global keywords if len(distractors) < num_options - 1 and global_keywords: # Filter keywords to ensure they are not the correct answer potential_distractors = [k for k in global_keywords if k.lower() != correct_answer.lower()] # Shuffle to get random ones random.shuffle(potential_distractors) for kw in potential_distractors: if kw not in distractors: distractors.append(kw) if len(distractors) >= num_options + 2: # Get a few extra break # If we still don't have enough distractors, add some generic ones generic_distractors = [ 'True', 'False', 'Yes', 'No', 'Maybe', 'Always', 'Never', 'Sometimes', 'Often', 'Rarely', 'All of the above', 'None of the above' ] while len(distractors) < num_options - 1 and generic_distractors: distractor = generic_distractors.pop(0) if distractor.lower() != correct_answer.lower() and distractor not in distractors: distractors.append(distractor) # Select the final set of options options = [correct_answer] + distractors[:(num_options-1)] random.shuffle(options) # Find the index of the correct answer correct_index = options.index(correct_answer) if correct_answer in options else 0 return { 'options': options, 'correct_index': correct_index, 'correct_answer': correct_answer } except Exception as e: print(f"Error generating options: {str(e)}") # Fallback options return { 'options': ['Option A', 'Option B', 'Option C', 'Option D'], 'correct_index': 0, 'correct_answer': 'Option A' }
# Example usageif __name__ == "__main__": og = OptionGenerator() test_question = "What is the capital of France?" test_context = "Paris is the capital of France, known for its art, fashion, and culture." print(f"Question: {test_question}") print(f"Context: {test_context}") mcq = og.create_mcq_options(test_question, test_context) print("\nOptions:") for i, option in enumerate(mcq['options']): marker = "✓" if i == mcq['correct_index'] else " " print(f"{marker} {chr(65+i)}. {option}")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392import reimport nltkimport loggingfrom nltk.tokenize import sent_tokenize, word_tokenizefrom nltk.corpus import stopwordsfrom nltk.tag import pos_tagimport torchfrom transformers import pipeline
# Configure logginglogging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)
# NLTK data setupdef setup_nltk(): """Download required NLTK data.""" try: nltk.data.find('tokenizers/punkt') nltk.data.find('corpora/stopwords') nltk.data.find('taggers/averaged_perceptron_tagger') nltk.data.find('corpora/wordnet') nltk.data.find('corpora/omw-1.4') logger.info("NLTK data is already set up.") return True except LookupError: logger.info("Downloading required NLTK data...") try: nltk.download('punkt', quiet=True) nltk.download('stopwords', quiet=True) nltk.download('averaged_perceptron_tagger', quiet=True) nltk.download('wordnet', quiet=True) nltk.download('omw-1.4', quiet=True) logger.info("NLTK data downloaded successfully.") return True except Exception as e: logger.error(f"Error downloading NLTK data: {str(e)}") return False
# Initialize NLTKif not setup_nltk(): logger.warning("NLTK data not available. Some features may not work properly.")
class QuestionGenerator: def __init__(self, model_name="valhalla/t5-small-qa-qg-hl", use_transformers=False): """Initialize the question generator with enhanced capabilities.""" self.use_transformers = use_transformers self.stop_words = set(stopwords.words('english')) self.device = 'cuda' if torch.cuda.is_available() else 'cpu' # Initialize rule-based system self._init_rule_based_system() # Initialize transformer model if requested if use_transformers: try: logger.info("Loading transformer model...") self.qg_model = pipeline( "text2text-generation", model=model_name, device=0 if self.device == 'cuda' else -1 ) logger.info("Transformer model loaded successfully.") except Exception as e: logger.error(f"Error loading transformer model: {str(e)}") self.use_transformers = False logger.info("Falling back to rule-based generation.") def _init_rule_based_system(self): """Initialize the rule-based question generation system.""" self.wh_words = ['what', 'when', 'where', 'who', 'whom', 'whose', 'which', 'why', 'how'] self.aux_verbs = ['is', 'are', 'was', 'were', 'do', 'does', 'did', 'have', 'has', 'had', 'can', 'could', 'will', 'would', 'shall', 'should', 'may', 'might', 'must'] self.common_nouns = {'time', 'year', 'people', 'way', 'day', 'man', 'thing', 'woman', 'life', 'child', 'world', 'school', 'state', 'family', 'student', 'group', 'country', 'problem'} def _is_good_sentence(self, sentence): """Check if a sentence is suitable for question generation.""" try: if not sentence or not isinstance(sentence, str): return False # Basic length checks words = word_tokenize(sentence) if len(words) < 4: # At least 4 words return False # Check for question mark if '?' in sentence: return False # Check for proper sentence ending if not sentence.strip().endswith(('.', '!', ';', ':')): return False # Check for at least one noun and one verb pos_tags = pos_tag(words) has_noun = any(tag.startswith('NN') for word, tag in pos_tags) has_verb = any(tag.startswith('VB') for word, tag in pos_tags) return has_noun and has_verb except Exception as e: logger.error(f"Error in _is_good_sentence: {str(e)}") return False def _generate_question_what_is(self, words, pos_tags): """Generate 'What is...?' questions.""" for i, (word, tag) in enumerate(pos_tags): if tag.startswith('NN'): return f"What is {word}?" return "" def _generate_question_verb_subject(self, words, pos_tags): """Generate questions by inverting subject and verb.""" for i, (word, tag) in enumerate(pos_tags): if tag.startswith('VB') and i > 0: subject = ' '.join(words[:i]) verb = word rest = ' '.join(words[i+1:]) return f"{verb.capitalize()} {subject} {rest}?" return "" def _generate_question_wh_word(self, words, pos_tags): """Generate questions using WH-words.""" for i, (word, tag) in enumerate(pos_tags): if tag.startswith('VB') and i > 0: wh_word = "What" if i > 0 and pos_tags[i-1][1].startswith('NNP'): wh_word = "Who" return f"{wh_word} {word} {' '.join(words[:i])}?" return "" def _generate_question_from_statement(self, sentence): """Generate a question from a statement using multiple strategies.""" try: if not sentence or not isinstance(sentence, str): return "" # Clean the sentence sentence = sentence.strip() if sentence.endswith('.'): sentence = sentence[:-1].strip() words = word_tokenize(sentence) if len(words) < 4: # Too short for a good question return "" pos_tags = pos_tag(words) # Try different question generation strategies strategies = [ self._generate_question_what_is, self._generate_question_verb_subject, self._generate_question_wh_word ] for strategy in strategies: question = strategy(words, pos_tags) if question: return question # Fallback: ask about the whole sentence return f"Can you explain: {sentence[:100]}...?" except Exception as e: logger.error(f"Error in _generate_question_from_statement: {str(e)}") return "" def generate_question_from_sentence(self, sentence): """Generate a question from a given sentence.""" if not self._is_good_sentence(sentence): return "" try: # Use transformer model if available if self.use_transformers and hasattr(self, 'qg_model'): try: # Prepare input for e2e model input_text = f"generate questions: {sentence}" outputs = self.qg_model(input_text) if outputs and len(outputs) > 0: generated_text = outputs[0]['generated_text'] # The model might generate multiple questions separated by <sep> questions = generated_text.split('<sep>') if questions: return questions[0].strip() except Exception as e: logger.error(f"Transformer generation failed: {e}") # Fallback to rule-based
# First try rule-based generation question = self._generate_question_from_statement(sentence) if question: return question # Fallback to simple question generation words = word_tokenize(sentence) if len(words) < 4: return "" # Try to make a simple question return f"What is the main point about: {sentence[:100]}...?" except Exception as e: logger.error(f"Error generating question: {str(e)}") return "" def _score_sentence(self, sentence): """Score a sentence based on its quality for question generation.""" try: if not self._is_good_sentence(sentence): return 0 words = word_tokenize(sentence) pos_tags = pos_tag(words) # Start with base score score = 1.0 # Check for content words has_noun = any(tag.startswith('NN') for _, tag in pos_tags) has_verb = any(tag.startswith('VB') for _, tag in pos_tags) has_adj = any(tag.startswith('JJ') for _, tag in pos_tags) # Increase score based on content if has_noun and has_verb: score += 2.0 elif has_noun or has_verb: score += 1.0 if has_adj: score += 0.5 # Adjust for sentence length word_count = len(words) if 8 <= word_count <= 25: # Ideal length score += 1.0 # Bonus for proper nouns or numbers if any(tag in {'NNP', 'NNPS', 'CD'} for _, tag in pos_tags): score += 1.0 return max(0.5, score) # Ensure minimum score except Exception as e: logger.error(f"Error in _score_sentence: {str(e)}") return 0.5 def generate_questions(self, text, num_questions=5): """Generate questions from the given text.""" if not text or not text.strip(): logger.warning("Empty text provided for question generation") return [] try: # Split text into sentences sentences = sent_tokenize(text) return self.generate_multiple_questions(sentences, num_questions) except Exception as e: logger.error(f"Error in generate_questions: {str(e)}") return [] def generate_multiple_questions(self, inputs, max_questions=5): """ Generate multiple questions from a list of inputs (context/answer pairs). Args: inputs: List of dicts {'context': str, 'answer': str} or list of strings max_questions: Maximum number of questions to generate Returns: List of generated questions with metadata """ if not inputs or max_questions <= 0: logger.warning("No inputs provided or invalid max_questions") return [] questions = [] used_contexts = set() logger.info(f"Generating up to {max_questions} questions from {len(inputs)} inputs") for item in inputs: try: if len(questions) >= max_questions: break # Handle different input types if isinstance(item, dict): context = item.get('context', '') answer = item.get('answer') else: context = str(item) answer = None if not context or not context.strip(): continue context = context.strip() # Skip if we've already used this context if context in used_contexts: continue question_text = "" # Use transformer model if available if self.use_transformers and hasattr(self, 'qg_model'): try: if answer: input_text = f"answer: {answer} context: {context}" else: input_text = f"generate questions: {context}" outputs = self.qg_model(input_text) if outputs and len(outputs) > 0: question_text = outputs[0]['generated_text'] except Exception as e: logger.error(f"Transformer generation failed: {e}") # Fallback to rule-based if transformer failed or not available if not question_text: question_text = self.generate_question_from_sentence(context) if question_text and question_text not in [q.get('question', '') for q in questions]: q_data = { 'question': question_text, 'context': context, 'score': 1.0, 'type': 'short_answer' } # If we have a known answer, use it for options later if answer: q_data['correct_answer'] = answer questions.append(q_data) used_contexts.add(context) except Exception as e: logger.error(f"Error processing input: {str(e)}") continue # If we still don't have enough questions, create simple ones if len(questions) < max_questions: logger.info(f"Creating simple questions to reach {max_questions} total") for i in range(len(questions), max_questions): # Try to find an unused context or reuse one fallback_context = "General knowledge about the topic" if inputs: # Pick a random input to generate a question from import random item = random.choice(inputs) if isinstance(item, dict): fallback_context = item.get('context', fallback_context) else: fallback_context = str(item) # Create a more specific fallback question words = fallback_context.split() topic_snippet = " ".join(words[:5]) + "..." if len(words) > 5 else fallback_context questions.append({ 'question': f"Explain the significance of: {topic_snippet}", 'context': fallback_context, 'score': 0.5, 'type': 'short_answer' }) logger.info(f"Successfully generated {len(questions)} questions") return questions[:max_questions]
# Example usageif __name__ == "__main__": # Test the question generator qg = QuestionGenerator(use_transformers=False) test_text = """ Machine learning is a branch of artificial intelligence that focuses on building systems that learn from data. These systems can improve their performance over time without being explicitly programmed. There are three main types of machine learning: supervised learning, unsupervised learning, and reinforcement learning. """ print("\nGenerating questions...") questions = qg.generate_questions(test_text, 3) print("\nGenerated Questions:") for i, q in enumerate(questions, 1): print(f"{i}. {q.get('question', 'No question generated')}") print(f" Context: {q.get('context', 'No context')[:100]}...") print()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600import reimport randomimport osimport sysimport nltkfrom nltk.tokenize import sent_tokenize, word_tokenizefrom nltk.corpus import stopwordsfrom nltk.probability import FreqDistfrom nltk.tag import pos_tagfrom collections import defaultdictimport torchfrom transformers import pipeline, AutoModelForQuestionAnswering, AutoTokenizerimport numpy as np
# Simple NLTK data setupdef setup_nltk(): try: # Try to import required NLTK components sent_tokenize("Test") word_tokenize("Test") pos_tag(["test"]) stopwords.words('english') return True except LookupError: try: import nltk nltk.download('punkt', quiet=True) nltk.download('stopwords', quiet=True) nltk.download('averaged_perceptron_tagger', quiet=True) nltk.download('wordnet', quiet=True) nltk.download('omw-1.4', quiet=True) return True except: return False
# Initialize NLTKif not setup_nltk(): print("Warning: Could not initialize NLTK. Some features may not work properly.")
# Set up NLTK data pathdef setup_nltk(): try: # Set NLTK data path to a local directory nltk_data = os.path.join(os.getcwd(), 'nltk_data') os.makedirs(nltk_data, exist_ok=True) nltk.data.path.append(nltk_data) # Download required NLTK data print("\n=== Downloading NLTK Data ===") # Download punkt tokenizer try: nltk.data.find('tokenizers/punkt') print("✓ punkt tokenizer is already available") except LookupError: print("Downloading punkt tokenizer...") nltk.download('punkt', download_dir=nltk_data) print("✓ Downloaded punkt tokenizer") # Download stopwords try: nltk.data.find('corpora/stopwords') print("✓ Stopwords are already available") except LookupError: print("Downloading stopwords...") nltk.download('stopwords', download_dir=nltk_data) print("✓ Downloaded stopwords") # Download averaged_perceptron_tagger try: nltk.data.find('taggers/averaged_perceptron_tagger') print("✓ POS tagger is already available") except LookupError: print("Downloading POS tagger...") nltk.download('averaged_perceptron_tagger', download_dir=nltk_data) print("✓ Downloaded POS tagger") # Download wordnet try: nltk.data.find('corpora/wordnet') print("✓ WordNet is already available") except LookupError: print("Downloading WordNet...") nltk.download('wordnet', download_dir=nltk_data) print("✓ Downloaded WordNet") # Download omw-1.4 try: nltk.data.find('corpora/omw-1.4') print("✓ OMW-1.4 is already available") except LookupError: print("Downloading OMW-1.4...") nltk.download('omw-1.4', download_dir=nltk_data) print("✓ Downloaded OMW-1.4") # Test NLTK components print("\n=== Testing NLTK Components ===") sent_tokenize("This is a test.") word_tokenize("This is a test.") pos_tag(["test", "this", "is", "a", "sentence"]) stopwords.words('english') print("\n=== NLTK Setup Completed Successfully ===\n") return True except Exception as e: print(f"\n⚠ Error during NLTK setup: {str(e)}") print("\nPlease try running these commands manually in a Python shell:") print("import nltk") print("nltk.download('punkt')") print("nltk.download('stopwords')") print("nltk.download('averaged_perceptron_tagger')") print("nltk.download('wordnet')") print("nltk.download('omw-1.4')\n") return False
# Initialize NLTKif not setup_nltk(): print("Failed to initialize NLTK. Some features may not work properly.") print("Trying to continue with limited functionality...\n") try: print(f"✓ {package} is already downloaded") except LookupError: print(f"Downloading {package}...") try: nltk.download(package, download_dir=nltk_data, quiet=False) # Verify download try: nltk.data.find(path) print(f"✓ Successfully downloaded {package}") except LookupError: print(f"⚠ Warning: {package} download verification failed") except Exception as e: print(f"⚠ Error downloading {package}: {str(e)}") if package == 'averaged_perceptron_tagger': print("⚠ This is a critical package. The application may not work properly.") print("\n=== NLTK Data Setup Complete ===\n")
# Initialize NLTK datadownload_nltk_data()
# Initialize NLTK componentstry: # Initialize tokenizers sent_tokenize("Initializing...") word_tokenize("Initializing...") # Initialize POS tagger from nltk.tag import pos_tag pos_tag(["test"]) # Initialize stopwords stopwords.words('english') print("✓ NLTK components initialized successfully")except Exception as e: print(f"⚠ Error initializing NLTK components: {str(e)}")
class QuestionGenerator: def __init__(self, model_name="deepset/roberta-base-squad2", use_transformers=True): """ Initialize the question generator with improved context understanding. Args: model_name (str): Name of the pre-trained model to use use_transformers (bool): Whether to use transformer models for better quality """ print("Initializing question generator with enhanced context understanding...") self.use_transformers = use_transformers self.stop_words = set(stopwords.words('english')) self.device = 'cuda' if torch.cuda.is_available() else 'cpu' if use_transformers: try: print("Loading question generation model...") self.qg_model = pipeline("text2text-generation", model="valhalla/t5-base-qa-qg-hl", device=0 if self.device == 'cuda' else -1) print("Question generation model loaded successfully!") except Exception as e: print(f"Error loading transformer model: {str(e)}") print("Falling back to rule-based generation.") self.use_transformers = False if not self.use_transformers: print("Using rule-based question generation.") self._init_rule_based_system() print("Question generator initialized successfully!") def _init_rule_based_system(self): """Initialize the rule-based question generation system.""" self.wh_words = ['what', 'when', 'where', 'who', 'whom', 'whose', 'which', 'why', 'how'] self.aux_verbs = ['is', 'are', 'was', 'were', 'do', 'does', 'did', 'have', 'has', 'had', 'can', 'could', 'will', 'would', 'shall', 'should', 'may', 'might', 'must'] self.important_pos_tags = {'NN', 'NNS', 'NNP', 'NNPS', 'VBG', 'VBN', 'JJ', 'JJR', 'JJS'} def _extract_key_phrases(self, text): """Extract key phrases from text based on POS tagging.""" words = word_tokenize(text) pos_tags = nltk.pos_tag(words) key_phrases = [] current_phrase = [] for word, tag in pos_tags: if tag in self.important_pos_tags: current_phrase.append(word.lower()) elif current_phrase: if len(current_phrase) > 1: # Only consider phrases with at least 2 words key_phrases.append(' '.join(current_phrase)) current_phrase = [] return list(set(key_phrases)) # Remove duplicates
def generate_question_from_sentence(self, sentence): """Generate a question from a given sentence using rule-based approach.""" words = word_tokenize(sentence) pos_tags = nltk.pos_tag(words) # Find the main verb and subject for i, (word, tag) in enumerate(pos_tags): if tag.startswith('VB'): # Verb # Find the subject before the verb for j in range(i-1, -1, -1): if pos_tags[j][1].startswith('NN'): # Noun subject = ' '.join([w for w, _ in pos_tags[j:i]]) # Create a wh-question question = f"What {pos_tags[i][0]} {subject}?" return question # Fallback: create a what question about the main noun phrase for i, (word, tag) in enumerate(pos_tags): if tag.startswith('NN'): # Noun return f"What is {word}?" # Final fallback return f"What is the main idea of: {sentence[:50]}...?"
def _analyze_text_structure(self, text): """Analyze text structure to identify important concepts and relationships.""" sentences = sent_tokenize(text) key_phrases = self._extract_key_phrases(text) # Find most important terms using frequency distribution words = [word.lower() for word in word_tokenize(text) if word.isalnum() and word.lower() not in self.stop_words] freq_dist = FreqDist(words) return { 'sentences': sentences, 'key_phrases': key_phrases, 'top_terms': [word for word, _ in freq_dist.most_common(10)], 'concept_map': self._build_concept_map(sentences, key_phrases) } def _build_concept_map(self, sentences, key_phrases): """Build a simple concept map showing relationships between key phrases.""" concept_map = defaultdict(list) for phrase in key_phrases: for sentence in sentences: if phrase in sentence.lower(): # Find other key phrases in the same sentence related = [p for p in key_phrases if p != phrase and p in sentence.lower()] concept_map[phrase].extend(related) # Remove duplicates return {k: list(set(v)) for k, v in concept_map.items()} try: print("Attempting to load T5 model... This may take a few minutes on first run.") from transformers import T5ForConditionalGeneration, T5Tokenizer # Use smaller, faster model for web deployment model_name = "t5-base" print(f"Loading {model_name} model...") self.tokenizer = T5Tokenizer.from_pretrained(model_name) self.model = T5ForConditionalGeneration.from_pretrained(model_name) # Use CPU for more reliable deployment (avoid CUDA issues) self.device = torch.device("cpu") self.model.to(self.device) self.model.eval() # Set to evaluation mode self.use_transformers = True print(f"T5 model loaded successfully on {self.device}") except ImportError as e: print(f"Transformers library not installed: {e}") print("Install with: pip install transformers torch") self.use_transformers = False except Exception as e: print(f"Failed to load T5 model: {e}") print("Falling back to rule-based generation.") self.use_transformers = False def generate_questions(self, text, num_questions=5, context_window=3): """ Generate meaningful questions from the given text with better context understanding. Args: text (str): Input text to generate questions from num_questions (int): Number of questions to generate context_window (int): Number of sentences to consider as context Returns: list: List of generated questions with their context """ if not text.strip(): return [] # Analyze the text structure first analysis = self._analyze_text_structure(text) sentences = analysis['sentences'] if not sentences: return [] questions = [] # Generate questions using different strategies if self.use_transformers and hasattr(self, 'qg_model'): # Use transformer-based generation for better quality for i in range(0, len(sentences), context_window): context = ' '.join(sentences[i:i+context_window]) try: # Generate questions for this context window generated = self.qg_model(context, max_length=128, num_return_sequences=1) if generated and len(generated) > 0: question = generated[0]['generated_text'].strip() if question and question[-1] != '?': question += '?' questions.append({ 'question': question, 'context': context, 'type': 'comprehension' }) if len(questions) >= num_questions: break except Exception as e: print(f"Error in transformer-based generation: {str(e)}") continue # Fallback to rule-based generation if needed if len(questions) < num_questions: for i, sentence in enumerate(sentences): if len(sentence.split()) < 5: # Skip very short sentences continue # Generate question using rule-based approach question = self._generate_question_from_sentence(sentence) # Get context (previous and next sentences) start = max(0, i-1) end = min(len(sentences), i+2) context = ' '.join(sentences[start:end]) questions.append({ 'question': question, 'context': context, 'type': 'factual' }) if len(questions) >= num_questions: break # Ensure we have enough questions if len(questions) < num_questions: # Generate some conceptual questions based on key phrases for phrase in analysis['key_phrases'][:num_questions - len(questions)]: questions.append({ 'question': f"Explain the concept of {phrase} in detail.", 'context': f"The concept of {phrase} is important in this context.", 'type': 'conceptual' }) return questions[:num_questions] def _generate_with_transformers(self, sentence, max_length): """Generate question using T5 model.""" if not self.use_transformers or self.model is None: return self._generate_with_rules(sentence) try: # Prepare input for T5 model input_text = f"generate question: {sentence[:300]}" # Limit input length # Tokenize input with error handling inputs = self.tokenizer.encode( input_text, return_tensors="pt", max_length=256, # Reduced for faster processing truncation=True, padding=True ) if self.device: inputs = inputs.to(self.device) # Generate question with optimized parameters with torch.no_grad(): outputs = self.model.generate( inputs, max_length=min(max_length, 64), # Increased output length num_beams=4, # Increased beams for better quality early_stopping=True, do_sample=False, # Deterministic for consistency pad_token_id=self.tokenizer.eos_token_id ) # Decode and clean question question = self.tokenizer.decode(outputs[0], skip_special_tokens=True) cleaned_question = self.clean_question(question) # Validate the generated question if len(cleaned_question) < 10 or not cleaned_question.endswith('?'): print("Generated question quality low, using rule-based fallback") return self._generate_with_rules(sentence) return cleaned_question except Exception as e: print(f"Transformer generation failed: {e}") print("Falling back to rule-based generation") return self._generate_with_rules(sentence) def _generate_with_rules(self, sentence): """Generate question using rule-based approach.""" sentence = sentence.strip() words = sentence.split() # Enhanced question templates based on sentence patterns question_templates = [ # What questions - most common (lambda s: any(word in s.lower() for word in ['is', 'are', 'means', 'refers', 'definition', 'concept']), lambda s: f"What {self._extract_predicate(s)}?"), # Define/Explain questions (lambda s: any(word in s.lower() for word in ['definition', 'meaning', 'concept', 'term']), lambda s: f"Define {self._extract_main_subject(s)}."), # How questions (lambda s: any(word in s.lower() for word in ['process', 'method', 'way', 'procedure', 'algorithm']), lambda s: f"How {self._extract_predicate(s)}?"), # Why questions (lambda s: any(word in s.lower() for word in ['because', 'reason', 'cause', 'purpose', 'important']), lambda s: f"Why {self._extract_predicate(s)}?"), # When questions (lambda s: any(word in s.lower() for word in ['year', 'century', 'time', 'date', 'period', 'era']), lambda s: f"When {self._extract_predicate(s)}?"), # Where questions (lambda s: any(word in s.lower() for word in ['place', 'location', 'country', 'city', 'region']), lambda s: f"Where {self._extract_predicate(s)}?"), # Who questions (lambda s: any(word in s.lower() for word in ['person', 'people', 'scientist', 'author', 'researcher']), lambda s: f"Who {self._extract_predicate(s)}?"), # How questions (lambda s: any(word in s.lower() for word in ['method', 'process', 'way', 'how']), lambda s: f"How {self._extract_predicate(s)}?"), # Why questions (lambda s: any(word in s.lower() for word in ['reason', 'because', 'cause', 'why']), lambda s: f"Why {self._extract_predicate(s)}?"), # Default question (lambda s: True, lambda s: f"What can you tell me about {self._extract_main_subject(s)}?") ] # Apply first matching template for condition, template in question_templates: if condition(sentence): try: question = template(sentence) return self.clean_question(question) except: continue # Fallback return f"What is the main point about {words[0] if words else 'this topic'}?" def _extract_main_subject(self, sentence): """Extract the main subject from a sentence.""" words = sentence.split() # Look for capitalized words (likely proper nouns) subjects = [word.strip('.,!?;:') for word in words if word[0].isupper() and len(word) > 2] if subjects: return subjects[0] # Fallback to first few words return ' '.join(words[:3]) if len(words) >= 3 else sentence def _extract_predicate(self, sentence): """Extract predicate for question formation.""" sentence = sentence.lower() # Remove common sentence starters sentence = re.sub(r'^(the|this|that|these|those|a|an)\s+', '', sentence) # Find verb patterns if ' is ' in sentence: parts = sentence.split(' is ', 1) if len(parts) > 1: return f"is {parts[1].strip('.,!?;:')}" if ' are ' in sentence: parts = sentence.split(' are ', 1) if len(parts) > 1: return f"are {parts[1].strip('.,!?;:')}" # Default fallback words = sentence.split() if len(words) > 3: return ' '.join(words[1:]).strip('.,!?;:') return sentence.strip('.,!?;:') def clean_question(self, question): """ Clean and format the generated question. Args: question (str): Raw generated question Returns: str: Cleaned question """ # Remove extra spaces question = re.sub(r'\s+', ' ', question.strip()) # Ensure question ends with question mark if not question.endswith('?'): question += '?' # Capitalize first letter if question: question = question[0].upper() + question[1:] return question def generate_multiple_questions(self, sentences, max_questions=5): """ Generate multiple questions from a list of sentences. Args: sentences (list): List of sentences to generate questions from max_questions (int): Maximum number of questions to generate Returns: list: List of generated questions with their source sentences """ questions = [] for i, (score, sentence) in enumerate(sentences[:max_questions]): try: question = self.generate_question_from_sentence(sentence) # Filter out low-quality questions if self.is_valid_question(question): questions.append({ 'question': question, 'context': sentence, 'score': score, 'question_id': i + 1 }) except Exception as e: print(f"Error generating question from sentence: {sentence[:50]}... Error: {e}") continue return questions def is_valid_question(self, question): """ Check if a generated question is valid. Args: question (str): Generated question Returns: bool: True if question is valid """ # Basic validation criteria if len(question) < 10: # Too short return False if len(question) > 200: # Too long return False # Must contain question words or end with question mark question_words = ['what', 'who', 'when', 'where', 'why', 'how', 'which', 'is', 'are', 'do', 'does', 'did', 'can', 'could', 'would', 'should'] question_lower = question.lower() has_question_word = any(word in question_lower for word in question_words) ends_with_question_mark = question.endswith('?') return has_question_word or ends_with_question_mark12345678910111213nltk>=3.8transformers>=4.30.0torch>=2.0.0flask>=3.0.0rake-nltk>=1.0.6scikit-learn>=1.3.0numpy>=1.24.0pandas>=1.5.0sentence-transformers>=2.2.0sentencepiece>=0.1.99python-docx>=0.8.11openai>=1.0.0PyPDF2>=3.0.0123456789Machine Learning and Artificial Intelligence
Machine learning is a method of data analysis that automates analytical model building. It is a branch of artificial intelligence based on the idea that systems can learn from data, identify patterns and make decisions with minimal human intervention. The term machine learning was coined in 1959 by Arthur Samuel, an American IBMer and pioneer in the field of computer gaming and artificial intelligence.
There are three main types of machine learning algorithms: supervised learning, unsupervised learning, and reinforcement learning. Supervised learning uses labeled training data to learn a mapping function from input variables to output variables. Common examples include classification and regression problems. Unsupervised learning finds hidden patterns in data without labeled examples, such as clustering and association rule learning. Reinforcement learning involves an agent learning to make decisions by taking actions in an environment to maximize cumulative reward.
Deep learning is a subset of machine learning that uses neural networks with multiple layers to model and understand complex patterns in data. These deep neural networks can automatically learn hierarchical representations of data, making them particularly effective for tasks like image recognition, natural language processing, and speech recognition. Popular deep learning frameworks include TensorFlow, PyTorch, and Keras.
Python has become the most popular programming language for machine learning due to its simplicity, readability, and extensive ecosystem of libraries. Key Python libraries for machine learning include NumPy for numerical computing, Pandas for data manipulation, Scikit-learn for traditional machine learning algorithms, and Matplotlib for data visualization. The combination of these tools makes Python an ideal choice for both beginners and experts in the field of artificial intelligence.12345678910111213141516171819202122232425262728293031323334353637383940import osimport nltk
def download_nltk_data(): # Set NLTK data path to a local directory nltk_data = os.path.join(os.getcwd(), 'nltk_data') os.makedirs(nltk_data, exist_ok=True) nltk.data.path.append(nltk_data) print(f"NLTK data will be downloaded to: {nltk_data}") # List of NLTK packages to download packages = [ 'punkt', 'stopwords', 'averaged_perceptron_tagger', 'averaged_perceptron_tagger_eng', 'wordnet', 'omw-1.4', 'maxent_ne_chunker', 'words', 'punkt' ] print("\n=== Downloading NLTK Data ===") for package in packages: try: print(f"Downloading {package}...") nltk.download(package, download_dir=nltk_data) print(f"✓ {package} downloaded successfully") except Exception as e: print(f"⚠ Error downloading {package}: {str(e)}") print("\n=== NLTK Setup Complete ===") print(f"NLTK data location: {nltk_data}") print("You can now run your application.")
if __name__ == "__main__": download_nltk_data()1234567891011121314151617181920212223242526272829303132import nltk
print("Testing NLTK installation...")
# Test tokenizationtry: from nltk.tokenize import word_tokenize, sent_tokenize text = "This is a test sentence. NLTK should be able to tokenize this." print("\nTokenization test:") print(f"Word tokens: {word_tokenize(text)}") print(f"Sentences: {sent_tokenize(text)}")except Exception as e: print(f"Tokenization error: {e}")
# Test POS taggingtry: from nltk import pos_tag tokens = word_tokenize("This is a test") print("\nPOS tagging test:") print(f"POS tags: {pos_tag(tokens)}")except Exception as e: print(f"POS tagging error: {e}")
# Test stopwordstry: from nltk.corpus import stopwords print("\nStopwords test:") print(f"English stopwords (first 5): {stopwords.words('english')[:5]}")except Exception as e: print(f"Stopwords error: {e}")
print("\nNLTK test complete.")12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576import reimport random
class SimpleQuestionGenerator: def __init__(self): self.question_words = [ 'What', 'Why', 'How', 'When', 'Where', 'Who', 'Which', 'Describe', 'Explain' ] self.auxiliary_verbs = ['is', 'are', 'was', 'were', 'do', 'does', 'did', 'have', 'has', 'had'] def generate_question(self, sentence): """Generate a simple question from a given sentence.""" if not sentence.strip(): return "" # Clean the sentence sentence = sentence.strip().strip('.').strip() # Simple question patterns patterns = [ (r'^(.*?) is (.*?)[.,;]?$', 'What is {}?'), (r'^(.*?) are (.*?)[.,;]?$', 'What are {}?'), (r'^(.*?) was (.*?)[.,;]?$', 'What was {}?'), (r'^(.*?) were (.*?)[.,;]?$', 'What were {}?'), (r'^(.*?) can be (.*?)[.,;]?$', 'How can {} be {}?'), (r'^(.*?) has (.*?)[.,;]?$', 'What has {}?'), (r'^(.*?) have (.*?)[.,;]?$', 'What have {}?'), ] # Try to match patterns for pattern, template in patterns: match = re.match(pattern, sentence, re.IGNORECASE) if match: return template.format(match.group(1)).capitalize() # Default question if no pattern matches return f"What is the main point about: {sentence[:50]}...?" def generate_questions(self, text, num_questions=5): """Generate questions from the given text.""" # Simple sentence splitting sentences = re.split(r'(?<=[.!?])\s+', text) questions = [] for sentence in sentences: if len(questions) >= num_questions: break question = self.generate_question(sentence) if question and question not in questions: questions.append(question) return questions
# Example usageif __name__ == "__main__": # Create a question generator qg = SimpleQuestionGenerator() # Sample text sample_text = """ Machine learning is a branch of artificial intelligence. It focuses on building systems that learn from data. These systems can improve their performance over time. There are three main types of machine learning: supervised, unsupervised, and reinforcement learning. Supervised learning uses labeled data to train models. Unsupervised learning finds patterns in unlabeled data. Reinforcement learning involves training an agent to make decisions through rewards. """ # Generate and print questions print("Generating questions...\n") questions = qg.generate_questions(sample_text, 5) for i, question in enumerate(questions, 1): print(f"{i}. {question}")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175import re
import nltk
import os
from nltk.tokenize import word_tokenize, sent_tokenize
from nltk.corpus import stopwords
from nltk.tag import pos_tag
from nltk.stem import WordNetLemmatizer
from typing import List, Dict, Tuple, Optional
import logging
from datetime import datetime
from question_generator import QuestionGenerator
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class SyllabusProcessor:
def __init__(self):
"""Initialize the SyllabusProcessor with necessary NLTK components."""
try:
# Download required NLTK data
nltk.download('punkt', quiet=True)
nltk.download('stopwords', quiet=True)
nltk.download('averaged_perceptron_tagger', quiet=True)
nltk.download('averaged_perceptron_tagger_eng', quiet=True)
nltk.download('wordnet', quiet=True)
nltk.download('omw-1.4', quiet=True)
# Initialize NLTK components
self.stop_words = set(stopwords.words('english'))
self.lemmatizer = WordNetLemmatizer()
# Import and initialize the PerceptronTagger
from nltk.tag import PerceptronTagger
self.tagger = PerceptronTagger()
# Initialize question generator
self.question_generator = QuestionGenerator()
logger.info("SyllabusProcessor initialized successfully")
except Exception as e:
logger.error(f"Error initializing SyllabusProcessor: {str(e)}")
raise
def parse_syllabus(self, syllabus_text: str) -> Dict[str, List[str]]:
"""
Parse a syllabus text into topics and subtopics.
Args:
syllabus_text: Raw syllabus text with units and topics
Returns:
Dictionary mapping unit names to lists of topics
"""
units = {}
current_unit = "General Topics"
units[current_unit] = []
# Split into lines and process each line
for line in syllabus_text.split('\n'):
line = line.strip()
if not line:
continue
# Check for unit headers (e.g., "Unit 1.0 Introduction" or "Unit 1: Introduction")
unit_match = re.match(r'(?:Unit[\s\-]*\d+(?:\.\d+)?\s*[:\-]?\s*)(.+)', line, re.IGNORECASE)
if unit_match:
current_unit = unit_match.group(0).strip()
units[current_unit] = []
# Check if there are topics on the same line (e.g., "Unit 1 ... 1.1 Topic")
remaining_text = unit_match.group(1)
# Find all topic patterns like "1.1 Topic Name"
inline_topics = re.findall(r'(\d+(?:\.\d+)+[\s\-]+[^0-9]+)', remaining_text)
for t in inline_topics:
# Clean up the topic text
t = re.sub(r'\s*\d+\.\d+.*$', '', t).strip() # Remove next topic number if caught
if t:
units[current_unit].append(t.strip())
continue
# Check for topic lines (e.g., "1.1 Topic Name" or "- Topic Name")
# Handle multiple topics on one line
topics_on_line = re.findall(r'(\d+(?:\.\d+)+[\s\-]+[^0-9]+)', line)
if topics_on_line:
for t in topics_on_line:
t = t.strip()
# Clean up trailing dots or next topic numbers
t = re.sub(r'\s*\d+\.\d+.*$', '', t).strip()
if t and len(t) > 3: # Avoid just numbers
units[current_unit].append(t)
else:
# Check for bullet points
topic_match = re.match(r'(?:[-•*]\s*)(.+)', line)
if topic_match:
topic = topic_match.group(1).strip()
if topic and topic.lower() not in ['introduction', 'overview']:
units[current_unit].append(topic)
return units
def extract_key_terms(self, topic: str) -> List[str]:
"""
Extract key terms from a topic for question generation.
Args:
topic: The topic text
Returns:
List of important terms from the topic
"""
try:
# Use the instance tagger
words = word_tokenize(topic.lower())
pos_tags = self.tagger.tag(words)
# Extract nouns and proper nouns
key_terms = [
word for word, tag in pos_tags
if tag.startswith('NN') and word not in self.stop_words
]
return list(set(key_terms)) # Remove duplicates
except Exception as e:
logger.error(f"Error extracting key terms: {str(e)}")
return []
def generate_topic_based_questions(self, syllabus_text: str, content_text: str,
questions_per_topic: int = 3) -> Dict[str, List[Dict]]:
"""
Generate questions based on syllabus topics.
Args:
syllabus_text: The syllabus text with units and topics
content_text: The content text to generate questions from
questions_per_topic: Number of questions to generate per topic
Returns:
Dictionary mapping topics to lists of questions
"""
# Parse the syllabus
units = self.parse_syllabus(syllabus_text)
# Process content into sentences
sentences = sent_tokenize(content_text)
topic_questions = {}
for unit, topics in units.items():
for topic in topics:
# Extract key terms from the topic
key_terms = self.extract_key_terms(topic)
# Find relevant sentences containing these terms
relevant_sentences = []
for sentence in sentences:
if any(term in sentence.lower() for term in key_terms):
relevant_sentences.append(sentence)
# If no relevant sentences found, use general content
if not relevant_sentences:
relevant_sentences = sentences
# Generate questions from relevant sentences
questions = self.question_generator.generate_multiple_questions(
relevant_sentences,
max_questions=min(questions_per_topic, len(relevant_sentences))
)
if questions:
topic_questions[f"{unit} - {topic}"] = questions
return topic_questions123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113#!/usr/bin/env python# -*- coding: utf-8 -*-"""Quick test to verify all modules can be imported and basic functionality works."""
import sys
def test_imports(): """Test if all modules can be imported.""" print("Testing module imports...") try: print(" [OK] Importing text_processor...") from text_processor import TextProcessor tp = TextProcessor() print(" [OK] TextProcessor initialized") print(" [OK] Importing keyword_extractor...") from keyword_extractor import KeywordExtractor ke = KeywordExtractor() print(" [OK] KeywordExtractor initialized") print(" [OK] Importing question_generator...") from question_generator import QuestionGenerator qg = QuestionGenerator(use_transformers=False) print(" [OK] QuestionGenerator initialized") print(" [OK] Importing option_generator...") from option_generator import OptionGenerator og = OptionGenerator() print(" [OK] OptionGenerator initialized") print(" [OK] Importing syllabus_processor...") from syllabus_processor import SyllabusProcessor sp = SyllabusProcessor() print(" [OK] SyllabusProcessor initialized") print(" [OK] Importing exam_question_system...") from exam_question_system import ExamQuestionSystem print(" [OK] ExamQuestionSystem imported") print(" [OK] Importing app...") from app import app print(" [OK] Flask app imported") print("\n[SUCCESS] All imports successful!") return True except ImportError as e: print(f"\n[ERROR] Import error: {e}") return False except Exception as e: print(f"\n[WARNING] Error during initialization: {e}") print(" (This might be due to missing NLTK data)") return False
def test_basic_functionality(): """Test basic functionality.""" print("\nTesting basic functionality...") try: from text_processor import TextProcessor from keyword_extractor import KeywordExtractor tp = TextProcessor() ke = KeywordExtractor() test_text = "Python is a programming language. It is widely used for web development and data science." print(" [OK] Testing text preprocessing...") processed = tp.preprocess_text(test_text) assert 'sentences' in processed assert len(processed['sentences']) > 0 print(f" [OK] Processed {len(processed['sentences'])} sentences") print(" [OK] Testing keyword extraction...") keywords = ke.extract_keywords_rake(test_text, max_keywords=5) assert len(keywords) > 0 print(f" [OK] Extracted {len(keywords)} keywords") print("\n[SUCCESS] Basic functionality test passed!") return True except Exception as e: print(f"\n[WARNING] Functionality test error: {e}") import traceback traceback.print_exc() return False
if __name__ == "__main__": print("=" * 60) print("Project Import and Basic Functionality Test") print("=" * 60) imports_ok = test_imports() if imports_ok: functionality_ok = test_basic_functionality() if functionality_ok: print("\n" + "=" * 60) print("[SUCCESS] PROJECT IS WORKING!") print("=" * 60) sys.exit(0) else: print("\n" + "=" * 60) print("[WARNING] Imports work but functionality may have issues") print("=" * 60) sys.exit(1) else: print("\n" + "=" * 60) print("[ERROR] PROJECT HAS IMPORT ERRORS") print("=" * 60) sys.exit(1)123456789101112131415161718192021222324252627from local_question_generator import LocalQuestionGenerator
def test_local_generator(): print("Testing Local Question Generator...") # Sample text sample_text = """ The water cycle describes how water evaporates from the Earth's surface, rises into the atmosphere, cools and condenses into rain or snow in clouds, and falls again to the surface as precipitation. """ # Initialize the generator print("Initializing model (this may take a minute)...") qg = LocalQuestionGenerator() # Generate questions print("\nGenerating questions...") questions = qg.generate_questions(sample_text, num_questions=3) # Print results print("\nGenerated Questions:") for i, q in enumerate(questions, 1): print(f"{i}. {q}")
if __name__ == "__main__": test_local_generator()12345678910111213141516171819202122232425262728293031323334353637383940414243import nltkimport os
# Set NLTK data pathdef setup_nltk(): nltk_data = os.path.join(os.getcwd(), 'nltk_data') os.makedirs(nltk_data, exist_ok=True) nltk.data.path.append(nltk_data) return nltk_data
print("Testing NLTK installation...")try: # Test basic NLTK functionality print("Testing tokenizers...") from nltk.tokenize import word_tokenize, sent_tokenize text = "This is a test sentence. NLTK is working!" print(f"Word tokens: {word_tokenize(text)}") print(f"Sentences: {sent_tokenize(text)}") print("\nTesting POS tagging...") from nltk import pos_tag print(f"POS tags: {pos_tag(word_tokenize(text))}") print("\nTesting stopwords...") from nltk.corpus import stopwords print(f"English stopwords: {list(stopwords.words('english'))[:5]}...") print("\n✅ NLTK is working correctly!") except LookupError as e: print(f"\n❌ NLTK data not found: {e}") nltk_data = setup_nltk() print(f"\nPlease run these commands in a Python shell to download NLTK data:") print(f"import nltk") print(f"nltk.download('punkt', download_dir=r'{nltk_data}')") print(f"nltk.download('stopwords', download_dir=r'{nltk_data}')") print(f"nltk.download('averaged_perceptron_tagger', download_dir=r'{nltk_data}')") print(f"nltk.download('wordnet', download_dir=r'{nltk_data}')") print(f"nltk.download('omw-1.4', download_dir=r'{nltk_data}')") except Exception as e: print(f"\n❌ Error testing NLTK: {e}")12345678910111213141516171819202122232425262728from question_generator import QuestionGenerator
def test_question_generator(): print("Testing Question Generator...") # Initialize the question generator qg = QuestionGenerator(use_transformers=False) # Using rule-based for faster testing # Sample text about machine learning sample_text = """ Machine learning is a branch of artificial intelligence that focuses on building systems that learn from data. These systems can improve their performance over time without being explicitly programmed. There are three main types of machine learning: supervised learning, unsupervised learning, and reinforcement learning. Supervised learning uses labeled data to train models, while unsupervised learning finds patterns in unlabeled data. Reinforcement learning involves training an agent to make decisions by rewarding desired behaviors and/or punishing undesired ones. """ print("\nGenerating questions...") questions = qg.generate_questions(sample_text, num_questions=3) print("\nGenerated Questions:") for i, q in enumerate(questions, 1): print(f"{i}. {q}")
if __name__ == "__main__": test_question_generator()12345678910111213141516171819202122232425262728293031323334353637import logging
import sys
import os
# Ensure the current directory is in the python path
sys.path.append(os.getcwd())
from syllabus_processor import SyllabusProcessor
# Configure logging
logging.basicConfig(level=logging.INFO)
def test_syllabus_parsing():
syllabus_text = """Unit 1.0 Introduction (9 Lectures) Self-Learning (SL) 1.1 Data for Graphics. 1.2 Design principles 1.3 Value for visualization 1.4 Categorical 1.5 Time series 1.6 statistical data graphics I 1.7 statistical data graphics II 1.8 Introduction to Visualization Tools I 1.9 Introduction to Visualization Tools II
Unit 2.0 Graphics Pipeline and Aesthetics and Perception (10 Lectures) 2.1 Primitives: vertices edges and triangles 2.2 Model transforms 2.3 Translations 2.4 Rotations 2.5 scaling 2.6 View transform, Perspective transform, window transform 2.7 Graphical Perception Theory 2.8 Experimentation, and the Application 2.9 Graphical Integrity, Layering and Separation 2.10 Color and Information, Using Space"""
print("Initializing SyllabusProcessor...")
try:
processor = SyllabusProcessor()
print("\nParsing syllabus...")
units = processor.parse_syllabus(syllabus_text)
print("\nParsing Results:")
for unit, topics in units.items():
print(f"\nUnit: {unit}")
print(f"Topics found: {len(topics)}")
for topic in topics:
print(f" - {topic}")
except Exception as e:
print(f"Error: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
test_syllabus_parsing()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118import nltkimport reimport stringfrom nltk.corpus import stopwordsfrom nltk.tokenize import sent_tokenize, word_tokenizefrom nltk.stem import WordNetLemmatizer
class TextProcessor: def __init__(self): """Initialize the text processor with required NLTK data.""" self.download_nltk_data() self.stop_words = set(stopwords.words('english')) self.lemmatizer = WordNetLemmatizer() def download_nltk_data(self): """Download required NLTK data if not already present.""" required_data = [ ('tokenizers/punkt_tab', 'punkt_tab'), ('tokenizers/punkt', 'punkt'), ('corpora/stopwords', 'stopwords'), ('corpora/wordnet', 'wordnet'), ('corpora/omw-1.4', 'omw-1.4') ] for path, name in required_data: try: nltk.data.find(path) except LookupError: print(f"Downloading NLTK {name}...") nltk.download(name) def clean_text(self, text): """ Clean and preprocess the input text. Args: text (str): Raw input text Returns: str: Cleaned text """ # Remove extra whitespace and normalize text = re.sub(r'\s+', ' ', text.strip()) # Remove common header/footer patterns (e.g., "Page 1 of 10", "Unit 1") text = re.sub(r'Page\s+\d+\s+of\s+\d+', '', text, flags=re.IGNORECASE) text = re.sub(r'Unit\s+\d+(\.\d+)?', '', text, flags=re.IGNORECASE) # Remove standalone numbers (often page numbers or list markers) text = re.sub(r'\b\d+\b', '', text) # Remove special characters but keep sentence structure # Keep periods, question marks, exclamation points, commas, and hyphens text = re.sub(r'[^\w\s\.\?\!,\-]', '', text) # Remove multiple periods/spaces text = re.sub(r'\.+', '.', text) text = re.sub(r'\s+', ' ', text).strip() return text def tokenize_sentences(self, text): """ Tokenize text into sentences. Args: text (str): Input text Returns: list: List of sentences """ sentences = sent_tokenize(text) # Filter out very short sentences (less than 5 words) filtered_sentences = [s for s in sentences if len(word_tokenize(s)) >= 5] return filtered_sentences def tokenize_words(self, text): """ Tokenize text into words and remove stopwords. Args: text (str): Input text Returns: list: List of processed words """ words = word_tokenize(text.lower()) # Remove punctuation and stopwords words = [word for word in words if word not in string.punctuation] words = [word for word in words if word not in self.stop_words] # Lemmatize words words = [self.lemmatizer.lemmatize(word) for word in words] return words def preprocess_text(self, text): """ Complete preprocessing pipeline. Args: text (str): Raw input text Returns: dict: Processed text components """ cleaned_text = self.clean_text(text) sentences = self.tokenize_sentences(cleaned_text) words = self.tokenize_words(cleaned_text) return { 'cleaned_text': cleaned_text, 'sentences': sentences, 'words': words, 'word_count': len(words), 'sentence_count': len(sentences) }123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124import logging
from exam_question_system import ExamQuestionSystem
from option_generator import OptionGenerator
# Configure logging
logging.basicConfig(level=logging.INFO)
def verify_generation():
# Mock content
content = """
Data visualization is the graphical representation of information and data.
By using visual elements like charts, graphs, and maps, data visualization
tools provide an accessible way to see and understand trends, outliers, and
patterns in data. In the world of Big Data, data visualization tools and
technologies are essential to analyze massive amounts of information and
make data-driven decisions.
Design principles in data visualization include understanding the audience,
choosing the right chart type, and using color effectively. Good design
makes complex data more accessible, understandable, and usable.
Exploratory Data Analysis (EDA) is an approach to analyzing data sets to
summarize their main characteristics, often with visual methods. A statistical
model can be used or not, but primarily EDA is for seeing what the data can
tell us beyond the formal modeling or hypothesis testing task.
"""
print("Initializing ExamQuestionSystem...")
system = ExamQuestionSystem(use_transformers=False) # Use rule-based for speed in test
print("\nGenerating questions (Target: 2 MCQ, 1 Short, 1 Long)...")
# We simulate the logic in app.py
num_mcq = 2
num_short = 1
num_long = 1
total_needed = num_mcq + num_short + num_long
results = system.generate_exam_questions(
input_text=content,
max_questions=total_needed,
include_mcq=False,
syllabus_text=content
)
all_questions = results.get('questions', [])
print(f"\nTotal questions generated: {len(all_questions)}")
# Simulate app.py distribution logic
generated_questions = {
'mcq_questions': [],
'short_questions': [],
'long_questions': []
}
# Filter out questions that are too simple for Long answers
long_candidates = [q for q in all_questions if len(q.get('context', '').split()) > 10]
short_candidates = [q for q in all_questions if q not in long_candidates]
# If we don't have enough long candidates, take from short
if len(long_candidates) < num_long:
needed = num_long - len(long_candidates)
long_candidates.extend(short_candidates[:needed])
short_candidates = short_candidates[needed:]
# 3. Process Long Questions
for _ in range(num_long):
if long_candidates:
q = long_candidates.pop(0)
q['type'] = 'long_answer'
generated_questions['long_questions'].append(q)
if q in all_questions:
all_questions.remove(q)
# 2. Process Short Questions
for _ in range(num_short):
if short_candidates:
q = short_candidates.pop(0)
q['type'] = 'short_answer'
generated_questions['short_questions'].append(q)
if q in all_questions:
all_questions.remove(q)
elif all_questions:
q = all_questions.pop(0)
q['type'] = 'short_answer'
generated_questions['short_questions'].append(q)
# 1. Process MCQs
global_keywords = [k[1] for k in results.get('keywords', [])]
print(f"\nGlobal Keywords (Cleaned): {global_keywords}")
for _ in range(num_mcq):
if all_questions:
q = all_questions.pop(0)
try:
mcq_data = system.option_generator.create_mcq_options(
q['question'],
q['context'],
correct_answer=q.get('correct_answer'),
global_keywords=global_keywords
)
if mcq_data and 'options' in mcq_data:
q.update(mcq_data)
q['type'] = 'mcq'
generated_questions['mcq_questions'].append(q)
except Exception as e:
print(f"Error generating options: {e}")
# Print Results
print("\n--- Generation Results ---")
print(f"MCQs: {len(generated_questions['mcq_questions'])}")
for q in generated_questions['mcq_questions']:
print(f" Q: {q['question']}")
print(f" Options: {q.get('options')}")
print(f"\nShort Questions: {len(generated_questions['short_questions'])}")
for q in generated_questions['short_questions']:
print(f" Q: {q['question']}")
print(f"\nLong Questions: {len(generated_questions['long_questions'])}")
for q in generated_questions['long_questions']:
print(f" Q: {q['question']}")
if __name__ == "__main__":
verify_generation().envAdded+1
PROJECT_REVIEW.mdAdded+250
README.md+50-2
app.pyAdded+497
download_nltk_data.pyAdded+29
exam_question_system.pyAdded+376
generated_questions.jsonAdded+22
keyword_extractor.pyAdded+138
local_question_generator.pyAdded+69
nltk_install.pyAdded+69
nltk_setup.pyAdded+56
option_generator.pyAdded+272
question_generator.pyAdded+392
question_generator_old.pyAdded+600
requirements.txtAdded+13
sample_text.txtAdded+9
setup_nltk.pyAdded+40
simple_nltk_test.pyAdded+32
simple_question_generator.pyAdded+76
syllabus_processor.pyAdded+175
test_imports.pyAdded+113
test_local_generator.pyAdded+27
test_nltk.pyAdded+43
test_question_generator.pyAdded+28
test_syllabus.pyAdded+37
text_processor.pyAdded+118
verify_generation.pyAdded+124