Teaching Machine

Calvin (Deutschbein)

W12Fri: 15 Nov

Announcements

  • Enigma Ongoing
    • You should have a working EnigmaRotor by now.
    • Finish on time, new project next week.

Today

  • Data-Driven Programming
    • Classes
    • Data Structures
    • "The Teaching Machine"
    • Prep for P5: Advencture

Data-Driven Programs

  • In most programming languages, data structures are easier to manipulate than the code itself
  • Often useful then to push as much of the program behavior as possible into data structures rather than methods and functions
    • Programs working in this fashion are said to be data driven
  • In a data-driven model, the actual program (commonly called the driver) is generally very small and simple simple managing:
    • Reading in data from a file into a suitable internal structure
    • Using the data structure to control the flow of the program

The Teaching Machine

  • Suppose you wanted to write a program that would give an “intelligent” quiz
    • Correct answers would move the client on to other, more difficult questions
    • Incorrect answers might give some explanation and then ask some follow-up questions
  • Having the questions and answers in some sort of data structure would make sense
  • The teacher would generally be who comes up with the questions and progression though, and most teachers are not experts at writing code
    • Need a format that is more not code based where teachers could construct the questions and progression
    • Need to translate that format into more common data structures that the computer can understand then and act on

File Formats

  • One common method of achieving this is to have configuration or data files
  • The general format of a file is shown to the left, and an example question to the right
  • Identifying name for first question
    Text of first question
    ------
    responseA: name of next question
    responseB: name of next question
    responseC: name of next question
    responseD: name of next question
    
    ...more questions/answers...
    
    RemQ1
    What is the value of 17 % 4?
        a. 0
        b. 1
        c. 3
        d. 4
    ------
    a: RemQ2
    b: PrecQ1
    c: RemQ2
    d: RemQ2

An Internal Representation

image/svg+xml course questions TMCourse name : question str TMQuestion name text answers str str array of strings response : name str str

Data file to Internal Rep

    DivQ1 What is the value of 3 / 2? ----- 1: DivQ2 1.5: DivQ4 *: DivQ3
    DivQ2 The / operator produces floats. What is the value of 9 / 3? ----- 3: DivQ2 3.0: DivQ4 *: DivQ3
    DivQ3 What is the value of 5 / 4? ----- 1.25: DivQ4 *: DivQ2
    DivQ4 What is the value of 9 // 4? ----- 2: EXIT *: DivQ1

    image/svg+xml DivQ1 DivQ2 DivQ3 DivQ4 questions START name text answers DivQ1 1 DivQ2 1.5 DivQ4 * DivQ3 What is the value of 3 / 2? name text answers 3 DivQ2 3.0 DivQ4 * DivQ3 DivQ2 The / operator produces floats. What is the value of 9 / 3? name text answers DivQ3 1.25 DivQ4 * DivQ2 What is the value of 5 / 4? name text answers 2 EXIT * DivQ1 DivQ4 What is the value of 9 // 4?

The TeachingMachine Program

    from TMCourse import read_course def teaching_machine(): course = choose_course() course.run() def choose_course(): # Returns a course chosen by the user. while True: try: filename = input("Enter course name: ") with open(filename + ".txt") as f: return read_course(f) except IOError: print("Please enter a valid course name.")

The TMCourse Class

    from TMQuestion import TMQuestion, read_question class TMCourse: def __init__(self, questions): # New TMCourse object with the specified questions. self._questions = questions def get_question(self, name): # Returns the question with the specified name. return self._questions[name] def run(self): # Steps through the questions in this course. current = "START" while current != "EXIT": question = self.get_question(current) for line in question.get_text(): print(line) answer = input("> ").strip().upper() next = question.lookup_answer(answer) if next is None: print("I don't understand that response.") else: current = next def read_course(fh): # Reads the entire course from the data file handle fh. questions = { } finished = False while not finished: question = read_question(fh) if question is None: finished = True else: name = question.get_name() if len(questions) == 0: questions["START"] = question questions[name] = question return TMCourse(questions)

The TMQuestion Class

    MARKER = "-----" class TMQuestion: def __init__(self, name, text, answers): # New TMQuestion object with these attributes. self._name = name self._text = text self._answers = answers def get_name(self): # Returns the name of this question. return self._name def get_text(self): # Returns the list containing the text of this question. return self._text def lookup_answer(self, response): # Looks up the response to find the next question. next_question = self._answers.get(response, None) if next_question is None: next_question = self._answers.get("*", None) return next_question def read_question(fh): # Reads one question from the data file handle fh. name = fh.readline().rstrip() if name == "": return None text = [ ] finished = False while not finished: line = fh.readline().rstrip() if line == MARKER: finished = True else: text.append(line) answers = { } finished = False while not finished: line = fh.readline().rstrip() if line == "": finished = True else: colon = line.find(":") if colon == -1: raise ValueError("Missing colon in " + line) response = line[:colon].strip().upper() next_question = line[colon + 1:].strip() answers[response] = next_question return TMQuestion(name, text, answers)

Teaching the Adventure

  • The TeachingMachine program can process and run any data file that has the correct format
    • Does not need to technically be a series of educational questions
    • This is part of the strength of the data driven model: data is easy to change, programs less so
  • Could make a sort of “Choose your own adventure” game out of it!

Today

Today

  • Data-Driven Programming
    • Classes
    • Data Structures
    • "The Teaching Machine"
    • Prep for P5: Advencture

Announcements

  • Enigma Ongoing
    • You should have a working EnigmaRotor by now.
    • Finish on time, new project next week.