{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "dc2b231b",
   "metadata": {},
   "source": [
    "The old version"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9b7ab118",
   "metadata": {},
   "outputs": [],
   "source": [
    "from selkie.langs import languages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0b6c813c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Living Language otw 'Ottawa'>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "languages['otw']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d999bbd9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Code:      otw                                \n",
      "Type:      Living                             \n",
      "Scope:     Language                           \n",
      "RefName:   Ottawa                             \n",
      "Parent:    <Living Macrolanguage oji 'Ojibwa'>\n",
      "----------------------------------------------\n",
      "Varieties:                                    \n"
     ]
    }
   ],
   "source": [
    "print(languages['otw'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04f17d1f",
   "metadata": {},
   "source": [
    "The code table:\n",
    "\n",
    "  * Id - The three-letter 639-3 identifier\n",
    "  * Part2B - Equivalent 639-2 identifier of the bibliographic applications code set, if there is one\n",
    "  * Part2T - Equivalent 639-2 identifier of the terminology applications code set, if there is one\n",
    "  * Part1 - Equivalent 639-1 identifier, if there is one    \n",
    "  * Scope - I(ndividual), M(acrolanguage), S(pecial)\n",
    "  * Type - A(ncient), C(onstructed), E(xtinct), H(istorical), L(iving), S(pecial)\n",
    "  * Ref_Name - Reference language name \n",
    "  * Comment - Comment relating to one or more of the columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5037fa28",
   "metadata": {},
   "outputs": [],
   "source": [
    "iso_table_fn = 'https://iso639-3.sil.org/sites/iso639-3/files/downloads/iso-639-3.tab'\n",
    "\n",
    "iso_macro_fn = 'https://iso639-3.sil.org/sites/iso639-3/files/downloads/iso-639-3-macrolanguages.tab'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "029851aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "0e92348e",
   "metadata": {},
   "outputs": [],
   "source": [
    "class ISOLanguageCode (object):\n",
    "    \n",
    "    def __init__ (self, iso3, iso2b, iso2t, iso1, scope, lgtype, name, comment):\n",
    "        self.iso3 = iso3\n",
    "        self.iso2b = iso2b\n",
    "        self.iso2t = iso2t\n",
    "        self.iso1 = iso1\n",
    "        self.scope = scope\n",
    "        self.lgtype = lgtype\n",
    "        self.name = name\n",
    "        self.comment = comment\n",
    "        \n",
    "    def __repr__ (self):\n",
    "        return f'<ISOLanguageCode {self.iso3} {self.name}>'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d3632e15",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7917"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(lines)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "490fccce",
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_iso_table ():\n",
    "    lines = requests.get(iso_table_fn).text.split('\\n')\n",
    "    assert lines[0].startswith('Id\\t')\n",
    "    table = {}\n",
    "    for line in lines[1:]:\n",
    "        code = ISOLanguageCode(*line.rstrip('\\r').split('\\t'))\n",
    "        table[code.iso3] = code\n",
    "    return table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "9dd982d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "iso_table = load_iso_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "2c9b20a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7916"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(iso_table)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "bb71d96e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<ISOLanguageCode hrz Harzani>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "table['hrz']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "01d4255d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<ISOLanguageCode otw Ottawa>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "table['otw']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb634113",
   "metadata": {},
   "source": [
    "Name index:\n",
    "  * Id - The three-letter 639-3 identifier\n",
    "  * Print_Name - One of the names associated with this identifier\n",
    "  * Inverted_Name - The inverted form of this Print_Name form\n",
    "\n",
    "Many codes have multiple entries.\n",
    "\n",
    "We can pool the names and inverted names together: all and only the inverted names contain a comma."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "51f79f80",
   "metadata": {},
   "outputs": [],
   "source": [
    "iso_names_fn = 'https://iso639-3.sil.org/sites/iso639-3/files/downloads/iso-639-3_Name_Index.tab'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "433c1433",
   "metadata": {},
   "outputs": [],
   "source": [
    "lines = requests.get(iso_names_fn).text.split('\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "292d59af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8287"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(lines)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "94edc169",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Id\\tPrint_Name\\tInverted_Name\\r'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lines[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "e93a865c",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert lines[0].startswith('Id\\t')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "6c3e785b",
   "metadata": {},
   "outputs": [],
   "source": [
    "index = {}\n",
    "for line in lines:\n",
    "    (iso3, name, invname) = line.rstrip('\\r').split('\\t')\n",
    "    if iso3 not in index:\n",
    "        index[iso3] = list()\n",
    "    index[iso3].append((name, invname))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "c521b184",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Inverted_Name\n"
     ]
    }
   ],
   "source": [
    "for lst in index.values():\n",
    "    for (name, invname) in lst:\n",
    "        if ',' in name:\n",
    "            print(name)\n",
    "        if name != invname and ',' not in invname:\n",
    "            print(invname)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3bf1befc",
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_names_index ():\n",
    "    lines = requests.get(iso_names_fn).text.split('\\n')\n",
    "    assert lines[0].startswith('Id\\t')\n",
    "    index = {}\n",
    "    for line in lines:\n",
    "        (iso3, name, invname) = line.rstrip('\\r').split('\\t')\n",
    "        if iso3 not in index:\n",
    "            index[iso3] = list()\n",
    "        index[iso3].append(name, invname))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
