Spaces:
Sleeping
Sleeping
File size: 25,952 Bytes
9b14109 96c8541 9b14109 2cb4802 9b14109 2cb4802 9b14109 2cb4802 9b14109 df1b485 9b14109 8bda43d 9b14109 8bda43d 9b14109 30bb00b 9b14109 96c8541 9b14109 |
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 |
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"MongoDB Cluster management\n",
"-----------------------\n",
"\n",
"Let us create some function to send (insert documents) and retrieve collections from our cluster database."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"We must import the pymongo the client and the server modules."
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [],
"source": [
"from pymongo.mongo_client import MongoClient\n",
"from pymongo.server_api import ServerApi\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {},
"outputs": [],
"source": [
"# let us set below the cluster uri\n",
"uri = \"mongodb+srv://oumar199:[email protected]/?retryWrites=true&w=majority\""
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Inserting new documents"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us insert new documents to our collection."
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [],
"source": [
"# we must initialize the client\n",
"client = MongoClient(uri, server_api = ServerApi('1'))"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us create a new collection."
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [],
"source": [
"# create a database\n",
"db = client.get_database('WolofTranslation')"
]
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {},
"outputs": [],
"source": [
"# create a new collection for the new sentences\n",
"sentences = db.sentences"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"It is time to insert the new documents."
]
},
{
"cell_type": "code",
"execution_count": 106,
"metadata": {},
"outputs": [],
"source": [
"# recuperate the already created sentences\n",
"corpora = pd.read_csv('wolof-translate/wolof_translate/data/sentences/wolof_french.csv')"
]
},
{
"cell_type": "code",
"execution_count": 107,
"metadata": {},
"outputs": [],
"source": [
"# let us reset the indices\n",
"corpora.reset_index(inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 108,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>index</th>\n",
" <th>french</th>\n",
" <th>wolof</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0</td>\n",
" <td>J'arrive tout de suite chez toi.</td>\n",
" <td>Léegui léegui ma egg sa kër.</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" <td>J'en suis sûr, cette photo ci c'est la photo p...</td>\n",
" <td>Waaw nataal bii nataal la boob ay nit ñu baree...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2</td>\n",
" <td>Je vois devant moi une photo sur laquelle beau...</td>\n",
" <td>Nataal bii maa ngi ciy janloog haa ay nit yu b...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>3</td>\n",
" <td>Ceux-ci sont des personnes qui sont sortis pou...</td>\n",
" <td>Lii, ay nit lañu yu génn di ñaxtu. Jëm yi nag ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>4</td>\n",
" <td>Salut ! Ceux-là qui ressemblent à des personne...</td>\n",
" <td>Salaawaalekum ! Ñii de, mel nañ ne, ay nit ñu ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>5</td>\n",
" <td>Cette photo ci c'est une photo sur laquelle je...</td>\n",
" <td>Nataal bi ab nataal la boo xamante yni maa ngi...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>6</td>\n",
" <td>Sur la photo, ont voit des personnes qui se ré...</td>\n",
" <td>Nataal bii ñoo ngi ciy gis ay nit ñuy ñaxtu wa...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>7</td>\n",
" <td>On voit sur la photo beaucoup de personnes sor...</td>\n",
" <td>Ñu gis ci nataal bi ay nit ñu bari ñu génn ci ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>8</td>\n",
" <td>C'est des poissons, oui. Ils sont de couleur b...</td>\n",
" <td>Jën la waaw, Wu am wirgo Wu baxa ak Wu xonq.</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>9</td>\n",
" <td>Ah sur cette photo ci cependant, il y a un poi...</td>\n",
" <td>Aah nataal bii nag, aw jën la. Jën wi mi ngi a...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" index french \\\n",
"0 0 J'arrive tout de suite chez toi. \n",
"1 1 J'en suis sûr, cette photo ci c'est la photo p... \n",
"2 2 Je vois devant moi une photo sur laquelle beau... \n",
"3 3 Ceux-ci sont des personnes qui sont sortis pou... \n",
"4 4 Salut ! Ceux-là qui ressemblent à des personne... \n",
"5 5 Cette photo ci c'est une photo sur laquelle je... \n",
"6 6 Sur la photo, ont voit des personnes qui se ré... \n",
"7 7 On voit sur la photo beaucoup de personnes sor... \n",
"8 8 C'est des poissons, oui. Ils sont de couleur b... \n",
"9 9 Ah sur cette photo ci cependant, il y a un poi... \n",
"\n",
" wolof \n",
"0 Léegui léegui ma egg sa kër. \n",
"1 Waaw nataal bii nataal la boob ay nit ñu baree... \n",
"2 Nataal bii maa ngi ciy janloog haa ay nit yu b... \n",
"3 Lii, ay nit lañu yu génn di ñaxtu. Jëm yi nag ... \n",
"4 Salaawaalekum ! Ñii de, mel nañ ne, ay nit ñu ... \n",
"5 Nataal bi ab nataal la boo xamante yni maa ngi... \n",
"6 Nataal bii ñoo ngi ciy gis ay nit ñuy ñaxtu wa... \n",
"7 Ñu gis ci nataal bi ay nit ñu bari ñu génn ci ... \n",
"8 Jën la waaw, Wu am wirgo Wu baxa ak Wu xonq. \n",
"9 Aah nataal bii nag, aw jën la. Jën wi mi ngi a... "
]
},
"execution_count": 108,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"corpora.head(10)"
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {},
"outputs": [],
"source": [
"# insert the sentences\n",
"results = sentences.insert_many({\n",
" '_id': corp, # set the id\n",
" 'french': corpora.loc[corp, 'french'],\n",
" 'wolof': corpora.loc[corp, 'wolof']\n",
" } for corp in corpora.index\n",
")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us insert the deleted sentences."
]
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {},
"outputs": [],
"source": [
"# create a new collection named deleted.\n",
"deleted = db.deleted"
]
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {},
"outputs": [],
"source": [
"# recuperated the data frame of deleted sentences\n",
"del_corpora = pd.read_csv('wolof-translate/wolof_translate/data/sentences/deleted_lines.csv')"
]
},
{
"cell_type": "code",
"execution_count": 112,
"metadata": {},
"outputs": [],
"source": [
"# reset the indices\n",
"del_corpora.reset_index(inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 113,
"metadata": {},
"outputs": [],
"source": [
"# insert the deleted sentences\n",
"results = deleted.insert_many({\n",
" '_id': corp, # set the id\n",
" 'french': corpora.loc[corp, 'french'],\n",
" 'wolof': corpora.loc[corp, 'wolof']\n",
" } for corp in del_corpora.index\n",
")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Modify sentences"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"We want to modify only one sentence at a time."
]
},
{
"cell_type": "code",
"execution_count": 114,
"metadata": {},
"outputs": [],
"source": [
"# select the id to modify\n",
"id_ = 1\n",
"\n",
"# retrieve new sentences\n",
"french = corpora.loc[id_, 'french']\n",
"wolof = corpora.loc[id_, 'wolof']"
]
},
{
"cell_type": "code",
"execution_count": 115,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(\"J'en suis sûr, cette photo ci c'est la photo pris au moment où plusieurs personnes font une marche de révolte tendant leurs mains. Ceux là sont assis, ceux là sont debout entrain de marcher. On a écrit sur la photo quelque chose de bleu concernant la Casamance.\",\n",
" 'Waaw nataal bii nataal la boob ay nit ñu baree bari ñoo xam ni dañuy doxub ñaxtu ñoo ci nekk tàllal seen i loxo. Ñee sukku ñeel taxaw jodd di dox. Ñu bind ci kaw nataal bi lu xaw a baxa la bind ci laa kaasamãs.')"
]
},
"execution_count": 115,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# print the sentences\n",
"french, wolof"
]
},
{
"cell_type": "code",
"execution_count": 118,
"metadata": {},
"outputs": [],
"source": [
"# modify the sentences at the id\n",
"results = sentences.update_one(\n",
" {\n",
" '_id': {'$eq': id_}\n",
" },\n",
" {\n",
" '$set': {\n",
" 'french': french,\n",
" 'wolof': wolof + \"--------\" # we added a modification\n",
" }\n",
" }\n",
")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us show the first documents."
]
},
{
"cell_type": "code",
"execution_count": 119,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>_id</th>\n",
" <th>french</th>\n",
" <th>wolof</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0</td>\n",
" <td>J'arrive tout de suite chez toi.</td>\n",
" <td>Léegui léegui ma egg sa kër.</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" <td>J'en suis sûr, cette photo ci c'est la photo p...</td>\n",
" <td>Waaw nataal bii nataal la boob ay nit ñu baree...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2</td>\n",
" <td>Je vois devant moi une photo sur laquelle beau...</td>\n",
" <td>Nataal bii maa ngi ciy janloog haa ay nit yu b...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>3</td>\n",
" <td>Ceux-ci sont des personnes qui sont sortis pou...</td>\n",
" <td>Lii, ay nit lañu yu génn di ñaxtu. Jëm yi nag ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>4</td>\n",
" <td>Salut ! Ceux-là qui ressemblent à des personne...</td>\n",
" <td>Salaawaalekum ! Ñii de, mel nañ ne, ay nit ñu ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>5</td>\n",
" <td>Cette photo ci c'est une photo sur laquelle je...</td>\n",
" <td>Nataal bi ab nataal la boo xamante yni maa ngi...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>6</td>\n",
" <td>Sur la photo, ont voit des personnes qui se ré...</td>\n",
" <td>Nataal bii ñoo ngi ciy gis ay nit ñuy ñaxtu wa...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>7</td>\n",
" <td>On voit sur la photo beaucoup de personnes sor...</td>\n",
" <td>Ñu gis ci nataal bi ay nit ñu bari ñu génn ci ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>8</td>\n",
" <td>C'est des poissons, oui. Ils sont de couleur b...</td>\n",
" <td>Jën la waaw, Wu am wirgo Wu baxa ak Wu xonq.</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>9</td>\n",
" <td>Ah sur cette photo ci cependant, il y a un poi...</td>\n",
" <td>Aah nataal bii nag, aw jën la. Jën wi mi ngi a...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" _id french \\\n",
"0 0 J'arrive tout de suite chez toi. \n",
"1 1 J'en suis sûr, cette photo ci c'est la photo p... \n",
"2 2 Je vois devant moi une photo sur laquelle beau... \n",
"3 3 Ceux-ci sont des personnes qui sont sortis pou... \n",
"4 4 Salut ! Ceux-là qui ressemblent à des personne... \n",
"5 5 Cette photo ci c'est une photo sur laquelle je... \n",
"6 6 Sur la photo, ont voit des personnes qui se ré... \n",
"7 7 On voit sur la photo beaucoup de personnes sor... \n",
"8 8 C'est des poissons, oui. Ils sont de couleur b... \n",
"9 9 Ah sur cette photo ci cependant, il y a un poi... \n",
"\n",
" wolof \n",
"0 Léegui léegui ma egg sa kër. \n",
"1 Waaw nataal bii nataal la boob ay nit ñu baree... \n",
"2 Nataal bii maa ngi ciy janloog haa ay nit yu b... \n",
"3 Lii, ay nit lañu yu génn di ñaxtu. Jëm yi nag ... \n",
"4 Salaawaalekum ! Ñii de, mel nañ ne, ay nit ñu ... \n",
"5 Nataal bi ab nataal la boo xamante yni maa ngi... \n",
"6 Nataal bii ñoo ngi ciy gis ay nit ñuy ñaxtu wa... \n",
"7 Ñu gis ci nataal bi ay nit ñu bari ñu génn ci ... \n",
"8 Jën la waaw, Wu am wirgo Wu baxa ak Wu xonq. \n",
"9 Aah nataal bii nag, aw jën la. Jën wi mi ngi a... "
]
},
"execution_count": 119,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# get the 10 first sentences into a Data Frame\n",
"pd.DataFrame(list(sentences.find().limit(10)))"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Delete sentences"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"We want to modify only one sentence at a time. The deleted sentences must be added into the 'deleted' collection. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# recuperate the sentences to delete (id = 0)\n",
"id_ = 0\n",
"\n",
"del_sent = sentences.find_one(\n",
" {\n",
" '_id': {'$eq': id_}\n",
" } \n",
")\n",
"\n",
"# delete the sentence and add it into the deleted sentences\n",
"sentences.delete_one(\n",
" {\n",
" '_id': {'$eq': del_sent['_id']}\n",
" }\n",
")\n",
"\n",
"results = deleted.insert_one(\n",
" {\n",
" '_id': len(list(deleted.find())),\n",
" 'french': del_sent['french'],\n",
" 'wolof': del_sent['wolof']\n",
" }\n",
")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### From collection to DataFrame"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"We must convert the sentences to csv files in order to use at the training step."
]
},
{
"cell_type": "code",
"execution_count": 123,
"metadata": {},
"outputs": [],
"source": [
"# recuperate the new corpora\n",
"new_corpora = pd.DataFrame(list(sentences.find()))\n",
"\n",
"# recuperate the deleted sentences as a Data Frame\n",
"deleted_df = pd.DataFrame(list(deleted.find()))\n",
"\n",
"# save the data frames as csv files\n",
"new_corpora.set_index('_id', inplace=True)\n",
"\n",
"deleted_df.set_index('_id', inplace=True)\n",
"\n",
"new_corpora.to_csv('wolof-translate/wolof_translate/data/sentences/wolof_french.csv', index=False)\n",
"\n",
"deleted_df.to_csv('wolof-translate/wolof_translate/data/sentences/deleted_lines.csv', index=False)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### All in one"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us create a class which consider each of the methods we investigated previously."
]
},
{
"cell_type": "code",
"execution_count": 129,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'_id': 150,\n",
" 'french': \"Sur la photo que vous m'avez envoyée, j'ai vu qu'il s'agissait de gendarmes. Des gendarmes qui portent, cependant, des... des boucliers. Des verres conçus pour les protéger. Ils sont faces au peuple s'échangeant contre eux des cailloux et des pierres.\",\n",
" 'wolof': 'Nataal bi ngeen ma yonnee, gis naa ni ay takk-der la. Takk der yoo xamantane bii nag, jël nañ loo xamantane bii mooy ay,... Ay baar. Ay verre yoo xam ne dañ kaa defar pour ñu leen di baare, ñu jàkkarlook askan wi di sànnanteek ñoom ay xeer ak ay doj.'}"
]
},
"execution_count": 129,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(sentences.find().sort('_id', -1).limit(1))[0]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting wolof_translate/utils/database_manager.py\n"
]
}
],
"source": [
"%%writefile wolof_translate/utils/database_manager.py\n",
"from pymongo.mongo_client import MongoClient\n",
"from pymongo.server_api import ServerApi\n",
"import pandas as pd\n",
"\n",
"class TranslationMongoDBManager:\n",
" \n",
" def __init__(self, uri: str, database: str):\n",
" \n",
" # recuperate the client\n",
" self.client = MongoClient(uri)\n",
" \n",
" # recuperate the database\n",
" self.db = self.client.get_database(database)\n",
" \n",
" def insert_documents(self, documents: list, collection: str = \"sentences\"):\n",
" \n",
" # insert documents inside a collection\n",
" results = self.db[collection].insert_many(documents)\n",
" \n",
" return results\n",
" \n",
" def insert_document(self, document: dict, collection: str = \"sentences\"):\n",
" \n",
" assert not '_id' in document\n",
" \n",
" # get the id of the last sentence (recuperate the max id and add 1 to it)\n",
" max_id = self.get_max_id(collection)\n",
" \n",
" # add the new sentences\n",
" document['_id'] = max_id + 1\n",
" \n",
" results = self.db[collection].insert_one(\n",
" document\n",
" )\n",
" \n",
" return results\n",
" \n",
" def update_document(self, id: int, collection: str = \"sentences\", update_collection: str = \"updated\"):\n",
" \n",
" # recuperate the document to update\n",
" upd_sent = self.db[collection].find_one(\n",
" {\n",
" '_id': {\n",
" '$eq': id\n",
" }\n",
" }\n",
" )\n",
" \n",
" # delete the document\n",
" self.db[collection].delete_one(\n",
" {\n",
" '_id': {'$eq': upd_sent['_id']}\n",
" }\n",
" )\n",
" \n",
" # add the sentences to the deleted sentences\n",
" upd_sent['_id'] = len(list(self.db[update_collection].find()))\n",
" \n",
" results = self.db[update_collection].insert_one(\n",
" upd_sent\n",
" )\n",
" \n",
" return results\n",
" \n",
" def delete_document(self, id: int, collection: str = \"sentences\", del_collection: str = \"deleted\"):\n",
" \n",
" # recuperate the document to delete\n",
" del_sent = self.db[collection].find_one(\n",
" {\n",
" '_id': {\n",
" '$eq': id\n",
" }\n",
" }\n",
" )\n",
" \n",
" # delete the sentence\n",
" self.db[collection].delete_one(\n",
" {\n",
" '_id': {'$eq': del_sent['_id']}\n",
" }\n",
" )\n",
" \n",
" # add the sentences to the deleted sentences\n",
" del_sent['_id'] = len(list(self.db[del_collection].find()))\n",
" \n",
" results = self.db[del_collection].insert_one(\n",
" del_sent\n",
" )\n",
" \n",
" return results\n",
" \n",
" def get_max_id(self, collection: str = \"sentences\"):\n",
" \n",
" # recuperate the maximum id\n",
" id = list(self.db[collection].find().sort('_id', -1).limit(1))[0]['_id']\n",
" \n",
" return id\n",
" \n",
" def save_data_frames(self, sentences_path: str, deleted_path: str, collection: str = \"sentences\", del_collection: str = \"deleted\"):\n",
" \n",
" # recuperate the new corpora\n",
" new_corpora = pd.DataFrame(list(self.db[collection].find()))\n",
"\n",
" # recuperate the deleted sentences as a Data Frame\n",
" deleted_df = pd.DataFrame(list(self.db[del_collection].find()))\n",
"\n",
" # save the data frames as csv files\n",
" new_corpora.set_index('_id', inplace=True)\n",
"\n",
" deleted_df.set_index('_id', inplace=True)\n",
"\n",
" new_corpora.to_csv(sentences_path, index=False)\n",
"\n",
" deleted_df.to_csv(deleted_path, index=False)\n",
" \n",
" def load_data_frames(self, collection: str = \"sentences\", del_collection: str = \"deleted\"):\n",
" \n",
" # recuperate the new corpora\n",
" new_corpora = pd.DataFrame(list(self.db[collection].find()))\n",
"\n",
" # recuperate the deleted sentences as a Data Frame\n",
" deleted_df = pd.DataFrame(list(self.db[del_collection].find()))\n",
" \n",
" return new_corpora, deleted_df"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "pytorch1-HleOW5am-py3.10",
"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.8"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
|