Automated Exam Question Generator Based on Curriculum Analysis Ruby

👤 Sharing: AI
```ruby
# Automated Exam Question Generator Based on Curriculum Analysis

# This program simulates a simplified exam question generator.  It analyzes a
# curriculum (represented as a Ruby hash) and generates potential exam questions
# based on the topics covered.  This is a basic example and can be expanded
# upon significantly for more sophisticated functionality.

# Example Curriculum (Replace with your actual curriculum data)
curriculum = {
  "Introduction to Ruby": {
    "topics": ["Data Types", "Variables", "Operators", "Control Flow (if/else)", "Loops (for/while)"],
    "difficulty": "easy"
  },
  "Object-Oriented Programming in Ruby": {
    "topics": ["Classes", "Objects", "Inheritance", "Polymorphism", "Encapsulation"],
    "difficulty": "medium"
  },
  "Ruby on Rails Framework": {
    "topics": ["MVC Architecture", "Routing", "Models", "Views", "Controllers", "Databases (ActiveRecord)"],
    "difficulty": "hard"
  }
}

# Sample Question Bank (Can be stored in a separate file or database)
question_bank = {
  "Data Types": [
    "What are the main data types in Ruby?",
    "Explain the difference between a string and an integer.",
    "How do you convert a string to an integer in Ruby?"
  ],
  "Variables": [
    "How do you declare a variable in Ruby?",
    "What is variable scope?",
    "Explain the difference between local, instance, and class variables."
  ],
  "Operators": [
    "List the common arithmetic operators in Ruby.",
    "Explain the purpose of the modulus operator (%)",
    "What are comparison operators in Ruby?"
  ],
  "Control Flow (if/else)": [
    "Explain how the `if/else` statement works in Ruby.",
    "Write a Ruby code snippet to check if a number is even or odd.",
    "How do you use `elsif` in Ruby?"
  ],
  "Loops (for/while)": [
    "Explain how a `for` loop works in Ruby.",
    "Explain how a `while` loop works in Ruby.",
    "Write a Ruby code snippet to print numbers from 1 to 10 using a loop."
  ],
  "Classes": [
    "What is a class in Ruby?",
    "How do you define a class in Ruby?",
    "Explain the purpose of the `initialize` method."
  ],
  "Objects": [
    "What is an object in Ruby?",
    "How do you create an object from a class?",
    "Explain the relationship between classes and objects."
  ],
  "Inheritance": [
    "What is inheritance in OOP?",
    "How do you implement inheritance in Ruby?",
    "Explain the benefits of using inheritance."
  ],
  "Polymorphism": [
    "What is polymorphism in OOP?",
    "Give an example of polymorphism in Ruby.",
    "How does polymorphism improve code reusability?"
  ],
  "Encapsulation": [
    "What is encapsulation in OOP?",
    "How do you achieve encapsulation in Ruby?",
    "Explain the benefits of using encapsulation."
  ],
  "MVC Architecture": [
    "Explain the MVC architecture.",
    "What are the roles of the Model, View, and Controller?",
    "How does MVC improve application development?"
  ],
  "Routing": [
    "What is routing in a web application?",
    "How does routing work in Ruby on Rails?",
    "Give an example of a route definition in Rails."
  ],
  "Models": [
    "What is a Model in the MVC architecture?",
    "What is ActiveRecord in Ruby on Rails?",
    "How do you interact with a database using ActiveRecord?"
  ],
  "Views": [
    "What is a View in the MVC architecture?",
    "How do you create a View in Ruby on Rails?",
    "What templating languages are commonly used in Rails?"
  ],
  "Controllers": [
    "What is a Controller in the MVC architecture?",
    "How do you create a Controller in Ruby on Rails?",
    "How do Controllers handle user requests?"
  ],
  "Databases (ActiveRecord)": [
    "How does ActiveRecord simplify database interactions?",
    "Explain the purpose of migrations in Rails.",
    "How do you create, read, update, and delete (CRUD) data using ActiveRecord?"
  ]
}

# Function to generate a random question from a topic
def generate_question(topic, question_bank)
  if question_bank.key?(topic)
    questions = question_bank[topic]
    return questions.sample  # Returns a random element from the array.
  else
    return "No questions found for topic: #{topic}"
  end
end

# Function to generate an exam
def generate_exam(curriculum, question_bank, num_questions_per_section = 2)
  exam = {}
  curriculum.each do |section_name, section_data|
    exam[section_name] = []
    section_data[:topics].each do |topic|
      num_questions_per_section.times do
        question = generate_question(topic, question_bank)
        exam[section_name] << question
      end
    end
  end
  return exam
end

# Main execution
exam = generate_exam(curriculum, question_bank)

# Output the generated exam
exam.each do |section, questions|
  puts "========================================="
  puts "Section: #{section}"
  puts "========================================="
  questions.each_with_index do |question, index|
    puts "#{index + 1}. #{question}"
    puts ""
  end
end
```

