Datasets:

Modalities:
Text
Formats:
json
ArXiv:
Libraries:
Datasets
pandas
License:
fzkuji commited on
Commit
c1e0945
·
verified ·
1 Parent(s): 80e527a

Upload 2 files

Browse files
Files changed (2) hide show
  1. cmexam_dataloading.ipynb +152 -0
  2. cmexam_preprocessing.ipynb +78 -78
cmexam_dataloading.ipynb ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "metadata": {},
5
+ "cell_type": "markdown",
6
+ "source": "生成答案是原始文本的Multiple Choice的prompt(考虑了多种语言的格式,使用4options)",
7
+ "id": "f205851d8d2c3a64"
8
+ },
9
+ {
10
+ "metadata": {
11
+ "ExecuteTime": {
12
+ "end_time": "2024-09-09T10:57:51.564790Z",
13
+ "start_time": "2024-09-09T10:57:42.760548Z"
14
+ }
15
+ },
16
+ "cell_type": "code",
17
+ "source": [
18
+ "from datasets import load_dataset\n",
19
+ "import os\n",
20
+ "import json\n",
21
+ "\n",
22
+ "# Load the dataset\n",
23
+ "dataset = load_dataset(\"fzkuji/cmexam\", trust_remote_code=True)\n",
24
+ "\n",
25
+ "# Define the save path\n",
26
+ "save_path = f\"./data/llama-factory\" # Change this path to your local directory\n",
27
+ "os.makedirs(save_path, exist_ok=True)\n",
28
+ "\n",
29
+ "# Function to save data as JSON with specified columns\n",
30
+ "def save_as_json(data, filename):\n",
31
+ " file_path = os.path.join(save_path, filename)\n",
32
+ " \n",
33
+ " data_to_save = []\n",
34
+ " for item in data:\n",
35
+ " # Extract the option texts and generate option letters dynamically\n",
36
+ " option_texts = []\n",
37
+ " option_letters = []\n",
38
+ " for idx, option in enumerate(item['Options']):\n",
39
+ " # Handle different data structures for options\n",
40
+ " if isinstance(option, dict):\n",
41
+ " option_text = option.get('value', '')\n",
42
+ " else:\n",
43
+ " option_text = str(option)\n",
44
+ " option_texts.append(option_text)\n",
45
+ " option_letters.append(chr(65 + idx)) # Generate letters 'A', 'B', etc.\n",
46
+ " \n",
47
+ " # Create a mapping for the current item\n",
48
+ " dict_num = {letter: idx for idx, letter in enumerate(option_letters)}\n",
49
+ " \n",
50
+ " # Process the answer keys, assuming Answer is a single string of concatenated letters (e.g., \"ABC\")\n",
51
+ " answer_keys = item['Answer']\n",
52
+ " if isinstance(answer_keys, str):\n",
53
+ " # Split the answer string into individual letters (e.g., \"ABC\" -> ['A', 'B', 'C'])\n",
54
+ " answer_keys = list(answer_keys)\n",
55
+ " elif isinstance(answer_keys, list):\n",
56
+ " pass # Already a list\n",
57
+ " elif answer_keys is None:\n",
58
+ " print(f\"Warning: Answer is None for item '{item['Question']}', skipping...\")\n",
59
+ " continue # Skip items with no answer\n",
60
+ " else:\n",
61
+ " # Skip if 'Answer' is not a string or list\n",
62
+ " print(f\"Warning: Unexpected type for 'Answer' in item '{item['Question']}': {type(answer_keys)}\")\n",
63
+ " continue\n",
64
+ " \n",
65
+ " # Map the answer keys to the actual option texts, but skip keys that are out of range\n",
66
+ " answer_text = []\n",
67
+ " for ans in answer_keys:\n",
68
+ " if ans in dict_num:\n",
69
+ " idx = dict_num[ans]\n",
70
+ " answer_text.append(option_texts[idx])\n",
71
+ " else:\n",
72
+ " print(f\"Warning: Answer '{ans}' not found in options for item '{item['Question']}'\")\n",
73
+ " \n",
74
+ " # Construct the input text\n",
75
+ " input_text = f\"问题:{item['Question']}\\n选项:\\n\" + \"\\n\".join(\n",
76
+ " [f\"\\t{letter}. {text}。\" for letter, text in zip(option_letters, option_texts)]\n",
77
+ " )\n",
78
+ " \n",
79
+ " # Add the instruction, input, and output to the data\n",
80
+ " if answer_text:\n",
81
+ " data_to_save.append({\n",
82
+ " \"instruction\": \"假设您是一名医生,请回答以下选择题。请您输出答案的文本内容(不包含选项序号)。\",\n",
83
+ " \"input\": input_text,\n",
84
+ " \"output\": \",\".join(answer_text) # Join multiple answers with commas\n",
85
+ " })\n",
86
+ " else:\n",
87
+ " print(f\"Warning: No valid answers for item '{item['Question']}', skipping...\")\n",
88
+ " \n",
89
+ " # Write the modified data to a JSON file\n",
90
+ " with open(file_path, 'w', encoding='utf-8') as f:\n",
91
+ " json.dump(data_to_save, f, ensure_ascii=False, indent=4)\n",
92
+ "\n",
93
+ "# Save the modified data for train, validation, and test splits\n",
94
+ "save_as_json(dataset['train'], 'train.json')\n",
95
+ "save_as_json(dataset['validation'], 'validation.json')\n",
96
+ "save_as_json(dataset['test'], 'test.json')\n"
97
+ ],
98
+ "id": "54192ba87c09ac2b",
99
+ "outputs": [
100
+ {
101
+ "name": "stdout",
102
+ "output_type": "stream",
103
+ "text": [
104
+ "Warning: Answer 'E' not found in options for item '不属于阿片类镇痛药的是'\n",
105
+ "Warning: No valid answers for item '不属于阿片类镇痛药的是', skipping...\n",
106
+ "Warning: Answer 'E' not found in options for item '结构中含有��个手性碳原子,有四个异构体的药物是'\n",
107
+ "Warning: No valid answers for item '结构中含有两个手性碳原子,有四个异构体的药物是', skipping...\n",
108
+ "Warning: Answer 'E' not found in options for item '将苯丙氨酸引入氮芥结构中得到美法伦的目的是'\n",
109
+ "Warning: No valid answers for item '将苯丙氨酸引入氮芥结构中得到美法伦的目的是', skipping...\n",
110
+ "Warning: Answer 'E' not found in options for item '化学名为4-(2-氨基乙基)-1、2-苯二酚盐酸盐的药物是'\n",
111
+ "Warning: No valid answers for item '化学名为4-(2-氨基乙基)-1、2-苯二酚盐酸盐的药物是', skipping...\n",
112
+ "Warning: Answer 'E' not found in options for item '非处方药的使用要求'\n",
113
+ "Warning: Answer 'E' not found in options for item '面对患者,药学服务的重要人群有'\n",
114
+ "Warning: Answer 'E' not found in options for item '针刺用毫针的常用消毒方法有'\n",
115
+ "Warning: Answer is None for item '由国家制定,各省可根据当地经济水平、医疗需求和用药习惯适当进行调整的是', skipping...\n",
116
+ "Warning: Answer 'E' not found in options for item '分子中含有季铵结构,中枢作用较弱的药物是'\n",
117
+ "Warning: No valid answers for item '分子中含有季铵结构,中枢作用较弱的药物是', skipping...\n",
118
+ "Warning: Answer 'E' not found in options for item '在体内经代谢后,其代谢产物具有活性的药物是'\n"
119
+ ]
120
+ }
121
+ ],
122
+ "execution_count": 1
123
+ },
124
+ {
125
+ "metadata": {},
126
+ "cell_type": "markdown",
127
+ "source": "备注,有Warning的原因是有些题目的答案为空,或者选项不完整。这些题目会被跳过,不会被保存到JSON文件中。",
128
+ "id": "c94e6ffe4fd9be8d"
129
+ }
130
+ ],
131
+ "metadata": {
132
+ "kernelspec": {
133
+ "display_name": "Python 3",
134
+ "language": "python",
135
+ "name": "python3"
136
+ },
137
+ "language_info": {
138
+ "codemirror_mode": {
139
+ "name": "ipython",
140
+ "version": 2
141
+ },
142
+ "file_extension": ".py",
143
+ "mimetype": "text/x-python",
144
+ "name": "python",
145
+ "nbconvert_exporter": "python",
146
+ "pygments_lexer": "ipython2",
147
+ "version": "2.7.6"
148
+ }
149
+ },
150
+ "nbformat": 4,
151
+ "nbformat_minor": 5
152
+ }
cmexam_preprocessing.ipynb CHANGED
@@ -1,78 +1,78 @@
1
- {
2
- "cells": [
3
- {
4
- "metadata": {
5
- "ExecuteTime": {
6
- "end_time": "2024-09-08T15:26:59.355018Z",
7
- "start_time": "2024-09-08T15:26:57.990909Z"
8
- }
9
- },
10
- "cell_type": "code",
11
- "source": [
12
- "import pandas as pd\n",
13
- "\n",
14
- "# 定义一个函数,将 Options 字符串转换为 key-value 格式的列表\n",
15
- "def format_options(options_str):\n",
16
- " options_list = []\n",
17
- " # 按行分割选项\n",
18
- " options_lines = options_str.split(\"\\n\")\n",
19
- " for line in options_lines:\n",
20
- " if len(line) > 1:\n",
21
- " key = line[0] # 第一个字符为选项字母\n",
22
- " value = line[2:].strip() # 从第三个字符开始为选项内容\n",
23
- " options_list.append({\"key\": key, \"value\": value})\n",
24
- " return options_list\n",
25
- "\n",
26
- "# 读取 CSV 文件\n",
27
- "train_data = pd.read_csv('./data/train.csv', encoding='utf-8')\n",
28
- "valid_data = pd.read_csv('./data/val.csv', encoding='utf-8')\n",
29
- "test_data = pd.read_csv('./data/test_with_annotations.csv', encoding='utf-8')\n",
30
- "\n",
31
- "# 将json数据只保留与train和valid一致的字段\n",
32
- "test_data = test_data[['Question', 'Options', 'Answer', 'Explanation']]\n",
33
- "\n",
34
- "# 遍历数据集,将每个样本的 Options 列格式化\n",
35
- "train_data['Options'] = train_data['Options'].apply(format_options)\n",
36
- "valid_data['Options'] = valid_data['Options'].apply(format_options)\n",
37
- "test_data['Options'] = test_data['Options'].apply(format_options)\n",
38
- "\n",
39
- "# 将修改后的 DataFrame 保存为 JSON 文件\n",
40
- "train_data.to_json('./data/train.json', orient='records', lines=True, force_ascii=False)\n",
41
- "valid_data.to_json('./data/valid.json', orient='records', lines=True, force_ascii=False)\n",
42
- "test_data.to_json('./data/test.json', orient='records', lines=True, force_ascii=False)\n"
43
- ],
44
- "id": "c003560dea95e12e",
45
- "outputs": [],
46
- "execution_count": 5
47
- },
48
- {
49
- "metadata": {},
50
- "cell_type": "code",
51
- "outputs": [],
52
- "execution_count": null,
53
- "source": "",
54
- "id": "8d142ed01196946"
55
- }
56
- ],
57
- "metadata": {
58
- "kernelspec": {
59
- "display_name": "Python 3",
60
- "language": "python",
61
- "name": "python3"
62
- },
63
- "language_info": {
64
- "codemirror_mode": {
65
- "name": "ipython",
66
- "version": 2
67
- },
68
- "file_extension": ".py",
69
- "mimetype": "text/x-python",
70
- "name": "python",
71
- "nbconvert_exporter": "python",
72
- "pygments_lexer": "ipython2",
73
- "version": "2.7.6"
74
- }
75
- },
76
- "nbformat": 4,
77
- "nbformat_minor": 5
78
- }
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "metadata": {
5
+ "ExecuteTime": {
6
+ "end_time": "2024-09-08T15:26:59.355018Z",
7
+ "start_time": "2024-09-08T15:26:57.990909Z"
8
+ }
9
+ },
10
+ "cell_type": "code",
11
+ "source": [
12
+ "import pandas as pd\n",
13
+ "\n",
14
+ "# 定义一个函数,将 Options 字符串转换为 key-value 格式的列表\n",
15
+ "def format_options(options_str):\n",
16
+ " options_list = []\n",
17
+ " # 按行分割选项\n",
18
+ " options_lines = options_str.split(\"\\n\")\n",
19
+ " for line in options_lines:\n",
20
+ " if len(line) > 1:\n",
21
+ " key = line[0] # 第一个字符为选项字母\n",
22
+ " value = line[2:].strip() # 从第三个字符开始为选项内容\n",
23
+ " options_list.append({\"key\": key, \"value\": value})\n",
24
+ " return options_list\n",
25
+ "\n",
26
+ "# 读取 CSV 文件\n",
27
+ "train_data = pd.read_csv('./data/train.csv', encoding='utf-8')\n",
28
+ "valid_data = pd.read_csv('./data/val.csv', encoding='utf-8')\n",
29
+ "test_data = pd.read_csv('./data/test_with_annotations.csv', encoding='utf-8')\n",
30
+ "\n",
31
+ "# 将json数据只保留与train和valid一致的字段\n",
32
+ "test_data = test_data[['Question', 'Options', 'Answer', 'Explanation']]\n",
33
+ "\n",
34
+ "# 遍历数据集,将每个样本的 Options 列格式化\n",
35
+ "train_data['Options'] = train_data['Options'].apply(format_options)\n",
36
+ "valid_data['Options'] = valid_data['Options'].apply(format_options)\n",
37
+ "test_data['Options'] = test_data['Options'].apply(format_options)\n",
38
+ "\n",
39
+ "# 将修改后的 DataFrame 保存为 JSON 文件\n",
40
+ "train_data.to_json('./data/train.json', orient='records', lines=True, force_ascii=False)\n",
41
+ "valid_data.to_json('./data/valid.json', orient='records', lines=True, force_ascii=False)\n",
42
+ "test_data.to_json('./data/test.json', orient='records', lines=True, force_ascii=False)\n"
43
+ ],
44
+ "id": "c003560dea95e12e",
45
+ "outputs": [],
46
+ "execution_count": 5
47
+ },
48
+ {
49
+ "metadata": {},
50
+ "cell_type": "code",
51
+ "outputs": [],
52
+ "execution_count": null,
53
+ "source": "",
54
+ "id": "8d142ed01196946"
55
+ }
56
+ ],
57
+ "metadata": {
58
+ "kernelspec": {
59
+ "display_name": "Python 3",
60
+ "language": "python",
61
+ "name": "python3"
62
+ },
63
+ "language_info": {
64
+ "codemirror_mode": {
65
+ "name": "ipython",
66
+ "version": 2
67
+ },
68
+ "file_extension": ".py",
69
+ "mimetype": "text/x-python",
70
+ "name": "python",
71
+ "nbconvert_exporter": "python",
72
+ "pygments_lexer": "ipython2",
73
+ "version": "2.7.6"
74
+ }
75
+ },
76
+ "nbformat": 4,
77
+ "nbformat_minor": 5
78
+ }