laugustyniak commited on
Commit
64fd3de
1 Parent(s): 2dbac05

convertion scripts

Browse files
.gitattributes CHANGED
@@ -35,3 +35,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
35
  *.mp3 filter=lfs diff=lfs merge=lfs -text
36
  *.ogg filter=lfs diff=lfs merge=lfs -text
37
  *.wav filter=lfs diff=lfs merge=lfs -text
 
 
35
  *.mp3 filter=lfs diff=lfs merge=lfs -text
36
  *.ogg filter=lfs diff=lfs merge=lfs -text
37
  *.wav filter=lfs diff=lfs merge=lfs -text
38
+ *.jsonl filter=lfs diff=lfs merge=lfs -text
notebooks/convert-dataset.ipynb ADDED
@@ -0,0 +1,529 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": null,
6
+ "id": "003ecb55",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "from pathlib import Path\n",
11
+ "from typing import Dict, List\n",
12
+ "from tqdm import tqdm\n",
13
+ "\n",
14
+ "import pandas as pd\n",
15
+ "import srsly\n",
16
+ "import spacy\n",
17
+ "from spacy.training.iob_utils import biluo_to_iob\n",
18
+ "from spacy.training import offsets_to_biluo_tags\n"
19
+ ]
20
+ },
21
+ {
22
+ "cell_type": "code",
23
+ "execution_count": null,
24
+ "id": "d43fd200",
25
+ "metadata": {},
26
+ "outputs": [],
27
+ "source": [
28
+ "dataset_path = Path(\"promises-elections-pl-twitter-1-reviewed-fixed-case.jsonl\")"
29
+ ]
30
+ },
31
+ {
32
+ "cell_type": "code",
33
+ "execution_count": null,
34
+ "id": "f2ffadfc",
35
+ "metadata": {},
36
+ "outputs": [],
37
+ "source": [
38
+ "data = list(srsly.read_jsonl(dataset_path.as_posix()))"
39
+ ]
40
+ },
41
+ {
42
+ "cell_type": "code",
43
+ "execution_count": null,
44
+ "id": "e89bde09",
45
+ "metadata": {},
46
+ "outputs": [],
47
+ "source": [
48
+ "import uuid\n",
49
+ "from IPython.display import display_javascript, display_html\n",
50
+ "import json\n",
51
+ "\n",
52
+ "class RenderJSON(object):\n",
53
+ " def __init__(self, json_data):\n",
54
+ " if isinstance(json_data, dict):\n",
55
+ " self.json_str = json.dumps(json_data)\n",
56
+ " else:\n",
57
+ " self.json_str = json\n",
58
+ " self.uuid = str(uuid.uuid4())\n",
59
+ " \n",
60
+ " def _ipython_display_(self):\n",
61
+ " display_html('<div id=\"{}\" style=\"height: 600px; width:100%;\"></div>'.format(self.uuid),\n",
62
+ " raw=True\n",
63
+ " )\n",
64
+ " display_javascript(\"\"\"\n",
65
+ " require([\"https://rawgit.com/caldwell/renderjson/master/renderjson.js\"], function() {\n",
66
+ " document.getElementById('%s').appendChild(renderjson(%s))\n",
67
+ " });\n",
68
+ " \"\"\" % (self.uuid, self.json_str), raw=True)"
69
+ ]
70
+ },
71
+ {
72
+ "cell_type": "code",
73
+ "execution_count": null,
74
+ "id": "fed644a9",
75
+ "metadata": {},
76
+ "outputs": [],
77
+ "source": [
78
+ "RenderJSON(data[0])"
79
+ ]
80
+ },
81
+ {
82
+ "cell_type": "code",
83
+ "execution_count": null,
84
+ "id": "4fad229b",
85
+ "metadata": {},
86
+ "outputs": [],
87
+ "source": [
88
+ "data_df = pd.DataFrame(data)"
89
+ ]
90
+ },
91
+ {
92
+ "cell_type": "code",
93
+ "execution_count": null,
94
+ "id": "9f75c8b5",
95
+ "metadata": {},
96
+ "outputs": [],
97
+ "source": [
98
+ "data[0][\"text\"]"
99
+ ]
100
+ },
101
+ {
102
+ "cell_type": "code",
103
+ "execution_count": null,
104
+ "id": "eadb23f1",
105
+ "metadata": {},
106
+ "outputs": [],
107
+ "source": [
108
+ "data_df = data_df[data_df.answer == \"accept\"]"
109
+ ]
110
+ },
111
+ {
112
+ "cell_type": "code",
113
+ "execution_count": null,
114
+ "id": "261e6a8c",
115
+ "metadata": {},
116
+ "outputs": [],
117
+ "source": [
118
+ "data_df.text[1]"
119
+ ]
120
+ },
121
+ {
122
+ "cell_type": "code",
123
+ "execution_count": null,
124
+ "id": "cbe9e877",
125
+ "metadata": {},
126
+ "outputs": [],
127
+ "source": [
128
+ "data_df.text[1][202:224]"
129
+ ]
130
+ },
131
+ {
132
+ "cell_type": "code",
133
+ "execution_count": null,
134
+ "id": "5583aa66",
135
+ "metadata": {},
136
+ "outputs": [],
137
+ "source": [
138
+ "data_df.spans[1]"
139
+ ]
140
+ },
141
+ {
142
+ "cell_type": "code",
143
+ "execution_count": null,
144
+ "id": "cedd7c42",
145
+ "metadata": {},
146
+ "outputs": [],
147
+ "source": [
148
+ "def convert_spacy_to_iob(text: str, spans: List[Dict], nlp: spacy.language.Language) -> str:\n",
149
+ " doc = nlp(text)\n",
150
+ " entities = [(span[\"start\"], span[\"end\"], span[\"label\"].upper()) for span in spans]\n",
151
+ " if entities:\n",
152
+ " main_entity = list(set(span[\"label\"].upper() for span in spans))[0]\n",
153
+ " else:\n",
154
+ " main_entity = None\n",
155
+ " tags = offsets_to_biluo_tags(doc, entities)\n",
156
+ " # then convert L->I and U->B to have IOB tags for the tokens in the doc\n",
157
+ " # tags_corrected = [tag.replace(\"L-\", \"I-\").replace(\"U-\", \"B-\") for tag in tags]\n",
158
+ " tags_corrected = biluo_to_iob(tags)\n",
159
+ " tokens = [token.text for token in doc]\n",
160
+ " return {\n",
161
+ " \"tokens\": tokens, \n",
162
+ " \"tags\": tags_corrected,\n",
163
+ " \"label\": main_entity or \"\"\n",
164
+ " }"
165
+ ]
166
+ },
167
+ {
168
+ "cell_type": "code",
169
+ "execution_count": null,
170
+ "id": "c7d3aa1c",
171
+ "metadata": {},
172
+ "outputs": [],
173
+ "source": [
174
+ "from spacy.lang import pl\n",
175
+ "\n",
176
+ "nlp = pl.Language()"
177
+ ]
178
+ },
179
+ {
180
+ "cell_type": "code",
181
+ "execution_count": null,
182
+ "id": "a0449216",
183
+ "metadata": {},
184
+ "outputs": [],
185
+ "source": [
186
+ "from pprint import pprint"
187
+ ]
188
+ },
189
+ {
190
+ "cell_type": "code",
191
+ "execution_count": null,
192
+ "id": "4edc0665",
193
+ "metadata": {},
194
+ "outputs": [],
195
+ "source": [
196
+ "tokens, tags, label = convert_spacy_to_iob(data_df.text[0], data_df.spans[0], nlp)"
197
+ ]
198
+ },
199
+ {
200
+ "cell_type": "code",
201
+ "execution_count": null,
202
+ "id": "db5c6b07",
203
+ "metadata": {},
204
+ "outputs": [],
205
+ "source": [
206
+ "tokens"
207
+ ]
208
+ },
209
+ {
210
+ "cell_type": "code",
211
+ "execution_count": null,
212
+ "id": "dee2cf4f",
213
+ "metadata": {},
214
+ "outputs": [],
215
+ "source": [
216
+ "tags"
217
+ ]
218
+ },
219
+ {
220
+ "cell_type": "code",
221
+ "execution_count": null,
222
+ "id": "656c39a8",
223
+ "metadata": {},
224
+ "outputs": [],
225
+ "source": [
226
+ "label"
227
+ ]
228
+ },
229
+ {
230
+ "cell_type": "code",
231
+ "execution_count": null,
232
+ "id": "37e46df4",
233
+ "metadata": {},
234
+ "outputs": [],
235
+ "source": [
236
+ "iob_dataset = pd.DataFrame([\n",
237
+ " {\n",
238
+ " **convert_spacy_to_iob(row[\"text\"], row[\"spans\"], nlp),\n",
239
+ " \"url\": row[\"url\"],\n",
240
+ " \"tweet_id\": row[\"url\"].split(\"/\")[-1],\n",
241
+ " }\n",
242
+ " for _, row\n",
243
+ " in tqdm(data_df.iterrows())\n",
244
+ "])"
245
+ ]
246
+ },
247
+ {
248
+ "cell_type": "code",
249
+ "execution_count": null,
250
+ "id": "74c3d161",
251
+ "metadata": {},
252
+ "outputs": [],
253
+ "source": [
254
+ "iob_dataset"
255
+ ]
256
+ },
257
+ {
258
+ "cell_type": "code",
259
+ "execution_count": null,
260
+ "id": "0caa005a",
261
+ "metadata": {},
262
+ "outputs": [],
263
+ "source": [
264
+ "iob_dataset.label.value_counts()"
265
+ ]
266
+ },
267
+ {
268
+ "cell_type": "code",
269
+ "execution_count": null,
270
+ "id": "d121b8fd",
271
+ "metadata": {},
272
+ "outputs": [],
273
+ "source": [
274
+ "from more_itertools import flatten "
275
+ ]
276
+ },
277
+ {
278
+ "cell_type": "code",
279
+ "execution_count": null,
280
+ "id": "55212d38",
281
+ "metadata": {},
282
+ "outputs": [],
283
+ "source": [
284
+ "sorted(set(flatten(iob_dataset.tags)))"
285
+ ]
286
+ },
287
+ {
288
+ "cell_type": "code",
289
+ "execution_count": null,
290
+ "id": "8063ab0f",
291
+ "metadata": {},
292
+ "outputs": [],
293
+ "source": [
294
+ "from sklearn.model_selection import train_test_split"
295
+ ]
296
+ },
297
+ {
298
+ "cell_type": "code",
299
+ "execution_count": null,
300
+ "id": "9787243d",
301
+ "metadata": {},
302
+ "outputs": [],
303
+ "source": [
304
+ "train, test = train_test_split(iob_dataset, test_size=0.4)\n",
305
+ "dev, test = train_test_split(test, test_size=0.5)"
306
+ ]
307
+ },
308
+ {
309
+ "cell_type": "code",
310
+ "execution_count": null,
311
+ "id": "bcb323fc",
312
+ "metadata": {},
313
+ "outputs": [],
314
+ "source": [
315
+ "train.label.value_counts()"
316
+ ]
317
+ },
318
+ {
319
+ "cell_type": "code",
320
+ "execution_count": null,
321
+ "id": "f11f7de6",
322
+ "metadata": {},
323
+ "outputs": [],
324
+ "source": [
325
+ "train.reset_index(inplace=True, drop=True)\n",
326
+ "train.drop(columns=[\"label\"], inplace=True)"
327
+ ]
328
+ },
329
+ {
330
+ "cell_type": "code",
331
+ "execution_count": null,
332
+ "id": "459dad3c",
333
+ "metadata": {},
334
+ "outputs": [],
335
+ "source": [
336
+ "train"
337
+ ]
338
+ },
339
+ {
340
+ "cell_type": "code",
341
+ "execution_count": null,
342
+ "id": "9bdcab49",
343
+ "metadata": {},
344
+ "outputs": [],
345
+ "source": [
346
+ "len(train)"
347
+ ]
348
+ },
349
+ {
350
+ "cell_type": "code",
351
+ "execution_count": null,
352
+ "id": "aeff2557",
353
+ "metadata": {},
354
+ "outputs": [],
355
+ "source": [
356
+ "len(test)"
357
+ ]
358
+ },
359
+ {
360
+ "cell_type": "code",
361
+ "execution_count": null,
362
+ "id": "d1301717",
363
+ "metadata": {},
364
+ "outputs": [],
365
+ "source": [
366
+ "len(dev)"
367
+ ]
368
+ },
369
+ {
370
+ "cell_type": "code",
371
+ "execution_count": null,
372
+ "id": "d55c9edc",
373
+ "metadata": {},
374
+ "outputs": [],
375
+ "source": [
376
+ "test"
377
+ ]
378
+ },
379
+ {
380
+ "cell_type": "code",
381
+ "execution_count": null,
382
+ "id": "636e9268",
383
+ "metadata": {},
384
+ "outputs": [],
385
+ "source": [
386
+ "test.label.value_counts()"
387
+ ]
388
+ },
389
+ {
390
+ "cell_type": "code",
391
+ "execution_count": null,
392
+ "id": "f5486e11",
393
+ "metadata": {},
394
+ "outputs": [],
395
+ "source": [
396
+ "test.reset_index(inplace=True, drop=True)\n",
397
+ "test.drop(columns=[\"label\"], inplace=True)"
398
+ ]
399
+ },
400
+ {
401
+ "cell_type": "code",
402
+ "execution_count": null,
403
+ "id": "34aa63ef",
404
+ "metadata": {},
405
+ "outputs": [],
406
+ "source": [
407
+ "test"
408
+ ]
409
+ },
410
+ {
411
+ "cell_type": "code",
412
+ "execution_count": null,
413
+ "id": "c1bc32c4",
414
+ "metadata": {},
415
+ "outputs": [],
416
+ "source": [
417
+ "dev"
418
+ ]
419
+ },
420
+ {
421
+ "cell_type": "code",
422
+ "execution_count": null,
423
+ "id": "c8ad5315",
424
+ "metadata": {},
425
+ "outputs": [],
426
+ "source": [
427
+ "dev.label.value_counts()"
428
+ ]
429
+ },
430
+ {
431
+ "cell_type": "code",
432
+ "execution_count": null,
433
+ "id": "5591dea4",
434
+ "metadata": {},
435
+ "outputs": [],
436
+ "source": [
437
+ "dev.reset_index(inplace=True, drop=True)\n",
438
+ "dev.drop(columns=[\"label\"], inplace=True)"
439
+ ]
440
+ },
441
+ {
442
+ "cell_type": "code",
443
+ "execution_count": null,
444
+ "id": "53f0c791",
445
+ "metadata": {},
446
+ "outputs": [],
447
+ "source": [
448
+ "dev"
449
+ ]
450
+ },
451
+ {
452
+ "cell_type": "code",
453
+ "execution_count": null,
454
+ "id": "1180559d",
455
+ "metadata": {},
456
+ "outputs": [],
457
+ "source": [
458
+ "train.to_json(\"train.json\", orient=\"records\")\n",
459
+ "test.to_json(\"test.json\", orient=\"records\")\n",
460
+ "dev.to_json(\"dev.json\", orient=\"records\")"
461
+ ]
462
+ },
463
+ {
464
+ "cell_type": "code",
465
+ "execution_count": null,
466
+ "id": "d70338dc",
467
+ "metadata": {},
468
+ "outputs": [],
469
+ "source": [
470
+ "df = pd.read_json(\"train.json\")"
471
+ ]
472
+ },
473
+ {
474
+ "cell_type": "code",
475
+ "execution_count": null,
476
+ "id": "2e774c48",
477
+ "metadata": {},
478
+ "outputs": [],
479
+ "source": [
480
+ "df"
481
+ ]
482
+ },
483
+ {
484
+ "cell_type": "code",
485
+ "execution_count": null,
486
+ "id": "eea225bb",
487
+ "metadata": {},
488
+ "outputs": [],
489
+ "source": [
490
+ "for idx, row in df.iterrows():\n",
491
+ " print(row.tweet_id)"
492
+ ]
493
+ },
494
+ {
495
+ "cell_type": "code",
496
+ "execution_count": null,
497
+ "id": "7c5b26e9",
498
+ "metadata": {},
499
+ "outputs": [],
500
+ "source": [
501
+ "print(train.head(1).T.values)"
502
+ ]
503
+ }
504
+ ],
505
+ "metadata": {
506
+ "interpreter": {
507
+ "hash": "e36ecc5ec16660b989892b46ab22daaf88d1f0f732170387d9da292f75b1ccf3"
508
+ },
509
+ "kernelspec": {
510
+ "display_name": "Python 3.9.12 64-bit ('embeddings')",
511
+ "language": "python",
512
+ "name": "python3"
513
+ },
514
+ "language_info": {
515
+ "codemirror_mode": {
516
+ "name": "ipython",
517
+ "version": 3
518
+ },
519
+ "file_extension": ".py",
520
+ "mimetype": "text/x-python",
521
+ "name": "python",
522
+ "nbconvert_exporter": "python",
523
+ "pygments_lexer": "ipython3",
524
+ "version": "3.9.12"
525
+ }
526
+ },
527
+ "nbformat": 4,
528
+ "nbformat_minor": 5
529
+ }
notebooks/promises-elections-pl-twitter-1-reviewed-fixed-case.jsonl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:252999a9d3f323e5333da4885e0e301daf4279ba09e2a6d9b6cc35a1cb330150
3
+ size 11337591