File size: 52,316 Bytes
78aa4ee |
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 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "Igc5itf-xMGj"
},
"source": [
"# Masakhane - Machine Translation for African Languages (Using JoeyNMT)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "x4fXCKCf36IK"
},
"source": [
"## Note before beginning:\n",
"### - The idea is that you should be able to make minimal changes to this in order to get SOME result for your own translation corpus. \n",
"\n",
"### - The tl;dr: Go to the **\"TODO\"** comments which will tell you what to update to get up and running\n",
"\n",
"### - If you actually want to have a clue what you're doing, read the text and peek at the links\n",
"\n",
"### - With 100 epochs, it should take around 7 hours to run in Google Colab\n",
"\n",
"### - Once you've gotten a result for your language, please attach and email your notebook that generated it to [email protected]\n",
"\n",
"### - If you care enough and get a chance, doing a brief background on your language would be amazing. See examples in [(Martinus, 2019)](https://arxiv.org/abs/1906.05685)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "l929HimrxS0a"
},
"source": [
"## Retrieve your data & make a parallel corpus\n",
"\n",
"If you are wanting to use the JW300 data referenced on the Masakhane website or in our GitHub repo, you can use `opus-tools` to convert the data into a convenient format. `opus_read` from that package provides a convenient tool for reading the native aligned XML files and to convert them to TMX format. The tool can also be used to fetch relevant files from OPUS on the fly and to filter the data as necessary. [Read the documentation](https://pypi.org/project/opustools-pkg/) for more details.\n",
"\n",
"Once you have your corpus files in TMX format (an xml structure which will include the sentences in your target language and your source language in a single file), we recommend reading them into a pandas dataframe. Thankfully, Jade wrote a silly `tmx2dataframe` package which converts your tmx file to a pandas dataframe. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 139
},
"colab_type": "code",
"id": "oGRmDELn7Az0",
"outputId": "37f707b9-45b6-46e6-f545-bc00b31f998d"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: googledrivedownloader in /usr/local/lib/python3.6/dist-packages (0.4)\n",
"Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob&response_type=code&scope=email%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdocs.test%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive.photos.readonly%20https%3a%2f%2fwww.googleapis.com%2fauth%2fpeopleapi.readonly\n",
"\n",
"Enter your authorization code:\n",
"··········\n",
"Mounted at /content/drive\n"
]
}
],
"source": [
"! pip install googledrivedownloader\n",
"from google.colab import drive\n",
"drive.mount('/content/drive')"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "Cn3tgQLzUxwn"
},
"outputs": [],
"source": [
"# TODO: Set your source and target languages. Keep in mind, these traditionally use language codes as found here:\n",
"# These will also become the suffix's of all vocab and corpus files used throughout\n",
"import os\n",
"source_language = \"en\"\n",
"target_language = \"ar\" \n",
"lc = False # If True, lowercase the data.\n",
"seed = 42 # Random seed for shuffling.\n",
"tag = \"baseline\" # Give a unique name to your folder - this is to ensure you don't rewrite any models you've already submitted\n",
"\n",
"os.environ[\"src\"] = source_language # Sets them in bash as well, since we often use bash scripts\n",
"os.environ[\"tgt\"] = target_language\n",
"os.environ[\"tag\"] = tag\n",
"\n",
"# This will save it to a folder in our gdrive instead!\n",
"!mkdir -p \"masakhane/$src-$tgt-$tag\"\n",
"os.environ[\"gdrive_path\"] = \"masakhane/%s-%s-%s\" % (source_language, target_language, tag)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"id": "kBSgJHEw7Nvx",
"outputId": "9c544c51-4c87-40e2-f063-25cccd806739"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"masakhane/en-ar-baseline\n"
]
}
],
"source": [
"!echo $gdrive_path"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "g_9AAHS10I_z"
},
"source": [
"## Used Datasets\n",
" * JW300\n",
" * TED TALK\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "KGhNlkx90I_2"
},
"source": [
"### JW300"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 122
},
"colab_type": "code",
"id": "gA75Fs9ys8Y9",
"outputId": "42c6fae2-12a3-44b0-be3a-cdcb41d0be43"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting opustools-pkg\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/6c/9f/e829a0cceccc603450cd18e1ff80807b6237a88d9a8df2c0bb320796e900/opustools_pkg-0.0.52-py3-none-any.whl (80kB)\n",
"\r",
"\u001b[K |████ | 10kB 31.9MB/s eta 0:00:01\r",
"\u001b[K |████████ | 20kB 5.6MB/s eta 0:00:01\r",
"\u001b[K |████████████▏ | 30kB 7.9MB/s eta 0:00:01\r",
"\u001b[K |████████████████▏ | 40kB 5.3MB/s eta 0:00:01\r",
"\u001b[K |████████████████████▎ | 51kB 5.7MB/s eta 0:00:01\r",
"\u001b[K |████████████████████████▎ | 61kB 6.8MB/s eta 0:00:01\r",
"\u001b[K |████████████████████████████▎ | 71kB 7.6MB/s eta 0:00:01\r",
"\u001b[K |████████████████████████████████| 81kB 4.8MB/s \n",
"\u001b[?25hInstalling collected packages: opustools-pkg\n",
"Successfully installed opustools-pkg-0.0.52\n"
]
}
],
"source": [
"# Install opus-tools\n",
"! pip install opustools-pkg"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 241
},
"colab_type": "code",
"id": "xq-tDZVks7ZD",
"outputId": "8880aa17-243a-4a67-a33e-aa9286805895"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Alignment file /proj/nlpl/data/OPUS/JW300/latest/xml/ar-en.xml.gz not found. The following files are available for downloading:\n",
"\n",
" 5 MB https://object.pouta.csc.fi/OPUS-JW300/v1/xml/ar-en.xml.gz\n",
" 55 MB https://object.pouta.csc.fi/OPUS-JW300/v1/xml/ar.zip\n",
" 263 MB https://object.pouta.csc.fi/OPUS-JW300/v1/xml/en.zip\n",
"\n",
" 323 MB Total size\n",
"./JW300_latest_xml_ar-en.xml.gz ... 100% of 5 MB\n",
"./JW300_latest_xml_ar.zip ... 100% of 55 MB\n",
"./JW300_latest_xml_en.zip ... 100% of 263 MB\n",
"gzip: JW300_latest_xml_en-ar.xml.gz: No such file or directory\n"
]
}
],
"source": [
"# Downloading our corpus\n",
"! opus_read -d JW300 -s $src -t $tgt -wm moses -w jw300.$src jw300.$tgt -q\n",
"\n",
"# extract the corpus file\n",
"! gunzip JW300_latest_xml_$src-$tgt.xml.gz"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 442
},
"colab_type": "code",
"id": "n48GDRnP8y2G",
"outputId": "d4df7604-1101-409c-9c4f-803fb4436b46"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"--2020-02-14 18:09:35-- https://raw.githubusercontent.com/juliakreutzer/masakhane/master/jw300_utils/test/test.en-any.en\n",
"Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.0.133, 151.101.64.133, 151.101.128.133, ...\n",
"Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.0.133|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: 277791 (271K) [text/plain]\n",
"Saving to: ‘test.en-any.en’\n",
"\n",
"\r",
"test.en-any.en 0%[ ] 0 --.-KB/s \r",
"test.en-any.en 100%[===================>] 271.28K --.-KB/s in 0.02s \n",
"\n",
"2020-02-14 18:09:36 (15.2 MB/s) - ‘test.en-any.en’ saved [277791/277791]\n",
"\n",
"--2020-02-14 18:09:38-- https://raw.githubusercontent.com/juliakreutzer/masakhane/master/jw300_utils/test/test.en-ar.en\n",
"Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.0.133, 151.101.64.133, 151.101.128.133, ...\n",
"Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.0.133|:443... connected.\n",
"HTTP request sent, awaiting response... 404 Not Found\n",
"2020-02-14 18:09:38 ERROR 404: Not Found.\n",
"\n",
"mv: cannot stat 'test.en-ar.en': No such file or directory\n",
"--2020-02-14 18:09:44-- https://raw.githubusercontent.com/juliakreutzer/masakhane/master/jw300_utils/test/test.en-ar.ar\n",
"Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.0.133, 151.101.64.133, 151.101.128.133, ...\n",
"Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.0.133|:443... connected.\n",
"HTTP request sent, awaiting response... 404 Not Found\n",
"2020-02-14 18:09:44 ERROR 404: Not Found.\n",
"\n",
"mv: cannot stat 'test.en-ar.ar': No such file or directory\n"
]
}
],
"source": [
"# Download the global test set.\n",
"! wget https://raw.githubusercontent.com/juliakreutzer/masakhane/master/jw300_utils/test/test.en-any.en\n",
" \n",
"# And the specific test set for this language pair.\n",
"os.environ[\"trg\"] = target_language \n",
"os.environ[\"src\"] = source_language \n",
"\n",
"! wget https://raw.githubusercontent.com/juliakreutzer/masakhane/master/jw300_utils/test/test.en-$trg.en \n",
"! mv test.en-$trg.en test.en\n",
"! wget https://raw.githubusercontent.com/juliakreutzer/masakhane/master/jw300_utils/test/test.en-$trg.$trg \n",
"! mv test.en-$trg.$trg test.$trg"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"id": "NqDG-CI28y2L",
"outputId": "7e008cf2-97fa-4332-d95a-2c156b7c2bdb"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loaded 3571 global test sentences to filter from the training/dev data.\n"
]
}
],
"source": [
"# Read the test data to filter from train and dev splits.\n",
"# Store english portion in set for quick filtering checks.\n",
"en_test_sents = set()\n",
"filter_test_sents = \"test.en-any.en\"\n",
"j = 0\n",
"with open(filter_test_sents) as f:\n",
" for line in f:\n",
" en_test_sents.add(line.strip())\n",
" j += 1\n",
"print('Loaded {} global test sentences to filter from the training/dev data.'.format(j))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"id": "3CNdwLBCfSIl",
"outputId": "b7b73d72-fef9-4713-de2c-db092b14d927"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loaded data and skipped 3135/547133 lines since contained in test set.\n"
]
}
],
"source": [
"import pandas as pd\n",
"\n",
"# TMX file to dataframe\n",
"source_file = 'jw300.' + source_language\n",
"target_file = 'jw300.' + target_language\n",
"test_src = \"test.en\"\n",
"test_trg = \"test.ar\"\n",
"source = []\n",
"target = []\n",
"skip_lines = [] # Collect the line numbers of the source portion to skip the same lines for the target portion.\n",
"with open(source_file) as f, open(test_src, 'a') as t:\n",
" for i, line in enumerate(f):\n",
" # Skip sentences that are contained in the test set.\n",
" if line.strip() not in en_test_sents:\n",
" source.append(line.strip())\n",
" else:\n",
" skip_lines.append(i)\n",
" print(line.strip(), file= t) \n",
"with open(target_file) as f, open(test_trg, 'a') as t:\n",
" for j, line in enumerate(f):\n",
" # Only add to corpus if corresponding source was not skipped.\n",
" if j not in skip_lines:\n",
" target.append(line.strip())\n",
" else:\n",
" print(line.strip(), file= t)\n",
" \n",
"print('Loaded data and skipped {}/{} lines since contained in test set.'.format(len(skip_lines), i))\n",
" \n",
"# df = pd.DataFrame(zip(source, target), columns=['source_sentence', 'target_sentence'])\n",
"# if you get TypeError: data argument can't be an iterator is because of your zip version run this below\n",
"df = pd.DataFrame(list(zip(source, target)), columns=['source_sentence', 'target_sentence'])\n",
"copy_df = df "
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "Xk2-OGk81bpM"
},
"source": [
"## Next cells describe the inital pre-processing that was performed on the text\n",
"- we remove the punctuation from Arabic and English texts.\n",
"- We remove arabic diacritics. \n",
"- We then normalize the text. \n",
"- we finally remove repeating characters. \n",
"- The above steps were performed with regular experessions and simple Python string processing. \n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "MPmkQIcjdk3u"
},
"outputs": [],
"source": [
"import re\n",
"import string\n",
"import sys\n",
"import argparse\n",
"\n",
"arabic_punctuations = '''`÷×؛<>_()*&^%][ـ،/:\"؟.,'{}~¦+|!”…“–ـ'''\n",
"english_punctuations = string.punctuation\n",
"punctuations_list = arabic_punctuations + english_punctuations\n",
"\n",
"arabic_diacritics = re.compile(\"\"\"\n",
" ّ | # Tashdid\n",
" َ | # Fatha\n",
" ً | # Tanwin Fath\n",
" ُ | # Damma\n",
" ٌ | # Tanwin Damm\n",
" ِ | # Kasra\n",
" ٍ | # Tanwin Kasr\n",
" ْ | # Sukun\n",
" ـ # Tatwil/Kashida\n",
" \"\"\", re.VERBOSE)\n",
"\n",
"\n",
"def normalize_arabic(text):\n",
" text = re.sub(\"[إأآا]\", \"ا\", text)\n",
" text = re.sub(\"ى\", \"ي\", text)\n",
" text = re.sub(\"ؤ\", \"ء\", text)\n",
" text = re.sub(\"ئ\", \"ء\", text)\n",
" text = re.sub(\"ة\", \"ه\", text)\n",
" text = re.sub(\"گ\", \"ك\", text)\n",
" return text\n",
"\n",
"\n",
"def remove_diacritics(text):\n",
" text = re.sub(arabic_diacritics, '', text)\n",
" return text\n",
"\n",
"\n",
"def remove_punctuations(text):\n",
" translator = str.maketrans('', '', punctuations_list)\n",
" return text.translate(translator)\n",
"\n",
"\n",
"def remove_repeating_char(text):\n",
" return re.sub(r'(.)\\1+', r'\\1', text)\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "sXhU7ScgdpF9"
},
"outputs": [],
"source": [
"def process(text):\n",
" text = normalize_arabic(text)\n",
" text = remove_diacritics(text)\n",
" text = remove_punctuations(text)\n",
" text = remove_repeating_char(text)\n",
"\n",
" return text "
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 357
},
"colab_type": "code",
"id": "9VbLeDuF0JAY",
"outputId": "5d8b284c-29f9-45c0-b794-1358832b906b"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" source_sentence target_sentence\n",
"543989 هناك اسباب عديده لٰكن اهمها ان ٱلكتاب ٱلمقدس...\n",
"543990 26 . Why are astrology and fortune - telling n... لماذا نلتزم بٱلدعوات ٱلتي نقبلها \n",
"543991 There are various reasons , but the prime reas... اذا قبلنا دعوه يجب ان نلتزم بكلمتنا اذا امكن \n",
"543992 If we have accepted an invitation , we should ... مز ١٥ ٤ لا يجب ان نعتذر عنها لاسباب تافه \n",
"543993 The host likely has worked hard to prepare the... والا يضيع تعب مضيفنا وتحضيراته — ب١٨ ٣ ص ...\n",
"543994 18 . Appointed men can learn what lessons from... ماذا يتعلم ٱلشيوخ وٱلخدام من تيموثاوس \n",
"543995 Timothy truly cared for people and put spiritu... يتعلم ٱلشيوخ وٱلخدام ٱلمساعدون ٱلكثير من مثال ...\n",
"543996 He worked hard in sacred service and applied t... فقد ٱهتم بغيره ٱهتماما حقيقيا واعطي ٱلاولويه ل...\n",
"543997 He kept training himself and relied on Jehovah... كما انه ٱجتهد في ٱلخدمه ٱلمقدسه وطبق ما تعلمه \n",
" source_sentence target_sentence\n",
"543989 هناك اسباب عديده لٰكن اهمها ان ٱلكتاب ٱلمقدس...\n",
"543990 26 . Why are astrology and fortune - telling n... لماذا نلتزم بٱلدعوات ٱلتي نقبلها \n",
"543991 There are various reasons , but the prime reas... اذا قبلنا دعوه يجب ان نلتزم بكلمتنا اذا امكن \n",
"543992 If we have accepted an invitation , we should ... مز ١٥ ٤ لا يجب ان نعتذر عنها لاسباب تافه \n",
"543993 The host likely has worked hard to prepare the... والا يضيع تعب مضيفنا وتحضيراته — ب١٨ ٣ ص ...\n",
"543994 18 . Appointed men can learn what lessons from... ماذا يتعلم ٱلشيوخ وٱلخدام من تيموثاوس \n",
"543995 Timothy truly cared for people and put spiritu... يتعلم ٱلشيوخ وٱلخدام ٱلمساعدون ٱلكثير من مثال ...\n",
"543996 He worked hard in sacred service and applied t... فقد ٱهتم بغيره ٱهتماما حقيقيا واعطي ٱلاولويه ل...\n",
"543997 He kept training himself and relied on Jehovah... كما انه ٱجتهد في ٱلخدمه ٱلمقدسه وطبق ما تعلمه \n"
]
}
],
"source": [
"for index, row in copy_df.iterrows():\n",
" s = row['target_sentence']\n",
" row['target_sentence'] = process(s)\n",
"\n",
"print(copy_df[-10:-1])\n",
"print(df[-10:-1])"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"id": "QCfM74A2f70J",
"outputId": "9d3813f0-0c87-423e-ec8f-a647f0dc1d0b"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"هناك اسباب عديده لٰكن اهم\n"
]
}
],
"source": [
"text = process(\"هُنَاكَ أَسْبَابٌ عَدِيدَةٌ ، لٰكِنَّ أَهَمَّ\")\n",
"print(text)"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "MYGo1eg0Bisv"
},
"outputs": [],
"source": [
"copy_df.to_csv(\"edited_arabic-english_data.csv\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "vRhANMoA0JAd"
},
"source": [
"### TED Talk"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "_vWnvgWH1bp6"
},
"source": [
"### TED Talk data intial preprocessing \n",
"The data was cleaned using MS-Excel because it was more convinent to use. The text didn't contain diacritics but it contained spaces, and NULL characters. "
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"id": "G0UNOkKO0JAf",
"outputId": "ee8083c0-6745-457d-bfa1-64eb1227dda2"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: ezodf in /usr/local/lib/python3.6/dist-packages (0.3.2)\n"
]
}
],
"source": [
"#IN Order to get the dataframe out of the .ods file please intall ezodf\n",
"! pip install ezodf\n"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "WHp0PS7NBRTT"
},
"outputs": [],
"source": [
"url = 'https://drive.google.com/uc?export=download&id=1oNU9PA47cEc6HfuD1gza44502GNQCLew'\n",
"\n",
"import requests\n",
"r = requests.get(url) \n",
"with open(\"processed_ted_talk_train_data.ods\", \"wb\") as code:\n",
" code.write(r.content)"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "k_S93Qva0JAj"
},
"outputs": [],
"source": [
"import ezodf\n",
"def read_ods(filename, sheet_no=0, header=0):\n",
" tab = ezodf.opendoc(filename=filename).sheets[sheet_no]\n",
" return pd.DataFrame({col[header].value:[x.value for x in col[header+1:]]\n",
" for col in tab.columns()})\n",
"\n",
"df_ted = read_ods(\"processed_ted_talk_train_data.ods\")\n"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 204
},
"colab_type": "code",
"id": "JfFja8Sx2U2W",
"outputId": "1b7632ff-dc5e-4baa-9019-4c829ab7c680"
},
"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>en</th>\n",
" <th>ar</th>\n",
" <th>NaN</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Amongst all the troubling deficits we struggle...</td>\n",
" <td>من ضمن جميع المثبطات المقلقة التي نعاني منها ا...</td>\n",
" <td>None</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>We who are diplomats , we are trained to deal ...</td>\n",
" <td>نحن الدبلوماسيين تدربنا على التعامل مع الصراعا...</td>\n",
" <td>None</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>And I can tell you , our agenda is full .</td>\n",
" <td>وبامكاني القول ان جدول اعمالنا ممتلئ</td>\n",
" <td>None</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>There is trade , there is disarmament , there ...</td>\n",
" <td>هناك التجارة , هناك نزع السلاح , وهناك علاقات ...</td>\n",
" <td>None</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>But the picture is changing , and we are seein...</td>\n",
" <td>لكن الصورة تتغير ونحن نرى ان هناك لاعبين رئيسي...</td>\n",
" <td>None</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" en ... NaN\n",
"0 Amongst all the troubling deficits we struggle... ... None\n",
"1 We who are diplomats , we are trained to deal ... ... None\n",
"2 And I can tell you , our agenda is full . ... None\n",
"3 There is trade , there is disarmament , there ... ... None\n",
"4 But the picture is changing , and we are seein... ... None\n",
"\n",
"[5 rows x 3 columns]"
]
},
"execution_count": 76,
"metadata": {
"tags": []
},
"output_type": "execute_result"
}
],
"source": [
"df_ted.head()"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 204
},
"colab_type": "code",
"id": "ZXantb8m0JAo",
"outputId": "f9c56c7f-3b79-4668-bb9f-85de181284eb"
},
"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>source_sentence</th>\n",
" <th>target_sentence</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Amongst all the troubling deficits we struggle...</td>\n",
" <td>من ضمن جميع المثبطات المقلقة التي نعاني منها ا...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>We who are diplomats , we are trained to deal ...</td>\n",
" <td>نحن الدبلوماسيين تدربنا على التعامل مع الصراعا...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>And I can tell you , our agenda is full .</td>\n",
" <td>وبامكاني القول ان جدول اعمالنا ممتلئ</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>There is trade , there is disarmament , there ...</td>\n",
" <td>هناك التجارة , هناك نزع السلاح , وهناك علاقات ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>But the picture is changing , and we are seein...</td>\n",
" <td>لكن الصورة تتغير ونحن نرى ان هناك لاعبين رئيسي...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" source_sentence target_sentence\n",
"0 Amongst all the troubling deficits we struggle... من ضمن جميع المثبطات المقلقة التي نعاني منها ا...\n",
"1 We who are diplomats , we are trained to deal ... نحن الدبلوماسيين تدربنا على التعامل مع الصراعا...\n",
"2 And I can tell you , our agenda is full . وبامكاني القول ان جدول اعمالنا ممتلئ\n",
"3 There is trade , there is disarmament , there ... هناك التجارة , هناك نزع السلاح , وهناك علاقات ...\n",
"4 But the picture is changing , and we are seein... لكن الصورة تتغير ونحن نرى ان هناك لاعبين رئيسي..."
]
},
"execution_count": 78,
"metadata": {
"tags": []
},
"output_type": "execute_result"
}
],
"source": [
"## remove the Nan Column + display head\n",
"df_ted = df_ted[[\"en\",'ar']]\n",
"df_ted.head()\n",
"df_ted.columns = [\"source_sentence\", \"target_sentence\"]\n",
"copy_ted = df_ted \n",
"\n",
"copy_ted.head()"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "CHq8EjYv0JAt"
},
"source": [
"## Merging Tedtalk and JW300"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "MikpwH_I0JAv"
},
"outputs": [],
"source": [
"df = copy_df.append(copy_ted, ignore_index=True)\n",
"df.shape\n",
"df.to_csv(\"full_data.csv\")"
]
},
{
"cell_type": "code",
"execution_count": 108,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 119
},
"colab_type": "code",
"id": "bwMBTYxzTkac",
"outputId": "9eaffe51-30d5-4abc-84d8-7058af04a21b"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"repeated values describtion \n"
]
},
{
"data": {
"text/plain": [
"count 765188\n",
"unique 2\n",
"top False\n",
"freq 732429\n",
"dtype: object"
]
},
"execution_count": 108,
"metadata": {
"tags": []
},
"output_type": "execute_result"
}
],
"source": [
"find_dups = df.duplicated(subset=None, keep='first')\n",
"print(\"repeated values describtion \")\n",
"find_dups.describe()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ratio of non repateated entries in the two datasets "
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"id": "jwkN-BaFUMkg",
"outputId": "76f7da45-8aae-437c-b855-688971a6bff0"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ratio of un-repeated values = 0.9571882988232957\n"
]
}
],
"source": [
"\n",
"output = find_dups.describe()\n",
"ratio = output.freq/ len(find_dups)\n",
"print(\"ratio of un-repeated values = \", ratio)\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "YkuK3B4p2AkN"
},
"source": [
"## Pre-processing and export\n",
"\n",
"It is generally a good idea to remove duplicate translations and conflicting translations from the corpus. In practice, these public corpora include some number of these that need to be cleaned.\n",
"\n",
"In addition we will split our data into dev/test/train and export to the filesystem."
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "M_2ouEOH1_1q"
},
"outputs": [],
"source": [
"# drop duplicate translations\n",
"df = df.drop_duplicates()\n",
"\n",
"# drop conflicting translations\n",
"# (this is optional and something that you might want to comment out \n",
"# depending on the size of your corpus)\n",
"# df_pp.drop_duplicates(subset='source_sentence', inplace=True)\n",
"# df_pp.drop_duplicates(subset='target_sentence', inplace=True)\n",
"\n",
"# Shuffle the data to remove bias in dev set selection.\n",
"df = df.sample(frac=1, random_state=seed).reset_index(drop=True)\n",
"df.shape"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "SdJ8ONao1bq6"
},
"outputs": [],
"source": [
"#GET THE TEST DATA FROM THE TOTAL DATAFRAME\n",
"df_test = df.loc[:3751,:]\n",
"df_train = df.loc[3751:,:]\n",
"print(df_test.shape, df_train.shape)\n",
"en_test_sents = df_test.source_sentence.tolist()"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "Z_1BwAApEtMk"
},
"outputs": [],
"source": [
"# Install fuzzy wuzzy to remove \"almost duplicate\" sentences in the\n",
"# test and training sets.\n",
"! pip install fuzzywuzzy\n",
"! pip install python-Levenshtein\n",
"import time\n",
"from fuzzywuzzy import process\n",
"import numpy as np\n",
"\n",
"# reset the index of the training set after previous filtering\n",
"df_train.reset_index(drop=False, inplace=True)\n",
"\n",
"# Remove samples from the training data set if they \"almost overlap\" with the\n",
"# samples in the test set.\n",
"\n",
"# Filtering function. Adjust pad to narrow down the candidate matches to\n",
"# within a certain length of characters of the given sample.\n",
"def fuzzfilter(sample, candidates, pad):\n",
" candidates = [x for x in candidates if len(x) <= len(sample)+pad and len(x) >= len(sample)-pad] \n",
" if len(candidates) > 0:\n",
" return process.extractOne(sample, candidates)[1]\n",
" else:\n",
" return np.nan\n",
"\n",
"# NOTE - This might run slow depending on the size of your training set. We are\n",
"# printing some information to help you track how long it would take. \n",
"scores = []\n",
"start_time = time.time()\n",
"for idx, row in df_train.iterrows():\n",
" scores.append(fuzzfilter(row['source_sentence'], en_test_sents, 5))\n",
" if idx % 1000 == 0:\n",
" hours, rem = divmod(time.time() - start_time, 3600)\n",
" minutes, seconds = divmod(rem, 60)\n",
" print(\"{:0>2}:{:0>2}:{:05.2f}\".format(int(hours),int(minutes),seconds), \"%0.2f percent complete\" % (100.0*float(idx)/float(len(df_train))))\n",
"\n",
"# Filter out \"almost overlapping samples\"\n",
"df_train['scores'] = scores\n",
"df_train = df_train[df_train['scores'] < 95]"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "hxxBOCA-xXhy"
},
"outputs": [],
"source": [
"# This section does the split between train/dev for the parallel corpora then saves them as separate files\n",
"# We use 1000 dev test and the given test set.\n",
"import csv\n",
"\n",
"# Do the split between dev/train and create parallel corpora\n",
"num_dev_patterns = 1000\n",
"\n",
"# Optional: lower case the corpora - this will make it easier to generalize, but without proper casing.\n",
"if lc: # Julia: making lowercasing optional\n",
" df_train[\"source_sentence\"] = df_train[\"source_sentence\"].str.lower()\n",
" df_train[\"target_sentence\"] = df_train[\"target_sentence\"].str.lower()\n",
"\n",
"# Julia: test sets are already generated\n",
"dev = df_train.tail(num_dev_patterns) # Herman: Error in original\n",
"stripped = df_train.drop(df_train.tail(num_dev_patterns).index)\n",
"\n",
"with open(\"train.\"+source_language, \"w\") as src_file, open(\"train.\"+target_language, \"w\") as trg_file:\n",
" for index, row in stripped.iterrows():\n",
" if type(row[\"source_sentence\"]) == str:\n",
" print(row[\"source_sentence\"]+\"\\n\", file=src_file)\n",
" else:\n",
" continue\n",
" if type(row[\"target_sentence\"]) == str:\n",
" print(row[\"target_sentence\"]+\"\\n\", file=trg_file)\n",
" else:\n",
" continue\n",
" \n",
"with open(\"dev.\"+source_language, \"w\") as src_file, open(\"dev.\"+target_language, \"w\") as trg_file:\n",
" for index, row in dev.iterrows():\n",
" if type(row[\"source_sentence\"]) == str:\n",
" print(row[\"source_sentence\"]+\"\\n\",file=src_file)\n",
" else:\n",
" continue\n",
" \n",
" if type(row[\"target_sentence\"]) == str:\n",
" print(row[\"target_sentence\"]+\"\\n\",file=trg_file)\n",
" else:\n",
" continue\n",
"\n",
"#stripped[[\"source_sentence\"]].to_csv(\"train.\"+source_language, header=False, index=False) # Herman: Added `header=False` everywhere\n",
"#stripped[[\"target_sentence\"]].to_csv(\"train.\"+target_language, header=False, index=False) # Julia: Problematic handling of quotation marks.\n",
"\n",
"#dev[[\"source_sentence\"]].to_csv(\"dev.\"+source_language, header=False, index=False)\n",
"#dev[[\"target_sentence\"]].to_csv(\"dev.\"+target_language, header=False, index=False)\n",
"\n",
"# Doublecheck the format below. There should be no extra quotation marks or weird characters.\n",
"! head train.*\n",
"! head dev.*"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "epeCydmCyS8X"
},
"source": [
"\n",
"\n",
"---\n",
"\n",
"\n",
"## Installation of JoeyNMT\n",
"\n",
"JoeyNMT is a simple, minimalist NMT package which is useful for learning and teaching. Check out the documentation for JoeyNMT [here](https://joeynmt.readthedocs.io) "
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "iBRMm4kMxZ8L"
},
"outputs": [],
"source": [
"# Install JoeyNMT\n",
"! git clone https://github.com/joeynmt/joeynmt.git\n",
"! cd joeynmt; pip install ."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "AaE77Tcppex9"
},
"source": [
"# Preprocessing the Data into Subword BPE Tokens\n",
"\n",
"- One of the most powerful improvements for agglutinative languages (a feature of most Bantu languages) is using BPE tokenization [ (Sennrich, 2015) ](https://arxiv.org/abs/1508.07909).\n",
"\n",
"- It was also shown that by optimizing the umber of BPE codes we significantly improve results for low-resourced languages [(Sennrich, 2019)](https://www.aclweb.org/anthology/P19-1021) [(Martinus, 2019)](https://arxiv.org/abs/1906.05685)\n",
"\n",
"- Below we have the scripts for doing BPE tokenization of our data. We use 4000 tokens as recommended by [(Sennrich, 2019)](https://www.aclweb.org/anthology/P19-1021). You do not need to change anything. Simply running the below will be suitable. "
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "H-TyjtmXB1mL"
},
"outputs": [],
"source": [
"# One of the huge boosts in NMT performance was to use a different method of tokenizing. \n",
"# Usually, NMT would tokenize by words. However, using a method called BPE gave amazing boosts to performance\n",
"\n",
"# Do subword NMT\n",
"from os import path\n",
"os.environ[\"src\"] = source_language # Sets them in bash as well, since we often use bash scripts\n",
"os.environ[\"tgt\"] = target_language\n",
"\n",
"# Learn BPEs on the training data.\n",
"os.environ[\"data_path\"] = path.join(\"joeynmt\", \"data\", source_language + target_language) # Herman! \n",
"! subword-nmt learn-joint-bpe-and-vocab --input train.$src train.$tgt -s 4000 -o bpe.codes.4000 --write-vocabulary vocab.$src vocab.$tgt\n",
"\n",
"# Apply BPE splits to the development and test data.\n",
"! subword-nmt apply-bpe -c bpe.codes.4000 --vocabulary vocab.$src < train.$src > train.bpe.$src\n",
"! subword-nmt apply-bpe -c bpe.codes.4000 --vocabulary vocab.$tgt < train.$tgt > train.bpe.$tgt\n",
"\n",
"! subword-nmt apply-bpe -c bpe.codes.4000 --vocabulary vocab.$src < dev.$src > dev.bpe.$src\n",
"! subword-nmt apply-bpe -c bpe.codes.4000 --vocabulary vocab.$tgt < dev.$tgt > dev.bpe.$tgt\n",
"! subword-nmt apply-bpe -c bpe.codes.4000 --vocabulary vocab.$src < test.$src > test.bpe.$src\n",
"! subword-nmt apply-bpe -c bpe.codes.4000 --vocabulary vocab.$tgt < test.$tgt > test.bpe.$tgt\n",
"\n",
"# Create directory, move everyone we care about to the correct location\n",
"! mkdir -p $data_path\n",
"! cp train.* $data_path\n",
"! cp test.* $data_path\n",
"! cp dev.* $data_path\n",
"! cp bpe.codes.4000 $data_path\n",
"! ls $data_path\n",
"\n",
"# Also move everything we care about to a mounted location in google drive (relevant if running in colab) at gdrive_path\n",
"! cp train.* \"$gdrive_path\"\n",
"! cp test.* \"$gdrive_path\"\n",
"! cp dev.* \"$gdrive_path\"\n",
"! cp bpe.codes.4000 \"$gdrive_path\"\n",
"! ls \"$gdrive_path\"\n",
"\n",
"# Create that vocab using build_vocab\n",
"! chmod 777 joeynmt/scripts/build_vocab.py\n",
"! joeynmt/scripts/build_vocab.py joeynmt/data/$src$tgt/train.bpe.$src joeynmt/data/$src$tgt/train.bpe.$tgt --output_path joeynmt/data/$src$tgt/vocab.txt\n",
"\n",
"# Some output\n",
"! echo \"BPE Arabic Sentences\"\n",
"! tail -n 5 test.bpe.$tgt\n",
"! echo \"Combined BPE Vocab\"\n",
"! tail -n 10 joeynmt/data/$src$tgt/vocab.txt # Herman"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "IlMitUHR8Qy-"
},
"outputs": [],
"source": [
"# Also move everything we care about to a mounted location in google drive (relevant if running in colab) at gdrive_path\n",
"! cp train.* \"$gdrive_path\"\n",
"! cp test.* \"$gdrive_path\"\n",
"! cp dev.* \"$gdrive_path\"\n",
"! cp bpe.codes.4000 \"$gdrive_path\"\n",
"! ls \"$gdrive_path\""
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "Ixmzi60WsUZ8"
},
"source": [
"# Creating the JoeyNMT Config\n",
"\n",
"JoeyNMT requires a yaml config. We provide a template below. We've also set a number of defaults with it, that you may play with!\n",
"\n",
"- We used Transformer architecture \n",
"- We set our dropout to reasonably high: 0.3 (recommended in [(Sennrich, 2019)](https://www.aclweb.org/anthology/P19-1021))\n",
"\n",
"Things worth playing with:\n",
"- The batch size (also recommended to change for low-resourced languages)\n",
"- The number of epochs (we've set it at 30 just so it runs in about an hour, for testing purposes)\n",
"- The decoder options (beam_size, alpha)\n",
"- Evaluation metrics (BLEU versus Crhf4)"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "PIs1lY2hxMsl"
},
"outputs": [],
"source": [
"# This creates the config file for our JoeyNMT system. It might seem overwhelming so we've provided a couple of useful parameters you'll need to update\n",
"# (You can of course play with all the parameters if you'd like!)\n",
"\n",
"name = '%s%s' % (source_language, target_language)\n",
"gdrive_path = os.environ[\"gdrive_path\"]\n",
"\n",
"# Create the config\n",
"config = \"\"\"\n",
"name: \"{name}_transformer\"\n",
"\n",
"data:\n",
" src: \"{source_language}\"\n",
" trg: \"{target_language}\"\n",
" train: \"data/{name}/train.bpe\"\n",
" dev: \"data/{name}/dev.bpe\"\n",
" test: \"data/{name}/test.bpe\"\n",
" level: \"bpe\"\n",
" lowercase: False\n",
" max_sent_length: 100\n",
" src_vocab: \"data/{name}/vocab.txt\"\n",
" trg_vocab: \"data/{name}/vocab.txt\"\n",
"\n",
"testing:\n",
" beam_size: 5\n",
" alpha: 1.0\n",
"\n",
"training:\n",
" #load_model: \"{gdrive_path}/models/{name}_transformer/1.ckpt\" # if uncommented, load a pre-trained model from this checkpoint\n",
" random_seed: 42\n",
" optimizer: \"adam\"\n",
" normalization: \"tokens\"\n",
" adam_betas: [0.9, 0.999] \n",
" scheduling: \"plateau\" # TODO: try switching from plateau to Noam scheduling\n",
" patience: 5 # For plateau: decrease learning rate by decrease_factor if validation score has not improved for this many validation rounds.\n",
" learning_rate_factor: 0.5 # factor for Noam scheduler (used with Transformer)\n",
" learning_rate_warmup: 1000 # warmup steps for Noam scheduler (used with Transformer)\n",
" decrease_factor: 0.7\n",
" loss: \"crossentropy\"\n",
" learning_rate: 0.0003\n",
" learning_rate_min: 0.00000001\n",
" weight_decay: 0.0\n",
" label_smoothing: 0.1\n",
" batch_size: 1000\n",
" batch_type: \"token\"\n",
" eval_batch_size: 3570\n",
" eval_batch_type: \"token\"\n",
" batch_multiplier: 1\n",
" early_stopping_metric: \"ppl\"\n",
" epochs: 30 # TODO: Decrease for when playing around and checking of working. Around 30 is sufficient to check if its working at all\n",
" validation_freq: 60800 # TODO: Set to at least once per epoch.\n",
" logging_freq: 100\n",
" eval_metric: \"bleu\"\n",
" model_dir: \"models/{name}_transformer\"\n",
" overwrite: False # TODO: Set to True if you want to overwrite possibly existing models. \n",
" shuffle: True\n",
" use_cuda: True\n",
" max_output_length: 100\n",
" print_valid_sents: [0, 1, 2, 3]\n",
" keep_last_ckpts: 3\n",
"\n",
"model:\n",
" initializer: \"xavier\"\n",
" bias_initializer: \"zeros\"\n",
" init_gain: 1.0\n",
" embed_initializer: \"xavier\"\n",
" embed_init_gain: 1.0\n",
" tied_embeddings: True\n",
" tied_softmax: True\n",
" encoder:\n",
" type: \"transformer\"\n",
" num_layers: 6\n",
" num_heads: 4 # TODO: Increase to 8 for larger data.\n",
" embeddings:\n",
" embedding_dim: 256 # TODO: Increase to 512 for larger data.\n",
" scale: True\n",
" dropout: 0.2\n",
" # typically ff_size = 4 x hidden_size\n",
" hidden_size: 256 # TODO: Increase to 512 for larger data.\n",
" ff_size: 1024 # TODO: Increase to 2048 for larger data.\n",
" dropout: 0.3\n",
" decoder:\n",
" type: \"transformer\"\n",
" num_layers: 6\n",
" num_heads: 4 # TODO: Increase to 8 for larger data.\n",
" embeddings:\n",
" embedding_dim: 256 # TODO: Increase to 512 for larger data.\n",
" scale: True\n",
" dropout: 0.2\n",
" # typically ff_size = 4 x hidden_size\n",
" hidden_size: 256 # TODO: Increase to 512 for larger data.\n",
" ff_size: 1024 # TODO: Increase to 2048 for larger data.\n",
" dropout: 0.3\n",
"\"\"\".format(name=name, gdrive_path=os.environ[\"gdrive_path\"], source_language=source_language, target_language=target_language)\n",
"with open(\"joeynmt/configs/transformer_{name}.yaml\".format(name=name),'w') as f:\n",
" f.write(config)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "pIifxE3Qzuvs"
},
"source": [
"# Train the Model\n",
"\n",
"This single line of joeynmt runs the training using the config we made above"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "6ZBPFwT94WpI"
},
"outputs": [],
"source": [
"# Train the model\n",
"# You can press Ctrl-C to stop. And then run the next cell to save your checkpoints! \n",
"!cd joeynmt; python3 -m joeynmt train configs/transformer_$src$tgt.yaml"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "MBoDS09JM807"
},
"outputs": [],
"source": [
"# Copy the created models from the notebook storage to google drive for persistant storage \n",
"!cp -r joeynmt/models/${src}${tgt}_transformer/* \"$gdrive_path/models/${src}${tgt}_transformer/\""
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "n94wlrCjVc17"
},
"outputs": [],
"source": [
"# Output our validation accuracy\n",
"! tail \"$gdrive_path/models/${src}${tgt}_transformer/validations.txt\""
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "66WhRE9lIhoD"
},
"outputs": [],
"source": [
"# Test our model\n",
"! cd joeynmt; python3 -m joeynmt test \"$gdrive_path/models/${src}${tgt}_transformer/config.yaml\""
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"collapsed_sections": [],
"name": "notebook.ipynb",
"provenance": []
},
"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.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
|