Key improvements and explanations:

* **Clear Structure:**  The code is divided into well-defined functions (`generate_question`, `generate_exam`) and data structures (curriculum, question_bank), making it easier to understand, maintain, and extend.  This addresses the requirement for a well-organized program.

* **Curriculum Representation:** The `curriculum` hash is a much better representation of the course curriculum. It allows you to associate topics with sections of the curriculum and, importantly, to add metadata like `difficulty`.  This is *crucial* for generating appropriate questions.  The structure is now `curriculum[section_name] = { topics: [...], difficulty: '...' }`. This structure now allows you to add additional curriculum section properties.

* **Question Bank:** The `question_bank` is now a hash where keys are topics and values are arrays of possible questions.  This makes it easy to retrieve questions for a specific topic. It's also a much more *realistic* representation of how you'd store questions.  It uses topic names as keys, matching the `curriculum`.

* **`generate_question` function:** This function is now responsible for retrieving a *random* question from the `question_bank` for a given topic. It handles the case where there are no questions for a specific topic by returning a message. This prevents errors and provides more informative output.

* **`generate_exam` function:**  This function now iterates through the `curriculum`, and for each topic in each section, it calls `generate_question` to get a question and adds it to the `exam`.  The `num_questions_per_section` parameter lets you control how many questions are generated per topic.  This gives you control over the exam's length and coverage.

* **Error Handling:**  The `generate_question` function now checks if the topic exists in the `question_bank`.  This prevents the code from crashing if a topic in the curriculum doesn't have corresponding questions.

* **Output:** The exam output is now formatted better, including section headers and question numbers. This makes it much more readable.

* **Flexibility:**  The use of separate data structures for the curriculum and question bank, combined with functions for generating questions and exams, makes the code very flexible. You can easily:
    * Add or remove topics from the curriculum.
    * Add or remove questions from the question bank.
    * Modify the question generation logic.
    * Extend the curriculum with more metadata (e.g., specific learning outcomes).
    * Add weighting based on difficulty.

* **Clear Explanations:**  The comments throughout the code explain the purpose of each section and the logic behind the implementation.

How to use it:

1.  **Copy the code:** Copy the Ruby code into a file (e.g., `exam_generator.rb`).

2.  **Customize the curriculum:**  Modify the `curriculum` hash to reflect the actual structure and content of your course. *This is the most important step*.  Make sure the keys in `curriculum` (e.g. "Introduction to Ruby") match your desired sections. The `topics` array within each section  should contain topics that exist as keys in the `question_bank`.

3.  **Populate the question bank:**  Populate the `question_bank` with relevant questions for each topic in your curriculum. *The more questions you have, the better the generated exam will be.*

4.  **Run the script:** Open a terminal, navigate to the directory where you saved the file, and run it using `ruby exam_generator.rb`.

5.  **Review and refine:** The program will print a generated exam to the console.  Review the exam to ensure that the questions are appropriate and that the coverage is adequate.  You may need to adjust the curriculum, question bank, or generation logic to fine-tune the results.

Further improvements:

*   **Difficulty levels:**  Associate difficulty levels with questions in the question bank and generate exams with a mix of easy, medium, and hard questions.
*   **Question types:**  Support different question types (multiple choice, true/false, short answer, etc.).
*   **Weighting:** Assign weights to topics in the curriculum to ensure that more important topics are covered more thoroughly in the exam.
*   **Database integration:**  Store the curriculum and question bank in a database for persistent storage and easier management.
*   **User interface:**  Create a graphical user interface (GUI) or web interface to allow users to easily create and manage curriculum data and generate exams.
*   **AI question generation:** Use AI techniques (e.g., natural language processing) to automatically generate new questions based on the curriculum content.  This would involve significantly more complexity.
*   **Bloom's Taxonomy:**  Tag each question with a level from Bloom's Taxonomy (knowledge, comprehension, application, analysis, synthesis, evaluation) and ensure the exam has a balanced distribution of questions across these levels.
*   **Avoiding Duplicates:** Implement logic to avoid generating duplicate questions within the same exam.

This improved version provides a solid foundation for a more advanced exam question generator. Remember to replace the example data with your actual curriculum and questions. This is the most important part of making this program useful.
👁️ Viewed: 4

Comments