Spaces:
Sleeping
Sleeping
import os | |
import pandas as pd | |
import json | |
from pathlib import Path | |
from openpyxl import workbook | |
current_file_path = Path(__file__).resolve() | |
project_root = current_file_path.parents[1] | |
assets_dir = Path(project_root) / 'asset' | |
database_dir = Path(project_root) / "data" | |
# datafile = pd.read_csv(database_dir / "database.csv") | |
def data_cleaning(datafile): | |
df = datafile.rename(columns={"id": "level", "action_text": "feedback", "next_id": "next"}) | |
df2 = df[["level", "question_text", "options", "feedback", "next"]] | |
df2 = df2.dropna(axis=1, how='all') | |
df2 = df2.dropna(axis=0, how='any') | |
return df2 | |
def option_maker(df_to_option): | |
# تبدیل به فرمت JSON | |
json_data = {} | |
for index, row in df_to_option.iterrows(): | |
level = row['level'] | |
option = row['options'] | |
feedback = row['feedback'] | |
next_id = row['next'] | |
if level not in json_data: | |
json_data[level] = {} | |
json_data[level][option] = {"feedback": feedback, "next": next_id} | |
with open(Path(database_dir)/'option_data.json', 'w', encoding="utf-8") as json_file: | |
json.dump(json_data, json_file, ensure_ascii=False, indent=4) | |
def quiz_maker(df_to_quiz): | |
quiz_data = {} | |
for index_q, row_q in df_to_quiz.iterrows(): | |
level_q = row_q['level'] | |
question_text = row_q['question_text'] | |
if level_q not in quiz_data: | |
quiz_data[level_q] = {} | |
quiz_data[level_q] = [question_text] | |
with open(Path(database_dir)/'question_data.json', 'w', encoding="utf-8") as json_file: | |
json.dump(quiz_data, json_file, ensure_ascii=False, indent=4) |