{ "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 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": {}, "colab_type": "code", "id": "oGRmDELn7Az0" }, "outputs": [], "source": [ "\"\"\"from google.colab import drive\n", "drive.mount('/content/drive')\"\"\"" ] }, { "cell_type": "code", "execution_count": 1, "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 = \"pcm\" \n", "lc = False # If True, lowercase the data.\n", "seed = 42 # Random seed for shuffling.\n", "tag = \"jw300-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", "\n", "# This will save it to a folder in our gdrive instead!\n", "!mkdir -p \"en_pcm/$src-$tgt-$tag\"\n", "os.environ[\"experiment_path\"] = \"en_pcm/%s-%s-%s\" % (source_language, target_language, tag)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "!mkdir -p \"en_pcm/$src-$tgt-$tag\"" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "colab": {}, "colab_type": "code", "id": "kBSgJHEw7Nvx" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "en_pcm/en-pcm-jw300-baseline\r\n" ] } ], "source": [ "!echo $experiment_path" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "colab": {}, "colab_type": "code", "id": "gA75Fs9ys8Y9" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: opustools-pkg in /home/ec2-user/anaconda3/envs/python3/lib/python3.6/site-packages (0.0.52)\n", "\u001b[33mYou are using pip version 10.0.1, however version 20.0.2 is available.\n", "You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n" ] } ], "source": [ "# Install opus-tools\n", "! pip install opustools-pkg" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "colab": {}, "colab_type": "code", "id": "xq-tDZVks7ZD" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Alignment file /proj/nlpl/data/OPUS/JW300/latest/xml/en-pcm.xml.gz not found. The following files are available for downloading:\n", "\n", " 256 KB https://object.pouta.csc.fi/OPUS-JW300/v1/xml/en-pcm.xml.gz\n", " 263 MB https://object.pouta.csc.fi/OPUS-JW300/v1/xml/en.zip\n", " 3 MB https://object.pouta.csc.fi/OPUS-JW300/v1/xml/pcm.zip\n", "\n", " 266 MB Total size\n", "./JW300_latest_xml_en-pcm.xml.gz ... 100% of 256 KB\n", "./JW300_latest_xml_en.zip ... 100% of 263 MB\n", "./JW300_latest_xml_pcm.zip ... 100% of 3 MB\n", "gzip: unrecognized option '--y'\n", "Try `gzip --help' for more information.\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 --y" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "colab": {}, "colab_type": "code", "id": "n48GDRnP8y2G" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--2020-02-09 12:37:41-- https://raw.githubusercontent.com/juliakreutzer/masakhane/master/jw300_utils/test/test.en-any.en\n", "Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 199.232.24.133\n", "Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|199.232.24.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", "test.en-any.en 100%[===================>] 271.28K --.-KB/s in 0.008s \n", "\n", "2020-02-09 12:37:41 (35.2 MB/s) - ‘test.en-any.en’ saved [277791/277791]\n", "\n", "--2020-02-09 12:37:41-- https://raw.githubusercontent.com/juliakreutzer/masakhane/master/jw300_utils/test/test.en-pcm.en\n", "Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 199.232.24.133\n", "Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|199.232.24.133|:443... connected.\n", "HTTP request sent, awaiting response... 200 OK\n", "Length: 154369 (151K) [text/plain]\n", "Saving to: ‘test.en-pcm.en’\n", "\n", "test.en-pcm.en 100%[===================>] 150.75K --.-KB/s in 0.005s \n", "\n", "2020-02-09 12:37:42 (29.3 MB/s) - ‘test.en-pcm.en’ saved [154369/154369]\n", "\n", "--2020-02-09 12:37:42-- https://raw.githubusercontent.com/juliakreutzer/masakhane/master/jw300_utils/test/test.en-pcm.pcm\n", "Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 199.232.24.133\n", "Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|199.232.24.133|:443... connected.\n", "HTTP request sent, awaiting response... 200 OK\n", "Length: 163323 (159K) [text/plain]\n", "Saving to: ‘test.en-pcm.pcm’\n", "\n", "test.en-pcm.pcm 100%[===================>] 159.50K --.-KB/s in 0.005s \n", "\n", "2020-02-09 12:37:42 (33.7 MB/s) - ‘test.en-pcm.pcm’ saved [163323/163323]\n", "\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": {}, "colab_type": "code", "id": "NqDG-CI28y2L" }, "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": {}, "colab_type": "code", "id": "3CNdwLBCfSIl" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loaded data and skipped 2536/26020 lines since contained in test set.\n" ] }, { "data": { "text/html": [ "
\n", " | source_sentence | \n", "target_sentence | \n", "
---|---|---|
0 | \n", "3 Settle Differences in a Spirit of Love | \n", "3 Make Una Dey Use Love Settle Quarrel | \n", "
1 | \n", "Because of our inherited imperfection , we are... | \n", "Because of the sin wey all of us carry from be... | \n", "
2 | \n", "This article shows how Bible principles can be... | \n", "This topic go show us how we fit let the thing... | \n", "