File size: 6,160 Bytes
79d1f7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
{
 "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
}