{ "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": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "id": "oGRmDELn7Az0", "outputId": "5dfcaf0a-4e8d-4f84-a1fa-42940b6f2252" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\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 = \"fon\" \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", "os.environ[\"gdrive_path\"] = \"/content/drive/My Drive/masakhane/%s-%s-%s\" % (source_language, target_language, tag)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "id": "kBSgJHEw7Nvx", "outputId": "f006df06-8d99-432e-d9f5-1b2227b2d1f6" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/content/drive/My Drive/masakhane/en-fon-baseline\n" ] } ], "source": [ "!echo $gdrive_path" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 102 }, "colab_type": "code", "id": "gA75Fs9ys8Y9", "outputId": "93dc8f60-2de8-45ba-9bb5-0afd9cf3b358" }, "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 25.2MB/s eta 0:00:01\r", "\u001b[K |████████ | 20kB 4.5MB/s eta 0:00:01\r", "\u001b[K |████████████▏ | 30kB 6.3MB/s eta 0:00:01\r", "\u001b[K |████████████████▏ | 40kB 8.1MB/s eta 0:00:01\r", "\u001b[K |████████████████████▎ | 51kB 5.2MB/s eta 0:00:01\r", "\u001b[K |████████████████████████▎ | 61kB 6.1MB/s eta 0:00:01\r", "\u001b[K |████████████████████████████▎ | 71kB 6.9MB/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": 0, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 187 }, "colab_type": "code", "id": "xq-tDZVks7ZD", "outputId": "e7343c5b-3bc2-4979-8fb3-b75b5a025164" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Alignment file /proj/nlpl/data/OPUS/JW300/latest/xml/en-fon.xml.gz not found. The following files are available for downloading:\n", "\n", " ./JW300_latest_xml_fon.zip already exists\n", " 324 KB https://object.pouta.csc.fi/OPUS-JW300/v1/xml/en-fon.xml.gz\n", " 263 MB https://object.pouta.csc.fi/OPUS-JW300/v1/xml/en.zip\n", "\n", " 263 MB Total size\n", "./JW300_latest_xml_en-fon.xml.gz ... 100% of 324 KB\n", "./JW300_latest_xml_en.zip ... 100% of 263 MB\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": 0, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 578 }, "colab_type": "code", "id": "n48GDRnP8y2G", "outputId": "ed67a5b7-a1db-4d8b-bb37-58111706be8a" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--2019-11-26 13:32:06-- 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", "2019-11-26 13:32:06 (12.5 MB/s) - ‘test.en-any.en’ saved [277791/277791]\n", "\n", "--2019-11-26 13:32:09-- https://raw.githubusercontent.com/juliakreutzer/masakhane/master/jw300_utils/test/test.en-fon.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: 206191 (201K) [text/plain]\n", "Saving to: ‘test.en-fon.en’\n", "\n", "test.en-fon.en 100%[===================>] 201.36K --.-KB/s in 0.02s \n", "\n", "2019-11-26 13:32:10 (11.5 MB/s) - ‘test.en-fon.en’ saved [206191/206191]\n", "\n", "--2019-11-26 13:32:16-- https://raw.githubusercontent.com/juliakreutzer/masakhane/master/jw300_utils/test/test.en-fon.fon\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: 266332 (260K) [text/plain]\n", "Saving to: ‘test.en-fon.fon’\n", "\n", "test.en-fon.fon 100%[===================>] 260.09K --.-KB/s in 0.02s \n", "\n", "2019-11-26 13:32:17 (11.2 MB/s) - ‘test.en-fon.fon’ saved [266332/266332]\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": 0, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "id": "NqDG-CI28y2L", "outputId": "1cae02eb-e883-4be6-a4ae-a55de11f0b8a" }, "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": 0, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 159 }, "colab_type": "code", "id": "3CNdwLBCfSIl", "outputId": "a8139b78-0751-4893-91dc-306662889805" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loaded data and skipped 4199/34746 lines since contained in test set.\n" ] }, { "data": { "text/html": [ "
\n", " | source_sentence | \n", "target_sentence | \n", "
---|---|---|
0 | \n", "While reviewing a financial account with his s... | \n", "Hwenu e é ɖò akwɛzinzan ɖé lɛ́n xá gǎn tɔn wɛ ... | \n", "
1 | \n", "The interviewer responded , “ Your way of thin... | \n", "Mɛ e ɖò nǔ kanbyɔ ɛ wɛ é ɖɔ n’i ɖɔ : “ Linlin ... | \n", "
2 | \n", "One form of dishonesty that is particularly wi... | \n", "Nugbǒmaɖɔ sín alɔkpa e gbakpé tawun ɖò égbé lɛ... | \n", "