Datasets:

Modalities:
Text
Formats:
json
ArXiv:
Libraries:
Datasets
pandas
License:
File size: 7,267 Bytes
c1e0945
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "生成答案是原始文本的Multiple Choice的prompt(考虑了多种语言的格式,使用4options)",
   "id": "f205851d8d2c3a64"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-09T10:57:51.564790Z",
     "start_time": "2024-09-09T10:57:42.760548Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from datasets import load_dataset\n",
    "import os\n",
    "import json\n",
    "\n",
    "# Load the dataset\n",
    "dataset = load_dataset(\"fzkuji/cmexam\", trust_remote_code=True)\n",
    "\n",
    "# Define the save path\n",
    "save_path = f\"./data/llama-factory\"  # Change this path to your local directory\n",
    "os.makedirs(save_path, exist_ok=True)\n",
    "\n",
    "# Function to save data as JSON with specified columns\n",
    "def save_as_json(data, filename):\n",
    "    file_path = os.path.join(save_path, filename)\n",
    "    \n",
    "    data_to_save = []\n",
    "    for item in data:\n",
    "        # Extract the option texts and generate option letters dynamically\n",
    "        option_texts = []\n",
    "        option_letters = []\n",
    "        for idx, option in enumerate(item['Options']):\n",
    "            # Handle different data structures for options\n",
    "            if isinstance(option, dict):\n",
    "                option_text = option.get('value', '')\n",
    "            else:\n",
    "                option_text = str(option)\n",
    "            option_texts.append(option_text)\n",
    "            option_letters.append(chr(65 + idx))  # Generate letters 'A', 'B', etc.\n",
    "        \n",
    "        # Create a mapping for the current item\n",
    "        dict_num = {letter: idx for idx, letter in enumerate(option_letters)}\n",
    "        \n",
    "        # Process the answer keys, assuming Answer is a single string of concatenated letters (e.g., \"ABC\")\n",
    "        answer_keys = item['Answer']\n",
    "        if isinstance(answer_keys, str):\n",
    "            # Split the answer string into individual letters (e.g., \"ABC\" -> ['A', 'B', 'C'])\n",
    "            answer_keys = list(answer_keys)\n",
    "        elif isinstance(answer_keys, list):\n",
    "            pass  # Already a list\n",
    "        elif answer_keys is None:\n",
    "            print(f\"Warning: Answer is None for item '{item['Question']}', skipping...\")\n",
    "            continue  # Skip items with no answer\n",
    "        else:\n",
    "            # Skip if 'Answer' is not a string or list\n",
    "            print(f\"Warning: Unexpected type for 'Answer' in item '{item['Question']}': {type(answer_keys)}\")\n",
    "            continue\n",
    "        \n",
    "        # Map the answer keys to the actual option texts, but skip keys that are out of range\n",
    "        answer_text = []\n",
    "        for ans in answer_keys:\n",
    "            if ans in dict_num:\n",
    "                idx = dict_num[ans]\n",
    "                answer_text.append(option_texts[idx])\n",
    "            else:\n",
    "                print(f\"Warning: Answer '{ans}' not found in options for item '{item['Question']}'\")\n",
    "        \n",
    "        # Construct the input text\n",
    "        input_text = f\"问题:{item['Question']}\\n选项:\\n\" + \"\\n\".join(\n",
    "            [f\"\\t{letter}. {text}。\" for letter, text in zip(option_letters, option_texts)]\n",
    "        )\n",
    "        \n",
    "        # Add the instruction, input, and output to the data\n",
    "        if answer_text:\n",
    "            data_to_save.append({\n",
    "                \"instruction\": \"假设您是一名医生,请回答以下选择题。请您输出答案的文本内容(不包含选项序号)。\",\n",
    "                \"input\": input_text,\n",
    "                \"output\": \"\".join(answer_text)  # Join multiple answers with commas\n",
    "            })\n",
    "        else:\n",
    "            print(f\"Warning: No valid answers for item '{item['Question']}', skipping...\")\n",
    "    \n",
    "    # Write the modified data to a JSON file\n",
    "    with open(file_path, 'w', encoding='utf-8') as f:\n",
    "        json.dump(data_to_save, f, ensure_ascii=False, indent=4)\n",
    "\n",
    "# Save the modified data for train, validation, and test splits\n",
    "save_as_json(dataset['train'], 'train.json')\n",
    "save_as_json(dataset['validation'], 'validation.json')\n",
    "save_as_json(dataset['test'], 'test.json')\n"
   ],
   "id": "54192ba87c09ac2b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Warning: Answer 'E' not found in options for item '不属于阿片类镇痛药的是'\n",
      "Warning: No valid answers for item '不属于阿片类镇痛药的是', skipping...\n",
      "Warning: Answer 'E' not found in options for item '结构中含有两个手性碳原子,有四个异构体的药物是'\n",
      "Warning: No valid answers for item '结构中含有两个手性碳原子,有四个异构体的药物是', skipping...\n",
      "Warning: Answer 'E' not found in options for item '将苯丙氨酸引入氮芥结构中得到美法伦的目的是'\n",
      "Warning: No valid answers for item '将苯丙氨酸引入氮芥结构中得到美法伦的目的是', skipping...\n",
      "Warning: Answer 'E' not found in options for item '化学名为4-(2-氨基乙基)-1、2-苯二酚盐酸盐的药物是'\n",
      "Warning: No valid answers for item '化学名为4-(2-氨基乙基)-1、2-苯二酚盐酸盐的药物是', skipping...\n",
      "Warning: Answer 'E' not found in options for item '非处方药的使用要求'\n",
      "Warning: Answer 'E' not found in options for item '面对患者,药学服务的重要人群有'\n",
      "Warning: Answer 'E' not found in options for item '针刺用毫针的常用消毒方法有'\n",
      "Warning: Answer is None for item '由国家制定,各省可根据当地经济水平、医疗需求和用药习惯适当进行调整的是', skipping...\n",
      "Warning: Answer 'E' not found in options for item '分子中含有季铵结构,中枢作用较弱的药物是'\n",
      "Warning: No valid answers for item '分子中含有季铵结构,中枢作用较弱的药物是', skipping...\n",
      "Warning: Answer 'E' not found in options for item '在体内经代谢后,其代谢产物具有活性的药物是'\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "备注,有Warning的原因是有些题目的答案为空,或者选项不完整。这些题目会被跳过,不会被保存到JSON文件中。",
   "id": "c94e6ffe4fd9be8d"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}