stdict_kor / create_dict.py
hac541309's picture
Create create_dict.py
cddd0d5
import pandas as pd
import os
import datasets
XLS_FOLDER = "stdict"
OUTPUT_FOLDER = "data"
SORT_KEY = "어휘"
NUM_PROC = 32
hf_access_token = ""
hf_ID = ""
ds_name = "stdict_kor"
def flatten_examples(example: dict) -> dict:
text_line = ""
for key in example:
# some columns are empty or invalid
if key == "의미 번호":
continue
if (single_column := example[key]) == None:
continue
# certain columns contain extraneous content
if key == "원어·어종":
single_column = single_column.removeprefix("안 밝힘 ")
text_line += key + ": " + single_column.strip() + ", "
return {"text": text_line.removesuffix(", ")}
if os.path.exists(XLS_FOLDER):
XLS_FOLDER = os.path.abspath(XLS_FOLDER)
xls_list = os.listdir(XLS_FOLDER)
else:
raise ValueError("input folder does not exist")
combined_df = pd.DataFrame()
length_check = 0
for xls in sorted(xls_list):
xls_path = os.path.join(XLS_FOLDER, xls)
if os.path.exists(xls_path):
# print(xls_path)
df = pd.read_excel(xls_path, header=0, index_col=None)
length_check += len(df)
combined_df = pd.concat([combined_df, df], ignore_index=True)
assert len(combined_df) == length_check
ds = datasets.Dataset.from_pandas(combined_df)
sorted_ds = ds.sort(SORT_KEY)
processed_ds = sorted_ds.map(flatten_examples, num_proc=NUM_PROC).select_columns("text")
processed_ds.push_to_hub(repo_id=ds_name, token=hf_access_token)