hac541309 commited on
Commit
cddd0d5
1 Parent(s): c10e786

Create create_dict.py

Browse files
Files changed (1) hide show
  1. create_dict.py +50 -0
create_dict.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import os
3
+ import datasets
4
+
5
+ XLS_FOLDER = "stdict"
6
+ OUTPUT_FOLDER = "data"
7
+ SORT_KEY = "어휘"
8
+ NUM_PROC = 32
9
+ hf_access_token = ""
10
+ hf_ID = ""
11
+ ds_name = "stdict_kor"
12
+
13
+
14
+ def flatten_examples(example: dict) -> dict:
15
+ text_line = ""
16
+ for key in example:
17
+ # some columns are empty or invalid
18
+ if key == "의미 번호":
19
+ continue
20
+ if (single_column := example[key]) == None:
21
+ continue
22
+ # certain columns contain extraneous content
23
+ if key == "원어·어종":
24
+ single_column = single_column.removeprefix("안 밝힘 ")
25
+ text_line += key + ": " + single_column.strip() + ", "
26
+ return {"text": text_line.removesuffix(", ")}
27
+
28
+
29
+ if os.path.exists(XLS_FOLDER):
30
+ XLS_FOLDER = os.path.abspath(XLS_FOLDER)
31
+ xls_list = os.listdir(XLS_FOLDER)
32
+ else:
33
+ raise ValueError("input folder does not exist")
34
+
35
+ combined_df = pd.DataFrame()
36
+ length_check = 0
37
+ for xls in sorted(xls_list):
38
+ xls_path = os.path.join(XLS_FOLDER, xls)
39
+ if os.path.exists(xls_path):
40
+ # print(xls_path)
41
+ df = pd.read_excel(xls_path, header=0, index_col=None)
42
+ length_check += len(df)
43
+ combined_df = pd.concat([combined_df, df], ignore_index=True)
44
+
45
+ assert len(combined_df) == length_check
46
+ ds = datasets.Dataset.from_pandas(combined_df)
47
+ sorted_ds = ds.sort(SORT_KEY)
48
+ processed_ds = sorted_ds.map(flatten_examples, num_proc=NUM_PROC).select_columns("text")
49
+
50
+ processed_ds.push_to_hub(repo_id=ds_name, token=hf_access_token)