{ "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": 24, "metadata": { "colab": {}, "colab_type": "code", "collapsed": true, "id": "oGRmDELn7Az0" }, "outputs": [], "source": [ "# from google.colab import drive\n", "# drive.mount('/content/drive')" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "colab": {}, "colab_type": "code", "collapsed": true, "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 = \"luo\" \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": 26, "metadata": { "colab": {}, "colab_type": "code", "collapsed": true, "id": "kBSgJHEw7Nvx" }, "outputs": [], "source": [ "# !echo $gdrive_path" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "colab": {}, "colab_type": "code", "collapsed": true, "id": "gA75Fs9ys8Y9" }, "outputs": [], "source": [ "# Install opus-tools\n", "#! pip install opustools-pkg" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Uncomment cell below if notebook is being run for the first time and you need to download the data" ] }, { "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-luo.xml.gz not found. The following files are available for downloading:\n", "\n", " 1 MB https://object.pouta.csc.fi/OPUS-JW300/v1/xml/en-luo.xml.gz\n", " 263 MB https://object.pouta.csc.fi/OPUS-JW300/v1/xml/en.zip\n", " 14 MB https://object.pouta.csc.fi/OPUS-JW300/v1/xml/luo.zip\n", "\n", " 279 MB Total size\n", "./JW300_latest_xml_en-luo.xml.gz ... 100% of 1 MB\n", "./JW300_latest_xml_en.zip ... 100% of 263 MB\n", "./JW300_latest_xml_luo.zip ... 100% of 14 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": 6, "metadata": { "colab": {}, "colab_type": "code", "id": "n48GDRnP8y2G" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--2020-02-19 06:24:03-- https://raw.githubusercontent.com/juliakreutzer/masakhane/master/jw300_utils/test/test.en-any.en\n", "Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.192.133, 151.101.128.133, 151.101.64.133, ...\n", "Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.192.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.04s \n", "\n", "2020-02-19 06:24:04 (6.13 MB/s) - ‘test.en-any.en’ saved [277791/277791]\n", "\n", "--2020-02-19 06:24:04-- https://raw.githubusercontent.com/juliakreutzer/masakhane/master/jw300_utils/test/test.en-luo.en\n", "Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.192.133, 151.101.128.133, 151.101.64.133, ...\n", "Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.192.133|:443... connected.\n", "HTTP request sent, awaiting response... 200 OK\n", "Length: 205584 (201K) [text/plain]\n", "Saving to: ‘test.en-luo.en’\n", "\n", "test.en-luo.en 100%[===================>] 200.77K --.-KB/s in 0.04s \n", "\n", "2020-02-19 06:24:04 (5.24 MB/s) - ‘test.en-luo.en’ saved [205584/205584]\n", "\n", "--2020-02-19 06:24:04-- https://raw.githubusercontent.com/juliakreutzer/masakhane/master/jw300_utils/test/test.en-luo.luo\n", "Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.192.133, 151.101.128.133, 151.101.64.133, ...\n", "Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.192.133|:443... connected.\n", "HTTP request sent, awaiting response... 200 OK\n", "Length: 209877 (205K) [text/plain]\n", "Saving to: ‘test.en-luo.luo’\n", "\n", "test.en-luo.luo 100%[===================>] 204.96K --.-KB/s in 0.04s \n", "\n", "2020-02-19 06:24:05 (4.95 MB/s) - ‘test.en-luo.luo’ saved [209877/209877]\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": 28, "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": 29, "metadata": { "colab": {}, "colab_type": "code", "id": "3CNdwLBCfSIl" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loaded data and skipped 4654/154941 lines since contained in test set.\n" ] }, { "data": { "text/html": [ "
\n", " | source_sentence | \n", "target_sentence | \n", "
---|---|---|
0 | \n", "Compassion in a Cruel World | \n", "Kecho Ji e Piny ma Ji Ok Dew Jowetegi | \n", "
1 | \n", "A MAN in Burundi falls seriously ill with mala... | \n", "MALARIA mager ogoyo dichwo moro e piny Burundi . | \n", "
2 | \n", "He urgently needs to be transferred to a hospi... | \n", "Dwarore otere e osiptal mapiyo ahinya . | \n", "