Spaces:
Build error
Build error
File size: 34,493 Bytes
c94e693 |
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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 |
{
"cells": [
{
"cell_type": "markdown",
"id": "b1b28232-b65d-41ce-88de-fd70b93a528d",
"metadata": {},
"source": [
"# Imports"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "abb5186b-ee67-4e1e-882d-3d8d5b4575d4",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import json\n",
"from pathlib import Path\n",
"import pickle\n",
"from tqdm.auto import tqdm\n",
"\n",
"from haystack.nodes.preprocessor import PreProcessor"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c4b82ea2-8b30-4c2e-99f0-9a30f2f1bfb7",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/home/ec2-user/arabic-wiki\n"
]
}
],
"source": [
"proj_dir = Path.cwd().parent\n",
"print(proj_dir)"
]
},
{
"cell_type": "markdown",
"id": "76119e74-f601-436d-a253-63c5a19d1c83",
"metadata": {},
"source": [
"# Config"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "f6f74545-54a7-4f41-9f02-96964e1417f0",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"files_in = list((proj_dir / 'data/consolidated').glob('*.ndjson'))\n",
"folder_out = proj_dir / 'data/processed'\n",
"folder_out_str = str(folder_out)"
]
},
{
"cell_type": "markdown",
"id": "509f41f9-a59f-4171-b61f-ae0cf756fc92",
"metadata": {},
"source": [
"# Analysis"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f0cbd1c9-3105-4940-85dc-c01ccaa217c7",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"with open(files_in[0], 'r') as f:\n",
" articles = [json.loads(line) for line in f]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "004aae7b-1a2f-4a0b-9450-5d80475258b1",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'content': 'الماء مادةٌ شفافةٌ عديمة اللون والرائحة، وهو المكو...',\n",
" 'meta': {'id': '7',\n",
" 'revid': '2080427',\n",
" 'title': 'ماء',\n",
" 'url': 'https://ar.wikipedia.org/wiki?curid=7'}}\n"
]
}
],
"source": [
"from pprint import pprint\n",
"article = articles[0].copy()\n",
"article['content'] = article['content'][:50] + '...'\n",
"pprint(article)"
]
},
{
"cell_type": "markdown",
"id": "6a643cf2-abce-48a9-b4e0-478bcbee28c3",
"metadata": {},
"source": [
"# Preprocessing"
]
},
{
"cell_type": "markdown",
"id": "a8f9630e-447e-423e-9f6c-e1dbc654f2dd",
"metadata": {},
"source": [
"Its important to choose good pre-processing options. \n",
"\n",
"Clean whitespace helps each stage of RAG. It adds noise to the embeddings, and wastes space when we prompt with it.\n",
"\n",
"I chose to split by word as it would be tedious to tokenize here, and that doesnt scale well. The context length for most embedding models ends up being 512 tokens. We saw this within a good z-score is ~225 token.\n",
"\n",
"I like to respect the sentence boundary, thats why I gave a ~50 word buffer."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "18807aea-24e4-4d74-bf10-55b24f3cb52c",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"pp = PreProcessor(clean_whitespace = True,\n",
" clean_header_footer = False,\n",
" clean_empty_lines = True,\n",
" remove_substrings = None,\n",
" split_by='word',\n",
" split_length = 225,\n",
" split_overlap = 50,\n",
" split_respect_sentence_boundary = True,\n",
" tokenizer_model_folder = None,\n",
" id_hash_keys = None,\n",
" progress_bar = False,\n",
" add_page_number = False,\n",
" max_chars_check = 10_000)"
]
},
{
"cell_type": "markdown",
"id": "3c1ab000-6574-485e-87f6-cc210f6e8a61",
"metadata": {},
"source": [
"When we break a wikipedia article up, we lose some of the context. The local context is somewhat preserved by the `split_overlap`. Im trying to preserve the global context by adding a prefix that has the article's title.\n",
"\n",
"You could enhance this with the summary as well. This is mostly to help the retrieval step of RAG. Note that the way Im doing it alters some of `haystack`'s features like the hash and the lengths, but those arent too necessary. \n",
"\n",
"A more advanced way for many business applications would be to summarize the document and add that as a prefix for sub-documents.\n",
"\n",
"One last thing to note, is that it would be prudent (in some use-cases) to preserve the original document without the summary to give to the reader (retrieve with the summary but prompt without), but since this is a demo use-case I wont be doing that."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "63871bdd-0369-4dd7-a65e-ccba29baed44",
"metadata": {},
"outputs": [],
"source": [
"with open(files_in[0], 'r', encoding='utf-8') as f:\n",
" articles = [json.loads(line) for line in f]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "5c3b48b7-3c0f-41ba-a423-b716649efcaa",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"We found one or more sentences whose word count is higher than the split length.\n",
"Document e3e2bf8b3399979cb16219b175041b4d is 11336 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
"Document 91ad1d1a24e93abacabd5a5478a96977 is 14251 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
"Document 1625c431c0fcfaf81c13e0da59071a81 is 13395 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
"Document 790d3b2d94a68cbec6d77f3c15d0e679 is 13484 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
"Document e2dcf80a1f9dfc118aed059255f9b90b is 13217 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 3min 31s, sys: 95.1 ms, total: 3min 31s\n",
"Wall time: 3min 31s\n"
]
}
],
"source": [
"%%time\n",
"documents = pp.process(articles)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "de6e1690-131a-41d1-a473-c908c2e40939",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Document 91ad1d1a24e93abacabd5a5478a96977 is 14251 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
"Document e3e2bf8b3399979cb16219b175041b4d is 11336 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
"Document 1625c431c0fcfaf81c13e0da59071a81 is 13395 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
"Document 790d3b2d94a68cbec6d77f3c15d0e679 is 13484 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
"Document e2dcf80a1f9dfc118aed059255f9b90b is 13217 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 6.86 s, sys: 1.31 s, total: 8.16 s\n",
"Wall time: 1min 33s\n"
]
}
],
"source": [
"%%time\n",
"import os\n",
"import concurrent.futures\n",
"\n",
"def parallel_preprocessing(articles):\n",
" # Utility function to divide the articles into smaller chunks\n",
" def chunkify(lst, n):\n",
" \"\"\"Yield successive n-sized chunks from lst.\"\"\"\n",
" for i in range(0, len(lst), n):\n",
" yield lst[i:i + n]\n",
"\n",
" # Size of each chunk. Adjust based on your needs.\n",
" CHUNK_SIZE = 10_000 \n",
" article_chunks = list(chunkify(articles, CHUNK_SIZE))\n",
"\n",
" # Number of processes to run in parallel.\n",
" # Use all available CPUs, but you can reduce the number if you wish to leave some CPUs free.\n",
" NUM_PROCESSES = os.cpu_count() \n",
"\n",
" with concurrent.futures.ProcessPoolExecutor(max_workers=NUM_PROCESSES) as executor:\n",
" documents_list = list(executor.map(pp.process, article_chunks))\n",
"\n",
" # Flatten the documents_list to get a single list of documents\n",
" documents = [doc for sublist in documents_list for doc in sublist]\n",
" return documents\n",
"\n",
"documents = parallel_preprocessing(articles)\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "dab1658a-79a7-40f2-9a8c-1798e0d124bf",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "a4d4ade8158144c6a06f072b550157c3",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/23 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Document 91ad1d1a24e93abacabd5a5478a96977 is 14251 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
"Document e3e2bf8b3399979cb16219b175041b4d is 11336 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
"Document 1625c431c0fcfaf81c13e0da59071a81 is 13395 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
"Document 790d3b2d94a68cbec6d77f3c15d0e679 is 13484 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
"Document e2dcf80a1f9dfc118aed059255f9b90b is 13217 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "bcb8fe6999ec408f99ef25c96c85c7b3",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/243068 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Document 2693d73124e824fb45633e34189a6226 is 14375 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "6ac231651a214181be49584511c97fe9",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/104065 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c43b5ac1f3b84cac8f2715c5dcf21fb8",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/123154 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "a1b8201fc0ba44cf9ac938556c7effc8",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/135965 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "58ce68b1b5044b0eb1c457495159cb6d",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/99138 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b80a37f6986e478d85397f7295b67079",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/83678 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "8197d71a396e47588699f22e44c843af",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/30573 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "39024c19258b41c4bcd7b6e5c0617d54",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/78957 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "aa26a27b522448b3bc2808f8ad650a28",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/86327 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "3aae87d4a40d4e9e937646f02c969bb2",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/83111 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "16038478b51a422d8ed7d597c9812999",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/92664 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "0b9ea31fc970402f8368b4be64d91546",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/66404 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "710bf094621c4472bc6d3973619627df",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/62844 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "0bae815b7b7448978fd042b6381b63ba",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/59349 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "483c99d5547a4bedb10802f9bbe414d3",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/52554 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Document c55a744420239d9865b23f9ff7ab37cc is 23179 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
"Document 991fef35ead593c41f1dc73224ed7799 is 13179 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "34288f7c2437440ab07134116a6f5bf2",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/34240 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c6b95ccc5799488980af6f73ab564c8b",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/35933 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Document c4ae5d66606dcc644293946f8f5c7cab is 10612 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c6794c5150274e5cb2a09c52bfcf4c1a",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/64575 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "5aaf9358178d4daf9169a14cd71aa20f",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/94244 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Document 1d1357a32c6cd115525ceb0590e6d67d is 11314 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
"Document eca1c79de6ca898903e5db0a44a54803 is 24659 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n",
"Document 8a59ff0086cc7772046f54a065566ff9 is 14659 characters long after preprocessing, where the maximum length should be 10000. Something might be wrong with the splitting, check the document affected to prevent issues at query time. This document will be now hard-split at 10000 chars recursively.\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "102f3b5a0c5e46f69742b9f41cc79928",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/124472 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "3e4803f9fc3d405eac97a64489c0d7e0",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/121849 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d2b4c133d207446c96b4e576aac15bf7",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/147110 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "1cbd39af41ef4b20a8bc1bea7fe8186f",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/70322 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 2min 21s, sys: 20.1 s, total: 2min 41s\n",
"Wall time: 13min 36s\n"
]
}
],
"source": [
"%%time\n",
"for file_in in tqdm(files_in):\n",
" # Load articles\n",
" with open(file_in, 'r', encoding='utf-8') as f:\n",
" articles = [json.loads(line) for line in f]\n",
" \n",
" # Preprocess articles\n",
" documents = parallel_preprocessing(articles)\n",
" \n",
" # Prefix each document's content\n",
" for document in tqdm(documents):\n",
" if document.meta['_split_id'] != 0:\n",
" document.content = f'عنوان: {document.meta[\"title\"]}. ' + document.content\n",
" \n",
" processed_articles = [document.to_dict() for document in documents]\n",
" with open(folder_out/file_in.name, 'w', encoding='utf-8') as f:\n",
" for article in processed_articles:\n",
" json_str = json.dumps(article, ensure_ascii=False)\n",
" f.write(json_str + '\\n')\n",
" "
]
},
{
"cell_type": "markdown",
"id": "72c1849c-1f4d-411f-b74b-6208b1e48217",
"metadata": {},
"source": [
"## Pre-processing Examples"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "02c1c6c8-6283-49a8-9d29-c355f1b08540",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"<Document: {'content': 'عشاء كارين هو من سلسلة مطاعم استرالية يهدف عمداً عن تجربة تناول طعام غير سارَة ويتم توجيه الموظفين لإهانة العملاء طوال وجباتهم.\\nاقتبس اسم المطعم من المصطلح العامي على الإنترنت (كارين) والذي يستخدم لوصف امرأة بيضاء مسنة وقحة بشكل نمطي.\\nتاريخ المطعم.\\nتم إنشاء السلسلة في أستراليا (سيدني) في عام 2021 من قبل إيدين ليفن وجيمس فاريل. المطعم ذو طابع خاص يعتمد على خدمة تجربة طعام غير سارة حيث يدفع العملاء للموظفين لإهانتهم وكان من المفترض ان يكون المطعم مطعماً منبثقاً لمدة ستة أشهر في وورلد سكوير.\\nاثارت فكرة المطعم في البداية ردات فعل متغايرة مما أثار الخوف بشأن ما إذا كانت الإهانات المتبادلة من الممكن ان تعرض الموظفين لسوء المعاملة من قبل العملاء.\\nاسم (كارين) هو إشارة إلى الإسم المستخدم في الميمات (النكت التي تنشهر بسرعة في مواقع التواصل) لوصف امرأة بيضاء في منتصف العمر ووقحة بشكل نمطي.\\nيطلب من الموظفين ارتداء شخصية وقحة والسخرية من العملاء بشكل هزلي اثناء تناول وجباتهم ومن المتوقع ان يعيد العملاء هذا السلوك من خلال التصرف بوقاحة تجاه الموظفين ومع ذلك يُحظر على العملاء والموظفين استخدام الإهانات العنصرية أو التحيز الجنسي أو رهاب المثلية الجنسية.\\nتتضمن العديد من هذه التبادلات لغة نابية ويجب ان يكون برفقة الاشخاص اللذين يقلون عن 16 عاماََ بالغين.\\nكما يمكن لمالكي بطاقة هوية تظهر ان اسمهم كارين الحصول على مشروب مجاني.\\n', 'content_type': 'text', 'score': None, 'meta': {'id': '8974231', 'revid': '593870', 'url': 'https://ar.wikipedia.org/wiki?curid=8974231', 'title': 'مطعم عشاء كارين', '_split_id': 0, '_split_overlap': [{'doc_id': '288196225044b53e6ff86f2485257a0a', 'range': (790, 1225)}]}, 'id_hash_keys': ['content'], 'embedding': None, 'id': '1af84f3b4cc6a9f1018f2f80b4fd3ba7'}>"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"documents[0]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "b34890bf-9dba-459a-9b0d-aa4b5929cbe8",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"<Document: {'content': 'عنوان: مطعم عشاء كارين. يطلب من الموظفين ارتداء شخصية وقحة والسخرية من العملاء بشكل هزلي اثناء تناول وجباتهم ومن المتوقع ان يعيد العملاء هذا السلوك من خلال التصرف بوقاحة تجاه الموظفين ومع ذلك يُحظر على العملاء والموظفين استخدام الإهانات العنصرية أو التحيز الجنسي أو رهاب المثلية الجنسية.\\nتتضمن العديد من هذه التبادلات لغة نابية ويجب ان يكون برفقة الاشخاص اللذين يقلون عن 16 عاماََ بالغين.\\nكما يمكن لمالكي بطاقة هوية تظهر ان اسمهم كارين الحصول على مشروب مجاني.\\nيرتكز المطعم علو وجبات العشاء الأمريكية في خمسينات القرن الماضي وتتميز القائمة بالهامبرغر وأجنحة الدجاج.\\nأصبح محتوى شائع لوسائل التواصل الإجتماعي خصوصاً على منصة (تيك توك) حيث نشر العملاء مقاطع فيديو لتفاعلاتهم مع الموظفين.\\nفتحت السلسلة في مواقع في المملكة المتحدة والولايات المتحدة ونيوزيلندا.\\nفي شهر أغسطس سنة 2022 اثار المطعم جدلاً بعد ان انتشر مقطع يُظهر فيه أحد موظفي فريق العمل في منطقة بريزبين يتصرف بشكل غير لائق على منصة تيك توك حيث القى تعليقات غير لائقة موجهة إلى زبونة قاصر ووالدها الذي كان يشاركها الطعام بإتهامه انه يمارس الرذيلة مع الأطفال، فقام المتحدث باسم السلسلة بالرد بإنهم اصيبو بخيبة أمل بسبب السلوك وأن الحادث يتعارض مع إرشاداتهم.', 'content_type': 'text', 'score': None, 'meta': {'id': '8974231', 'revid': '593870', 'url': 'https://ar.wikipedia.org/wiki?curid=8974231', 'title': 'مطعم عشاء كارين', '_split_id': 1, '_split_overlap': [{'doc_id': '1af84f3b4cc6a9f1018f2f80b4fd3ba7', 'range': (0, 435)}]}, 'id_hash_keys': ['content'], 'embedding': None, 'id': '288196225044b53e6ff86f2485257a0a'}>"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"documents[1]"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "e6f50c27-a486-47e9-ba60-d567f5e530db",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"<Document: {'content': 'نيكولاي فليفا والمعروف أيضًا باسم نيكو فليفا (1840 - 4 أغسطس عام 1920) هو سياسي وصحفي سياسي ومحامي أفلاقي ومن ثم روماني. اشتهر بانخراطه في المجريات السياسية ونزعته الوطنية الصريحة التي كادت تصل إلى حد الديماغوجية. اختبر كافة الصيغ السياسية التي يسمح بها نظام الحزبين في رومانيا. دام حضوره على الساحة العامة عقودًا من الزمن، شغل خلالها مقعدًا في جمعية النواب وتولى منصب عمدة مدينة بوخارست خلال الفترة الممتدة من عام 1884 حتى عام 1886.\\nباشر فليفا مسيرته السياسية مع الحزب الليبرالي الوطني الذي ساعد على تأسيسه وتمثيله أمام القضاء، ولكنه اتجه في ما بعد إلى معارضة احتكار الحزب للسلطة. حاول إنشاء حزب ثالث ودخل في مفاوضات من أجل اعتماد برامج سياسية مشتركة خاصة بقوى المعارضة المختلفة ومن بينها حزب المحافظين وجمعية جونيما في ظل الإدارات الليبرالية الوطنية المتعاقبة. ذاع صيته عندما تورط في فضيحتين كبيرتين خلال ثمانينيات القرن التاسع عشر حين أدى استهزائه بسلطة الحزب الليبرالي الوطني إلى اندلاع معارك في الشوارع ووقوع حادثتي إطلاق النار منفصلتين. اعتُبرت الجماعات الموالية لفليفا الصوت الرائد المعبر عن سخط الطبقة الوسطى وقتذاك، وشكلت إحدى التيارات التي دفعت باتجاه تبني حق الاقتراع العام للذكور.\\nعاد فليفا إلى المعسكر الليبرالي الوطني بعد منعه من تولي حقائب وزارية ريادية في الحكومات المحافظة، وأصبح وزيرًا للشؤون الداخلية خلال الفترة من عام 1895 حتى عام 1896. ', 'content_type': 'text', 'score': None, 'meta': {'id': '9044009', 'revid': '1673186', 'url': 'https://ar.wikipedia.org/wiki?curid=9044009', 'title': 'نيكولاي فليفا', '_split_id': 0, '_split_overlap': [{'doc_id': '188181b1026773d720383c7e7307b241', 'range': (943, 1257)}]}, 'id_hash_keys': ['content'], 'embedding': None, 'id': 'af5cda4722fa2a961bef66de8a6b3e17'}>"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"documents[10102]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "5485cc27-3d3f-4b96-8884-accf5324da2d",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2094596\n"
]
}
],
"source": [
"!cat \"$folder_out_str\"/*.ndjson | wc -l"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c5833dba-1bf6-48aa-be6f-0d70c71e54aa",
"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
}
|