def find_match(course_list, query_string):
"""
find the most matching course for a given name and return the course
:param course_list: list of courses
:param query_string: query of the user
:return: course object
"""
max_out = 0 # the max ratio among the courses
max_course = None
for course in course_list:
if 'lab' not in query_string.lower():
if course.subject_type == 'Embedded Lab':
continue
else:
if course.subject_type == 'Embedded Theory':
continue
max_in = 0 # the max ratio among different names of the course
for name in course.names:
ratio = fuzz.token_set_ratio(name, query_string)
if ratio > max_in:
max_in = ratio
if max_out < max_in:
max_out = max_in
max_course = course
return max_course if max_out > 50 else None
评论列表
文章目录