Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/app/api/courseNameAutocomplete/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import untypedCourseNameTable from 'src/data/course_name_table.json';
import type { GenericFetchedData } from 'src/utils/GenericFetchedData';
import { type SearchQuery } from 'src/utils/SearchQuery';

type TableEntry = SearchQuery & { totalStudents: number };

const courseNameTable = untypedCourseNameTable as {
[key: string]: SearchQuery[];
[key: string]: TableEntry[];
};

//find all the prefixes in the course name table
Expand Down Expand Up @@ -200,12 +202,16 @@ export async function GET(request: Request) {
})
.sort((a, b) => b - a)[0] ?? 0;

const popularityBonus =
result.totalStudents > 0 ? -Math.log10(result.totalStudents + 1) : 0;

return {
distance:
(smartNumberMatch < 0 ? 0 : distanceMetric) + // if checking course number, ignore distance metric
2 * smartWordCapture + // double weight for word capture
prefixPriority +
smartNumberMatch,
smartNumberMatch +
5 * popularityBonus,
title: title,
result: result,
};
Expand Down
2 changes: 1 addition & 1 deletion src/data/course_name_table.json

Large diffs are not rendered by default.

33 changes: 30 additions & 3 deletions src/scripts/generateCourseNameTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface ProfessorData {

interface SectionData {
professors: ProfessorData[];
total_students?: number;
}

interface AcademicSessionData {
Expand All @@ -33,12 +34,18 @@ interface PrefixData {
// tell compiler that aggregatedData DOES have data member
const aggregatedData = aggregatedDataRaw as { data: PrefixData[] };

export type TableType = { [key: string]: SearchQuery[] };
export type TableEntry = SearchQuery & { totalStudents: number };
export type TableType = { [key: string]: TableEntry[] };

const table: TableType = {};

function addCourse(title: string, prefix: string, number: string) {
const courseObject: SearchQuery = { prefix, number };
function addCourse(
title: string,
prefix: string,
number: string,
totalStudents: number,
) {
const courseObject: TableEntry = { prefix, number, totalStudents };

if (!Object.prototype.hasOwnProperty.call(table, title)) {
table[title] = [courseObject];
Expand All @@ -57,10 +64,30 @@ for (let prefixItr = 0; prefixItr < aggregatedData.data.length; prefixItr++) {
) {
const courseNumberData = prefixData.course_numbers[courseNumberItr];
if (!courseNumberData) continue; //handle blank data
let totalStudents = 0;
for (
let academicSessionItr = 0;
academicSessionItr < courseNumberData.academic_sessions.length;
academicSessionItr++
) {
const academicSessionData =
courseNumberData.academic_sessions[academicSessionItr];
if (!academicSessionData) continue;
for (
let sectionItr = 0;
sectionItr < academicSessionData.sections.length;
sectionItr++
) {
const sectionData = academicSessionData.sections[sectionItr];
if (!sectionData) continue;
totalStudents += sectionData.total_students ?? 0;
}
}
addCourse(
courseNumberData.title,
prefixData.subject_prefix,
courseNumberData.course_number,
totalStudents,
);
}
}
Expand Down
Loading