-
Notifications
You must be signed in to change notification settings - Fork 74
(EAI-1257) handle multiple correct and more answer options #887
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f1ffc82
27f65ee
ba05cee
3712018
cacf923
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,36 +2,91 @@ import fs from "fs"; | |
import path from "path"; | ||
import csv from "csv-parser"; | ||
import { QuizQuestionData, QuizQuestionDataSchema } from "../QuizQuestionData"; | ||
import { makeTags } from "./makeTags"; | ||
|
||
const testDataPath = path.resolve(__dirname, "..", "..", "..", "testData"); | ||
const csvFileInPath = path.resolve(testDataPath, "badge-questions.csv"); | ||
const jsonFileOutPath = path.resolve(testDataPath, "badge-questions.json"); | ||
|
||
const handleAnswers = (row: any) => { | ||
const correctAnswers = row.Answer.trim()?.split("") || []; | ||
const answers = ["A", "B", "C", "D", "E", "F"] | ||
.map((label) => { | ||
const isCorrect = correctAnswers.includes(label); | ||
return { | ||
answer: row[label], | ||
isCorrect, | ||
label, | ||
}; | ||
}) | ||
.filter((answer) => answer.answer && answer.answer.trim() !== ""); // Remove empty answers | ||
Comment on lines
+12
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think this code could be a little cleaner. rather than filtering at the end, can you slice the array of ["A", "B", "C", ...] to the correct length before running the map over it? |
||
return answers; | ||
}; | ||
|
||
// const createTagsFromAssessmentName = (assessmentName: string) => { | ||
// return assessmentName.split(",").map((tag) => tag.trim()); | ||
// }; | ||
|
||
const assessmentNameToTagsMap = { | ||
'MongoDB Aggregation Fundamentals': ['aggregation'], | ||
'MongoDB Query Optimization Techniques': ['query'], | ||
"From Relational Model (SQL) to MongoDB's Document Model": ['data_modeling'], | ||
'MongoDB Schema Design Patterns and Antipatterns': ['data_modeling'], | ||
'MongoDB Advanced Schema Design Patterns and Antipatterns': ['data_modeling'], | ||
'MongoDB Schema Design Optimization': ['data_modeling'], | ||
'Building AI Agents with MongoDB': ['gen_ai'], | ||
'Building AI-Powered Search with MongoDB Vector Search': ['gen_ai'], | ||
'Building RAG Apps Using MongoDB': ['gen_ai'], | ||
'MongoDB Indexing Design Fundamentals': ['indexing'], | ||
'Monitoring MongoDB with Built-in Tools': ['monitoring_tuning_and_automation'], | ||
'Optimizing MongoDB Performance with Tuning Tools': ['monitoring_tuning_and_automation'], | ||
'CRUD Operations in MongoDB': ['query'], | ||
'Search with MongoDB': ['search'], | ||
'Securing MongoDB Atlas: Authentication & Authorization': ['security'], | ||
'Securing MongoDB Self-Managed: Authentication & Authorization': ['security'], | ||
'MongoDB Sharding Strategies': ['sharding'], | ||
'Optimizing and Maintaining MongoDB Cluster Reliability': ['performance_at_scale'], | ||
}; | ||
|
||
// excluded: | ||
// 'MongoDB Overview: Core Concepts and Architecture' | ||
|
||
const parseCSV = async (filePath: string): Promise<QuizQuestionData[]> => { | ||
return new Promise((resolve, reject) => { | ||
const results: QuizQuestionData[] = []; | ||
const assessments = new Set<string>(); | ||
fs.createReadStream(filePath) | ||
.pipe(csv()) | ||
.on("data", (row) => { | ||
// console.log("HIT TRY"); | ||
try { | ||
const answers = ["A", "B", "C", "D"].map((label, index) => ({ | ||
answer: row[label], | ||
isCorrect: row.Answer === (index + 1).toString(), | ||
label, | ||
})); | ||
|
||
const assessmentName = row["Assessment"]?.trim(); | ||
if (!assessmentName) { | ||
console.warn("Skipping row with missing assessment name"); | ||
return; | ||
} | ||
|
||
// Type guard to ensure assessmentName is a valid key | ||
if (assessmentName in assessmentNameToTagsMap) { | ||
console.log('>> tags', assessmentNameToTagsMap[assessmentName as keyof typeof assessmentNameToTagsMap]); | ||
} else { | ||
console.warn(`Assessment name not found in map: "${assessmentName}"`); | ||
} | ||
|
||
const answers = handleAnswers(row); | ||
const questionData: QuizQuestionData = QuizQuestionDataSchema.parse({ | ||
questionText: row["Question Text"], | ||
title: row["Assessment"], | ||
title: assessmentName, | ||
topicType: "badge", // Defaulting topic type | ||
questionType: "singleCorrect", // Assuming single correct answer | ||
questionType: | ||
row["Answer"].length > 1 ? "multipleCorrect" : "singleCorrect", | ||
Comment on lines
+80
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consider doing more typesafe parsing rather than checking string length (which isn't very resilient). for example, there could be an err in the spreadsheet |
||
answers, | ||
explanation: row["Reference"], | ||
tags: row["tags"] ? row["tags"].split(",") : [], | ||
// tags: row["tags"] ? row["tags"].split(",") : [], | ||
tags: assessmentName in assessmentNameToTagsMap | ||
? assessmentNameToTagsMap[assessmentName as keyof typeof assessmentNameToTagsMap] | ||
: [], | ||
}); | ||
questionData.tags = makeTags(questionData); | ||
results.push(questionData); | ||
// console.log(">>>> assessments >>>>", assessments); | ||
} catch (error) { | ||
console.error("Validation error:", error); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of passing
row: any
, please make sure that the input is strongly typed. you can use zod to do any validation that you need.