File size: 1,997 Bytes
d0b115f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import pandas as pd
from pathlib import Path
from string import ascii_lowercase

def convert_csv_to_markdown(csv_path: Path) -> str:
    # Load the CSV file without using the first row as headers
    df = pd.read_csv(csv_path, header=None)

    # Manually set column names
    df.columns = ["Type", "Group", "Points", "Question", "Correct Answer",
                 "Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5",
                 "Page", "Taxonomy"]

    # Generate Markdown output
    markdown_output = "## Generated Questions\n\n"

    for index, row in df.iterrows():
        question = row["Question"]
        correct_index = row["Correct Answer"]

        # Skip rows where the correct answer index is missing
        if pd.isna(correct_index):
            continue

        correct_index = int(correct_index) - 1  # Convert 1-based index to 0-based
        choices = row[["Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5"]].dropna().tolist()
        metadata = row[["Page", "Taxonomy"]].dropna().tolist()

        markdown_output += f"{index+1}. {question}<br>"

        for i, choice in enumerate(choices):
            letter = ascii_lowercase[i]  # Get letter a, b, c, etc.
            # Indent choices and bold the correct answer
            if i == correct_index:
                markdown_output += f"&emsp;&emsp;{letter}. **{choice}**<br>"
            else:
                markdown_output += f"&emsp;&emsp;{letter}. {choice}<br>"
        # Add metadata as additional choices, with modified page format
        for meta in metadata:
            if meta == row["Page"]:  # If this metadata item is the page
                # Remove first character and prepend "Pages"
                page_number = meta[1:]  # Remove first character
                markdown_output += f"   - Pages {page_number}<br>"
            else:
                markdown_output += f"   - {meta}<br><br>"

        markdown_output += "<br>"  # Space between questions

    return markdown_output