File size: 9,950 Bytes
70bad37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6115cdf
70bad37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6115cdf
70bad37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6115cdf
70bad37
 
 
6115cdf
 
70bad37
6115cdf
70bad37
6115cdf
 
70bad37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6115cdf
70bad37
 
 
 
 
 
 
 
 
6115cdf
70bad37
 
 
 
6115cdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70bad37
6115cdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70bad37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6115cdf
70bad37
 
 
 
 
 
 
 
 
 
 
6115cdf
70bad37
 
 
 
 
 
6115cdf
 
 
 
 
70bad37
 
 
 
 
6115cdf
70bad37
 
 
 
 
 
 
 
6115cdf
 
 
 
 
 
 
 
70bad37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6115cdf
70bad37
 
 
 
 
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "883a8a6a-d0b5-40ea-90a0-5b33d3332360",
   "metadata": {},
   "source": [
    "# Get Data\n",
    "The data from wikipedia starts in XML, this is a relatively simple way to format that into a single json for our purposes."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7d66da5-185c-409e-9568-f211ca4b725e",
   "metadata": {},
   "source": [
    "## Initialize Variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ea8ae64c-f597-4c94-b93d-1b78060d7953",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "import sys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2f9527f9-4756-478b-99ac-a3c8c26ab63e",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "proj_dir_path = Path.cwd().parent\n",
    "proj_dir = str(proj_dir_path)\n",
    "\n",
    "# So we can import later\n",
    "sys.path.append(proj_dir)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "860da614-743b-4060-9d22-673896414cbd",
   "metadata": {},
   "source": [
    "## Install Libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8bec29e3-8434-491f-914c-13f303dc68f3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "%pip install -q -r \"$proj_dir\"/requirements.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b928c71f-7e34-47ee-b55e-aa12d5118ba7",
   "metadata": {},
   "source": [
    "## Download Latest Arabic Wikipedia"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1dc5f57-c877-43e3-8131-4f351b99168d",
   "metadata": {},
   "source": [
    "Im getting \"latest\" but its good to see what version it is nonetheless."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fe4b357f-88fe-44b5-9fce-354404b1447f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Last-Modified: Sat, 21 Oct 2023 02:57:42 GMT\n"
     ]
    }
   ],
   "source": [
    "!curl -I https://dumps.wikimedia.org/arwiki/latest/arwiki-latest-pages-articles-multistream.xml.bz2 --silent | grep \"Last-Modified\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe62d4a3-b59b-40c4-9a8c-bf0a447a9ec2",
   "metadata": {},
   "source": [
    "Download simple wikipedia"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0f309c12-12de-4460-a03f-bd5b6fcc942c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--2023-10-28 08:09:45--  https://dumps.wikimedia.org/arwiki/latest/arwiki-latest-pages-articles-multistream.xml.bz2\n",
      "Resolving dumps.wikimedia.org (dumps.wikimedia.org)... 208.80.154.142, 2620:0:861:2:208:80:154:142\n",
      "Connecting to dumps.wikimedia.org (dumps.wikimedia.org)|208.80.154.142|:443... connected.\n",
      "HTTP request sent, awaiting response... 200 OK\n",
      "Length: 1671369109 (1.6G) [application/octet-stream]\n",
      "Saving to: β€˜/home/ec2-user/arabic-wiki/data/raw/arwiki-latest-pages-articles-multistream.xml.bz2’\n",
      "\n",
      "100%[====================================>] 1,671,369,109 4.54MB/s   in 5m 54s \n",
      "\n",
      "2023-10-28 08:15:39 (4.51 MB/s) - β€˜/home/ec2-user/arabic-wiki/data/raw/arwiki-latest-pages-articles-multistream.xml.bz2’ saved [1671369109/1671369109]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!wget -nc -P \"$proj_dir\"/data/raw https://dumps.wikimedia.org/arwiki/latest/arwiki-latest-pages-articles-multistream.xml.bz2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46af5df6-5785-400a-986c-54a2c98768ea",
   "metadata": {},
   "source": [
    "## Extract from XML\n",
    "The download format from wikipedia is in XML. `wikiextractor` will convert this into a jsonl format split into many folders and files."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c22dedcd-73b3-4aad-8eb7-1063954967ed",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO: Preprocessing '/home/ec2-user/arabic-wiki/data/raw/arwiki-latest-pages-articles-multistream.xml.bz2' to collect template definitions: this may take some time.\n",
      "INFO: Preprocessed 100000 pages\n",
      "INFO: Preprocessed 200000 pages\n",
      "INFO: Preprocessed 300000 pages\n",
      "INFO: Preprocessed 400000 pages\n",
      "INFO: Preprocessed 500000 pages\n",
      "INFO: Preprocessed 600000 pages\n",
      "INFO: Preprocessed 700000 pages\n",
      "INFO: Preprocessed 800000 pages\n",
      "INFO: Preprocessed 900000 pages\n",
      "INFO: Preprocessed 1000000 pages\n",
      "INFO: Preprocessed 1100000 pages\n",
      "INFO: Preprocessed 1200000 pages\n",
      "INFO: Preprocessed 1300000 pages\n",
      "INFO: Preprocessed 1400000 pages\n",
      "INFO: Preprocessed 1500000 pages\n",
      "INFO: Preprocessed 1600000 pages\n",
      "INFO: Preprocessed 1700000 pages\n",
      "INFO: Preprocessed 1800000 pages\n",
      "INFO: Preprocessed 1900000 pages\n",
      "INFO: Preprocessed 2000000 pages\n",
      "INFO: Preprocessed 2100000 pages\n",
      "INFO: Preprocessed 2200000 pages\n",
      "INFO: Preprocessed 2300000 pages\n",
      "INFO: Preprocessed 2400000 pages\n",
      "INFO: Preprocessed 2500000 pages\n",
      "INFO: Preprocessed 2600000 pages\n",
      "INFO: Preprocessed 2700000 pages\n",
      "INFO: Preprocessed 2800000 pages\n",
      "INFO: Preprocessed 2900000 pages\n",
      "INFO: Preprocessed 3000000 pages\n",
      "INFO: Preprocessed 3100000 pages\n",
      "INFO: Preprocessed 3200000 pages\n",
      "INFO: Preprocessed 3300000 pages\n",
      "INFO: Preprocessed 3400000 pages\n",
      "INFO: Preprocessed 3500000 pages\n",
      "INFO: Loaded 130917 templates in 407.5s\n",
      "INFO: Starting page extraction from /home/ec2-user/arabic-wiki/data/raw/arwiki-latest-pages-articles-multistream.xml.bz2.\n",
      "INFO: Using 3 extract processes.\n",
      "INFO: Extracted 100000 articles (1963.4 art/s)\n",
      "INFO: Extracted 200000 articles (3371.1 art/s)\n",
      "INFO: Extracted 300000 articles (3008.9 art/s)\n",
      "INFO: Extracted 400000 articles (2935.2 art/s)\n",
      "INFO: Extracted 500000 articles (3563.3 art/s)\n",
      "INFO: Extracted 600000 articles (4209.0 art/s)\n",
      "INFO: Extracted 700000 articles (6075.1 art/s)\n",
      "INFO: Extracted 800000 articles (3531.2 art/s)\n",
      "INFO: Extracted 900000 articles (3466.4 art/s)\n",
      "INFO: Extracted 1000000 articles (3789.2 art/s)\n",
      "INFO: Extracted 1100000 articles (2282.6 art/s)\n",
      "INFO: Extracted 1200000 articles (4499.8 art/s)\n",
      "INFO: Extracted 1300000 articles (5143.6 art/s)\n",
      "INFO: Extracted 1400000 articles (5474.2 art/s)\n",
      "INFO: Extracted 1500000 articles (6086.9 art/s)\n",
      "INFO: Extracted 1600000 articles (5453.4 art/s)\n",
      "INFO: Extracted 1700000 articles (5911.6 art/s)\n",
      "INFO: Extracted 1800000 articles (5087.4 art/s)\n",
      "INFO: Extracted 1900000 articles (3782.4 art/s)\n",
      "INFO: Extracted 2000000 articles (2493.9 art/s)\n",
      "INFO: Extracted 2100000 articles (2742.2 art/s)\n",
      "INFO: Extracted 2200000 articles (2416.5 art/s)\n",
      "INFO: Finished 3-process extraction of 2254650 articles in 641.2s (3516.3 art/s)\n"
     ]
    }
   ],
   "source": [
    "!wikiextractor -o \"$proj_dir\"/data/raw/output  --json \"$proj_dir\"/data/raw/arwiki-latest-pages-articles-multistream.xml.bz2 "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb8063c6-1bed-49f0-948a-eeb9a7933b4a",
   "metadata": {},
   "source": [
    "## Consolidate into json\n",
    "\n",
    "The split format is tedious to deal with, so now we we will consolidate this into 1 json file. This is fine since our data fits easily in RAM. But if it didnt, there are better options.\n",
    "\n",
    "Feel free to check out the [consolidate file](../src/preprocessing/consolidate.py) for more details."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0a4ce3aa-9c1e-45e4-8219-a1714f482371",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from src.preprocessing.consolidate import folder_to_json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3e93da6a-e304-450c-a81e-ffecaf0d8a9a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Processing: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 6119/6119 [01:11<00:00, 85.38file/s, File: wiki_18 | Dir: /home/ec2-user/arabic-wiki/data/raw/output/CJ]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wiki processed in 72.87 seconds!\n"
     ]
    }
   ],
   "source": [
    "folder = proj_dir_path / 'data/raw/output'\n",
    "folder_out = proj_dir_path / 'data/consolidated/'\n",
    "folder_to_json(folder, folder_out, 'ar_wiki')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "553039d0-6315-40b8-b8f5-c6672598a5f3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}