{ "cells": [ { "cell_type": "markdown", "id": "82be8cbf", "metadata": {}, "source": [ "# LangDex: Getting Started\n", "\n", "[LangDex](https://huggingface.co/datasets/Asakiri/langdex) integrates seven open linguistic resources under one schema, keyed on a Glottolog language code and a PanLex concept id, covering ~5,700 languages.\n", "\n", "This notebook walks through several use cases. It downloads the Parquet tables once and queries them with DuckDB. Runs in Colab or Jupyter." ] }, { "cell_type": "code", "execution_count": null, "id": "daf1407f", "metadata": {}, "outputs": [], "source": [ "!pip install -q duckdb huggingface_hub" ] }, { "cell_type": "code", "execution_count": null, "id": "7dfe2cef", "metadata": {}, "outputs": [], "source": [ "import duckdb\n", "from huggingface_hub import snapshot_download\n", "\n", "base = snapshot_download('Asakiri/langdex', repo_type='dataset', allow_patterns='data/*.parquet')\n", "D = f'{base}/data'\n", "con = duckdb.connect()\n", "for t in ['languoids','crosswalk','concepts','lexemes','lexeme_concept',\n", " 'kaikki_lexemes','senses','etymology','phonology','typology','cognate_sets']:\n", " con.execute(f\"CREATE VIEW {t} AS SELECT * FROM read_parquet('{D}/{t}.parquet')\")\n", "print('ready')" ] }, { "cell_type": "markdown", "id": "94c4f355", "metadata": {}, "source": [ "## 1. Translate between two languages, without an English pivot\n", "Words connect through a language-neutral concept id, so any language pair links directly. Spanish `agua` to Japanese, Korean, Russian, and German." ] }, { "cell_type": "code", "execution_count": null, "id": "e21668f9", "metadata": {}, "outputs": [], "source": [ "def translate(word, src_iso, target_isos):\n", " targets = ', '.join(f\"'{t}'\" for t in target_isos)\n", " return con.execute(f'''\n", " WITH src AS (\n", " SELECT DISTINCT lc.concept_id\n", " FROM lexemes le\n", " JOIN languoids lg ON lg.glottocode = le.glottocode AND lg.iso639_3 = '{src_iso}'\n", " JOIN lexeme_concept lc ON lc.lexeme_id = le.lexeme_id\n", " WHERE le.lemma = '{word}'\n", " )\n", " SELECT l.name AS language, string_agg(DISTINCT le.lemma, ', ') AS words\n", " FROM src\n", " JOIN lexeme_concept lc USING (concept_id)\n", " JOIN lexemes le ON le.lexeme_id = lc.lexeme_id\n", " JOIN languoids l ON l.glottocode = le.glottocode\n", " WHERE l.iso639_3 IN ({targets})\n", " GROUP BY 1 ORDER BY 1\n", " ''').df()\n", "\n", "translate('agua', 'spa', ['jpn', 'kor', 'rus', 'deu'])" ] }, { "cell_type": "markdown", "id": "8b0af823", "metadata": {}, "source": [ "## 2. A word profile: part of speech, pronunciation, and definitions" ] }, { "cell_type": "code", "execution_count": null, "id": "22e86b96", "metadata": {}, "outputs": [], "source": [ "con.execute('''\n", " SELECT k.pos, k.ipa, s.definition\n", " FROM kaikki_lexemes k\n", " JOIN senses s ON s.lexeme_id = k.lexeme_id\n", " WHERE k.lang_code = 'en' AND k.lemma = 'free'\n", "''').df()" ] }, { "cell_type": "markdown", "id": "6f413df5", "metadata": {}, "source": [ "## 3. Etymology: English words borrowed from Latin, French, and Ancient Greek" ] }, { "cell_type": "code", "execution_count": null, "id": "dff2169f", "metadata": {}, "outputs": [], "source": [ "con.execute('''\n", " SELECT term1 AS english_word, term2 AS origin, lang2_name AS origin_language\n", " FROM etymology\n", " WHERE lang1 = 'english' AND relationship_type = 'borrowed'\n", " AND lang2 IN ('latin', 'french', 'ancient greek')\n", " LIMIT 15\n", "''').df()" ] }, { "cell_type": "markdown", "id": "1a02620d", "metadata": {}, "source": [ "## 4. Phonology: languages with the largest phoneme inventories" ] }, { "cell_type": "code", "execution_count": null, "id": "9a7d8731", "metadata": {}, "outputs": [], "source": [ "con.execute('''\n", " SELECT lg.name, count(*) AS phonemes\n", " FROM phonology p\n", " JOIN languoids lg ON lg.glottocode = p.glottocode\n", " GROUP BY 1 ORDER BY 2 DESC LIMIT 10\n", "''').df()" ] }, { "cell_type": "markdown", "id": "7762ed47", "metadata": {}, "source": [ "## 5. One meaning across many languages: 'water'" ] }, { "cell_type": "code", "execution_count": null, "id": "1ff2817d", "metadata": {}, "outputs": [], "source": [ "con.execute('''\n", " SELECT lg.name AS language, string_agg(DISTINCT le.lemma, ', ') AS words\n", " FROM concepts c\n", " JOIN lexeme_concept lc USING (concept_id)\n", " JOIN lexemes le ON le.lexeme_id = lc.lexeme_id\n", " JOIN languoids lg ON lg.glottocode = le.glottocode\n", " WHERE c.gloss = 'water' AND lg.name IS NOT NULL\n", " GROUP BY 1 ORDER BY 1 LIMIT 25\n", "''').df()" ] }, { "cell_type": "markdown", "id": "9044bb39", "metadata": {}, "source": [ "## 6. Cognate sets (Indo-European, from Lexibank)" ] }, { "cell_type": "code", "execution_count": null, "id": "cf5c90ca", "metadata": {}, "outputs": [], "source": [ "con.execute('''\n", " SELECT concept, language_count, word_count\n", " FROM cognate_sets ORDER BY language_count DESC LIMIT 10\n", "''').df()" ] }, { "cell_type": "markdown", "id": "adb723db", "metadata": {}, "source": [ "## Try your own" ] }, { "cell_type": "code", "execution_count": null, "id": "a56a4ec2", "metadata": {}, "outputs": [], "source": [ "translate('mother', 'eng', ['spa', 'fra', 'deu', 'hin', 'swa'])" ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 5 }