File size: 6,498 Bytes
2eacb8b |
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "882ae3ed",
"metadata": {},
"outputs": [],
"source": [
"import datasets"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f6b5f6bf",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Reusing dataset reuters21578 (/Users/juliensimon/.cache/huggingface/datasets/reuters21578/ModHayes/1.0.0/bd91fac5a25fc818873c02a7281cc276c9b326a9e6a89288fc6ba6967772240f)\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d47dfac1e7e54f87bcf922b4616bfc9b",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/2 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from datasets import load_dataset\n",
"\n",
"dataset = load_dataset(\"reuters21578\", 'ModHayes')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "252eaf4a",
"metadata": {},
"outputs": [],
"source": [
"dataset.save_to_disk('reuters_original')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "af85b023",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DatasetDict({\n",
" test: Dataset({\n",
" features: ['text', 'text_type', 'topics', 'lewis_split', 'cgis_split', 'old_id', 'new_id', 'places', 'people', 'orgs', 'exchanges', 'date', 'title'],\n",
" num_rows: 722\n",
" })\n",
" train: Dataset({\n",
" features: ['text', 'text_type', 'topics', 'lewis_split', 'cgis_split', 'old_id', 'new_id', 'places', 'people', 'orgs', 'exchanges', 'date', 'title'],\n",
" num_rows: 20856\n",
" })\n",
"})"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dataset"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "0af0d4b3",
"metadata": {},
"outputs": [],
"source": [
"dataset = dataset.remove_columns(\n",
" ['text_type', 'topics', 'lewis_split', 'cgis_split', 'old_id',\n",
" 'new_id','places', 'people', 'orgs', 'exchanges', 'date'])\n",
"\n",
"dataset = dataset.rename_column('title', 'target') "
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "3a97954e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'text': 'Standard Oil Co and BP North America\\nInc said they plan to form a venture to manage the money market\\nborrowing and investment activities of both companies.\\n BP North America is a subsidiary of British Petroleum Co\\nPlc <BP>, which also owns a 55 pct interest in Standard Oil.\\n The venture will be called BP/Standard Financial Trading\\nand will be operated by Standard Oil under the oversight of a\\njoint management committee.\\n\\n Reuter\\n',\n",
" 'target': 'STANDARD OIL <SRD> TO FORM FINANCIAL UNIT'}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dataset['train'][1]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "6947b4a0",
"metadata": {},
"outputs": [],
"source": [
"def clean(row):\n",
" row['text'] = row['text'].replace('\\n',' ').replace('\\t',' ')\\\n",
" .replace(',','').replace('\\'','').replace('\\\"','')\\\n",
" .replace(' Reuter','').replace(' REUTER','')\n",
" row['text'] = \" \".join(row['text'].split())\n",
" row['target'] = row['target'].replace('<','<').replace('>','>')\n",
" return row"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "deec4da9",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Loading cached processed dataset at /Users/juliensimon/.cache/huggingface/datasets/reuters21578/ModHayes/1.0.0/bd91fac5a25fc818873c02a7281cc276c9b326a9e6a89288fc6ba6967772240f/cache-80b0dc9c8071ba93.arrow\n",
"Loading cached processed dataset at /Users/juliensimon/.cache/huggingface/datasets/reuters21578/ModHayes/1.0.0/bd91fac5a25fc818873c02a7281cc276c9b326a9e6a89288fc6ba6967772240f/cache-38a3b39f977c1f02.arrow\n"
]
}
],
"source": [
"dataset = dataset.map(clean)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "d6aa8777",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'text': 'Standard Oil Co and BP North America Inc said they plan to form a venture to manage the money market borrowing and investment activities of both companies. BP North America is a subsidiary of British Petroleum Co Plc <BP> which also owns a 55 pct interest in Standard Oil. The venture will be called BP/Standard Financial Trading and will be operated by Standard Oil under the oversight of a joint management committee.',\n",
" 'target': 'STANDARD OIL <SRD> TO FORM FINANCIAL UNIT'}"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dataset['train'][1]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "b0562fe2",
"metadata": {},
"outputs": [],
"source": [
"dataset.save_to_disk('reuters_processed')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d2e6a3e4",
"metadata": {},
"outputs": [],
"source": [
"dataset['train'].to_csv('reuters_train.csv', index=False, header=True)\n",
"dataset['test'].to_csv('reuters_test.csv', index=False, header=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d79a04f2",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|