def handler_times_course_offered_next(self, params: QueryParameters) -> str: params.require_class_id() sections = self.datastore.get_sections_from_id_and_quarter( params.class_id, False) if len(sections) == 0: return f"Sorry, there are no sections of STAT {params.class_id} next quarter." times = [] for section in sections: if len(section.times_offered) > 0: times.append(section.times_offered) if len(times) > 2: times[-1] = "and " + str(times[-1]) if len(times) == 0: return f"Sorry, {sections[0].full_name()} isn't offered synchronously next quarter. " elif len(times) == 2: return f"{sections[0].full_name()} is offered at {times[0] + ' and ' + times[1]} each week next quarter." else: return f"{sections[0].full_name()} is offered at {', '.join(times)} each week next quarter."
def handler_professor_courses_next(self, params: QueryParameters) -> str: params.require_professor() sections = self.datastore.get_sections_from_professor( params.professor, False) if len(sections) == 0: return f"Sorry, {params.professor} is not teaching any courses next quarter." classes = set() # Correct formatting and no duplicates for section in sections: name = section.full_name() classes.add(name) classes = list(classes) if len(classes) > 2: classes[-1] = "and " + str(classes[-1]) if len(classes) == 0: return f"Sorry, Professor {params.professor.title()} is not teaching any classes next quarter." elif len(classes) == 2: return f"Professor {params.professor.title()} is teaching {classes[0] + ' and ' + classes[1]} next quarter." else: return f"Professor {params.professor.title()} is teaching {', '.join(classes)} next quarter."
def handler_title_of_course(self, params: QueryParameters) -> str: params.require_class_id() course = self.get_course(params.class_id) return f"The title of {course.full_name()} is {course.title}."
def handler_course_id_of_course(self, params: QueryParameters) -> str: params.require_class_id() course = self.get_course(params.class_id) return f"The class number of {course.full_name()} is {course.id}."
def handler_hours_of_course(self, params: QueryParameters) -> str: params.require_class_id() course = self.get_course(params.class_id) return f"{course.full_name()} meets for {course.units} hours a week."
def handler_teachers_of_course_next(self, params: QueryParameters) -> str: params.require_class_id() sections = self.datastore.get_sections_from_id_and_quarter( params.class_id, False) if len(sections) == 0: return f"Sorry, there are no sections of STAT {params.class_id} next quarter." professors = set() # Correct formatting and no duplicates for section in sections: name = section.teacher.split(", ")[0].title() professors.add(name) professors = list(professors) if len(professors) > 1: professors[-1] = "and " + professors[-1] # Can have 0, 1, or multiple professors teaching a class if len(professors) == 1: return f"Professor {', '.join(professors)} is teaching {sections[0].full_name()} next quarter." elif len(professors) == 0: return f"Sorry, no one is teaching STAT {params.class_id} next quarter." else: return f"Professors {', '.join(professors)} are teaching {sections[0].full_name()} next quarter."
def handler_description_of_course( self, params: QueryParameters ) -> str: #TODO: Make sure response sounds natural params.require_class_id() course = self.get_course(params.class_id) return f"{course.full_name()} is about {course.about}."
def handler_level_of_course( self, params: QueryParameters) -> str: #TODO: Verify works correctly params.require_class_id() course = self.get_course(params.class_id) return f"The level of {course.full_name()} is {str(course.id)[0]}00"
def handler_is_course_elective(self, params: QueryParameters) -> str: params.require_class_id() course = self.get_course(params.class_id) if course.elective: return f"Yes, {course.full_name()} is an elective." else: return f"No, {course.full_name()} is not an elective."
def handler_does_course_involve_coding(self, params: QueryParameters) -> str: params.require_class_id() course = self.get_course(params.class_id) if course.coding_involved: return f"Yes, {course.full_name()} involves coding." else: return f"No, {course.full_name()} does not involve coding."
def handler_terms_course_offered(self, params: QueryParameters) -> str: params.require_class_id() course = self.get_course(params.class_id) if len(course.terms) == 0: return f"Sorry, {course.full_name()} is not a regularly offered class." if len(course.terms) == 1: return f"{course.full_name()} is typically offered in the {', '.join([t.title() for t in course.terms])}." else: return f"{course.full_name()} is typically offered in the following quarters: {', '.join([t.title() for t in course.terms])}."
def handler_course_offered_in_term(self, params: QueryParameters) -> str: params.require_class_id() params.require_term() course = self.get_course(params.class_id) if params.term in course.terms: return f"Yes, STAT {params.class_id} is offered in the {params.term.title()}." else: return f"Sorry, STAT {params.class_id} is not offered in the {params.term.title()}."
def handler_enrollment_cap_of_course_next(self, params: QueryParameters) -> str: params.require_class_id() sections = self.datastore.get_sections_from_id_and_quarter( params.class_id, False) cap = 0 for section in sections: cap += section.enrollment_cap return f"The enrollment cap for {sections[0].full_name()} next quarter is {cap}."
def handler_number_of_terms_course_offered(self, params: QueryParameters) -> str: params.require_class_id() course = self.get_course(params.class_id) numberOfTerms = len(course.terms) if numberOfTerms == 1: return f"{course.full_name()} is usually offered in {numberOfTerms} quarter." else: return f"{course.full_name()} is usually offered in {numberOfTerms} quarters."
def handler_prereqs_of_course(self, params: QueryParameters) -> str: # Require the presence of variable for a given intent, this corresponds to the [variable] in the query params.require_class_id() # Retrieve the course object via self.get_course() this handles the case of an invalid course automatically course = self.get_course(params.class_id) #Special case response since prereqs could be None if course.prereqs is None: return f"{course.full_name()} has no prerequisite courses." #prefer to use course.full_name() as opposed to course.title return f"The prerequisites for {course.full_name()} are: {course.prereqs}"
def handler_find_course_about_topic( self, params: QueryParameters) -> str: #TODO: Improve response message params.require_topic() courses = self.datastore.get_courses_about_topic(params.topic) classes = [] for course in courses: classes.append(course.full_name()) if len(classes) > 2: classes[-1] = "and " + str(classes[-1]) if len(classes) == 0: return f"Sorry, there aren't any courses about {params.topic}" elif len(classes) == 1: return f"{', '.join(classes)} is about {params.topic}." elif len(classes) == 2: return f"{classes[0] + ' and ' + classes[1]} are about {params.topic}." else: return f"{', '.join(classes)} are about {params.topic}."
def create_query_params(self, generalized: str, variables: List[str]) -> QueryParameters: """Creates a QueryParameters object from a generalized query and its variables.""" var_locations = re.findall(r"(\[(.*?)\])", generalized) class_id = None term = None professor = None topic = None for i in range(len(var_locations)): if var_locations[i][0] == "[class]": if variables[i].isdigit(): class_id = int(variables[i]) else: class_id = self.datastore.get_id_of_class(variables[i]) elif var_locations[i][0] == "[term]": term = variables[i] if var_locations[i][0] == "[professor]": professor = variables[i] if var_locations[i][0] == "[topic]": topic = variables[i] return QueryParameters(class_id, term, professor, topic)