{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "view-in-github" }, "source": [ "" ] }, { "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 masakhanetranslation@gmail.com\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": 0, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 122 }, "colab_type": "code", "id": "oGRmDELn7Az0", "outputId": "ccea5c09-bc5a-4a84-9818-b4271b72dc38" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "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": [ "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 = \"efi\" \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 \"/content/drive/My Drive/masakhane/$src-$tgt-$tag\"\n", "g_drive_path = \"/content/drive/My Drive/masakhane/%s-%s-%s\" % (source_language, target_language, tag)\n", "os.environ[\"gdrive_path\"] = g_drive_path\n", "models_path = '%s/models/%s%s_transformer'% (g_drive_path, source_language, target_language)\n", "# model temporary directory for training\n", "model_temp_dir = \"/content/drive/My Drive/masakhane/model-temp\"\n", "# model permanent storage on the drive\n", "!mkdir -p \"$gdrive_path/models/${src}${tgt}_transformer/\"" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "id": "kBSgJHEw7Nvx", "outputId": "a3167fc9-7bfb-44c1-e0b2-6232350a7e20" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/content/drive/My Drive/masakhane/en-efi-baseline\n" ] } ], "source": [ "!echo $gdrive_path" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "id": "gA75Fs9ys8Y9", "outputId": "4286ba7f-2e11-4366-e034-abdb843c5593" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: opustools-pkg in /usr/local/lib/python3.6/dist-packages (0.0.52)\n" ] } ], "source": [ "#TODO: Skip for retrain\n", "# Install opus-tools\n", "! pip install opustools-pkg " ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 221 }, "colab_type": "code", "id": "xq-tDZVks7ZD", "outputId": "724f71b4-2db9-486e-93d2-56d2f3d495bc" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Alignment file /proj/nlpl/data/OPUS/JW300/latest/xml/efi-en.xml.gz not found. The following files are available for downloading:\n", "\n", " 3 MB https://object.pouta.csc.fi/OPUS-JW300/v1/xml/efi-en.xml.gz\n", " 36 MB https://object.pouta.csc.fi/OPUS-JW300/v1/xml/efi.zip\n", " 263 MB https://object.pouta.csc.fi/OPUS-JW300/v1/xml/en.zip\n", "\n", " 303 MB Total size\n", "./JW300_latest_xml_efi-en.xml.gz ... 100% of 3 MB\n", "./JW300_latest_xml_efi.zip ... 100% of 36 MB\n", "./JW300_latest_xml_en.zip ... 100% of 263 MB\n", "gzip: JW300_latest_xml_en-efi.xml.gz: No such file or directory\n" ] } ], "source": [ "#TODO: Skip for retrain\n", "# 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": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "j2K6QK2NOaUX" }, "outputs": [], "source": [ "# extract the corpus file\n", "! gunzip JW300_latest_xml_$tgt-$src.xml.gz" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 578 }, "colab_type": "code", "id": "n48GDRnP8y2G", "outputId": "3c765279-6999-4977-c553-17cc87982fc0" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--2020-04-07 10:41:12-- 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.1s \n", "\n", "2020-04-07 10:41:13 (2.35 MB/s) - ‘test.en-any.en’ saved [277791/277791]\n", "\n", "--2020-04-07 10:41:15-- https://raw.githubusercontent.com/juliakreutzer/masakhane/master/jw300_utils/test/test.en-efi.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: 203603 (199K) [text/plain]\n", "Saving to: ‘test.en-efi.en’\n", "\n", "test.en-efi.en 100%[===================>] 198.83K --.-KB/s in 0.09s \n", "\n", "2020-04-07 10:41:16 (2.07 MB/s) - ‘test.en-efi.en’ saved [203603/203603]\n", "\n", "--2020-04-07 10:41:20-- https://raw.githubusercontent.com/juliakreutzer/masakhane/master/jw300_utils/test/test.en-efi.efi\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: 229202 (224K) [text/plain]\n", "Saving to: ‘test.en-efi.efi’\n", "\n", "test.en-efi.efi 100%[===================>] 223.83K --.-KB/s in 0.1s \n", "\n", "2020-04-07 10:41:20 (2.19 MB/s) - ‘test.en-efi.efi’ saved [229202/229202]\n", "\n" ] } ], "source": [ "#TODO: Skip for retrain\n", "# 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": 0, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "id": "NqDG-CI28y2L", "outputId": "ae596401-d6d3-4bb0-84d1-b2955c623cf1" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loaded 3571 global test sentences to filter from the training/dev data.\n" ] } ], "source": [ "#TODO: Skip for retrain\n", "# 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": 0, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 159 }, "colab_type": "code", "id": "3CNdwLBCfSIl", "outputId": "51262d44-631c-494d-e3e8-c8547e74b8d9" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loaded data and skipped 6113/377824 lines since contained in test set.\n" ] }, { "data": { "text/html": [ "
\n", " | source_sentence | \n", "target_sentence | \n", "
---|---|---|
0 | \n", "© 2013 Watch Tower Bible and Tract Society of ... | \n", "© 2013 Watch Tower Bible and Tract Society of ... | \n", "
1 | \n", "All rights reserved . | \n", "All rights reserved . | \n", "
2 | \n", "3 Watching the World | \n", "3 Se Itịbede ke Ererimbot | \n", "