{
"cells": [
{
"cell_type": "code",
"execution_count": 85,
"id": "138769da",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import requests\n",
"import itertools\n",
"import pandas as pd\n",
"from bs4 import BeautifulSoup\n",
"from tqdm import trange\n",
"import datasets as dst # to create a huggingface dataset\n",
"\n",
"from chainfury.utils import threaded_map, to_json, from_json, get_files_in_folder"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f51b009f",
"metadata": {},
"outputs": [],
"source": [
"def _get_page_html(script, chapter_n, sutra_n, get_trans: bool = True, v: bool = False):\n",
" translations = {\n",
" \"htrskd\": 1, # Hindi Translation By Swami Ramsukhdas\n",
" \"httyn\": 1, # Hindi Translation By Swami Tejomayananda\n",
" \"htshg\": 1, # Hindi Translation Of Sri Shankaracharya's Sanskrit Commentary By Sri Harikrishnadas Goenka\n",
" \"scsh\": 1, # Sanskrit Commentary By Sri Shankaracharya\n",
" \"hcchi\": 1, # Hindi Commentary By Swami Chinmayananda\n",
" \"hcrskd\": 1, # Hindi Commentary By Swami Ramsukhdas\n",
" \"scang\": 1, # Sanskrit Commentary By Sri Abhinavgupta\n",
" \"scram\": 1, # Sanskrit Commentary By Sri Ramanujacharya\n",
" \"scanand\": 1, # Sanskrit Commentary By Sri Anandgiri\n",
" \"scjaya\": 1, # Sanskrit Commentary By Sri Jayatirtha\n",
" \"scmad\": 1, # Sanskrit Commentary By Sri Madhvacharya\n",
" \"scval\": 1, # Sanskrit Commentary By Sri Vallabhacharya\n",
" \"scms\": 1, # Sanskrit Commentary By Sri Madhusudan Saraswati\n",
" \"scsri\": 1, # Sanskrit Commentary By Sri Sridhara Swami\n",
" \"scvv\": 1, # Sanskrit Commentary By Sri Vedantadeshikacharya Venkatanatha\n",
" \"scpur\": 1, # Sanskrit Commentary By Sri Purushottamji\n",
" \"scneel\": 1, # Sanskrit Commentary By Sri Neelkanth\n",
" \"scdhan\": 1, # Sanskrit Commentary By Sri Dhanpati\n",
" \"ecsiva\": 1, # English Commentary By Swami Sivananda\n",
" \"etsiva\": 1, # English Translation By Swami Sivananda\n",
" \"etpurohit\": 1, # English Translation By Purohit Swami\n",
" \"etgb\": 1, # English Translation By Swami Gambirananda\n",
" \"setgb\": 1, # English Translation Of Sri Shankaracharya By Swami Gambirananda\n",
" \"etssa\": 1, # English Translation By Dr. S. Sankaranarayan\n",
" \"etassa\": 1, # English Translation of Abhinavgupta's Sanskrit Commentary By Dr. S. Sankaranarayan\n",
" \"etradi\": 1, # English Translation of Ramanujacharya's Sanskrit Commentary By Swami Adidevananda\n",
" \"etadi\": 1, # English Translation By Swami Adidevananda\n",
" }\n",
" params = {\n",
" \"language\": script,\n",
" \"field_chapter_value\": chapter_n,\n",
" \"field_nsutra_value\": sutra_n,\n",
" \"show_mool\": 1,\n",
" }\n",
" if get_trans:\n",
" params.update(translations)\n",
" r = requests.get(\n",
" \"https://www.gitasupersite.iitk.ac.in/srimad\",\n",
" params = params\n",
" )\n",
" if v:\n",
" print(r.url)\n",
" r.raise_for_status()\n",
" soup = BeautifulSoup(r.text)\n",
"\n",
" # get the main shloka text first\n",
" shloka_div = BeautifulSoup(\n",
" str(soup.find_all(\"div\", {\"class\": \"views-field views-field-body\"})[0].find_all(\"p\")[0])\n",
" .replace(\"
\", \"\\n\")\n",
" )\n",
" shloka = shloka_div.getText().strip()\n",
"\n",
" # now find all the translations and store them\n",
" data = {\n",
" \"id\": f\"{chapter_n}.{sutra_n}\",\n",
" \"script\": script,\n",
" \"body\": shloka,\n",
" \"translations\": []\n",
" }\n",
" for t in translations:\n",
" trans_div = soup.find_all(\"div\", {\"class\": f\"views-field views-field-field-{t}\"})\n",
" if trans_div:\n",
" all_p = trans_div[0].find_all(\"p\")\n",
" if all_p:\n",
" trans_div = all_p[0]\n",
" trans = trans_div.getText().strip()\n",
" data[\"translations\"].append({\n",
" \"trans\": t,\n",
" \"content\": trans\n",
" })\n",
" return data\n",
"\n",
"def get_page_html(script, chapter_n, sutra_n, get_trans = True, pbar = None):\n",
" try:\n",
" out = _get_page_html(script, chapter_n, sutra_n, get_trans)\n",
" pbar.update(1)\n",
" return out, None\n",
" except Exception as e:\n",
" return (script, chapter_n, sutra_n), e"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "954f795a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(701, [('dv', 1, 1, True)])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"scripts = {\n",
" \"dv\": \"Devanagari\",\n",
" \"as\": \"Assamese\",\n",
" \"bn\": \"Bengali\",\n",
" \"gu\": \"Gujarati\",\n",
" \"pa\": \"Gurmukhi\",\n",
" \"kn\": \"Kannada\",\n",
" \"ml\": \"Malayalam\",\n",
" \"or\": \"Odia\",\n",
" \"ro\": \"Roman\",\n",
" \"ta\": \"Tamil\",\n",
" \"te\": \"Telugu\",\n",
"}\n",
"chapter_to_max_sutra = {\n",
" \"1\": 47,\n",
" \"2\": 72,\n",
" \"3\": 43,\n",
" \"4\": 42,\n",
" \"5\": 29,\n",
" \"6\": 47,\n",
" \"7\": 30,\n",
" \"8\": 28,\n",
" \"9\": 34,\n",
" \"10\": 42,\n",
" \"11\": 55,\n",
" \"12\": 20,\n",
" \"13\": 35,\n",
" \"14\": 27,\n",
" \"15\": 20,\n",
" \"16\": 24,\n",
" \"17\": 28,\n",
" \"18\": 78,\n",
"}\n",
"\n",
"items = []\n",
"for sc in list(scripts.keys())[:1]:\n",
" for c in chapter_to_max_sutra:\n",
" items.extend([(sc, int(c), i, True) for i in range(1,chapter_to_max_sutra[c]+1)])\n",
"len(items), items[:1]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "f03fa731",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'id': '1.1',\n",
" 'script': 'dv',\n",
" 'body': 'धृतराष्ट्र उवाच\\n\\n\\n\\nधर्मक्षेत्रे कुरुक्षेत्रे समवेता युयुत्सवः।\\n\\n\\n\\nमामकाः पाण्डवाश्चैव किमकुर्वत सञ्जय।।1.1।।',\n",
" 'translations': []}"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = _get_page_html(*items[0])\n",
"data = _get_page_html(*items[0][:3], False)\n",
"data"
]
},
{
"cell_type": "markdown",
"id": "4eed8567",
"metadata": {},
"source": [
"## Create Gita singular dump"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "f297d9a3",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 7711/7711 [07:53<00:00, 16.29it/s]\n",
"100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▊| 700/701 [00:49<00:00, 14.38it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 34.9 s, sys: 2.38 s, total: 37.3 s\n",
"Wall time: 49.8 s\n"
]
}
],
"source": [
"%%time\n",
"\n",
"pbar = trange(len(items))\n",
"inputs = [(*x, pbar) for x in items]\n",
"translation_data = threaded_map(get_page_html, inputs, max_threads=10)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "80b8278b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(701, 0, 701)"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 701/701 [01:02<00:00, 14.38it/s]"
]
}
],
"source": [
"failed_args = []\n",
"for (out, err) in translation_data:\n",
" if err:\n",
" failed_args.append(out)\n",
"len(translation_data), len(failed_args), len(inputs)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "acd03e14",
"metadata": {},
"outputs": [],
"source": [
"translation_data[0]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "7956f645",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(7711, [('dv', 1, 1, False)])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# now download all the translations\n",
"script_items = []\n",
"for sc in list(scripts.keys()):\n",
" for c in chapter_to_max_sutra:\n",
" script_items.extend([(sc, int(c), i, False) for i in range(1,chapter_to_max_sutra[c]+1)])\n",
"len(script_items), script_items[:1]"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "a5f3781c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'id': '1.1',\n",
" 'script': 'dv',\n",
" 'body': 'धृतराष्ट्र उवाच\\n\\n\\n\\nधर्मक्षेत्रे कुरुक्षेत्रे समवेता युयुत्सवः।\\n\\n\\n\\nमामकाः पाण्डवाश्चैव किमकुर्वत सञ्जय।।1.1।।',\n",
" 'translations': []}"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = _get_page_html(*script_items[0])\n",
"data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3ca21550",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"%%time\n",
"pbar = trange(len(script_items))\n",
"inputs = [(*x, pbar) for x in script_items]\n",
"translit_data = threaded_map(get_page_html, inputs)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "87f03939",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(7711, 0, 7711)"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"failed_args = []\n",
"for (out, err) in translit_data:\n",
" if err:\n",
" failed_args.append(out)\n",
"len(translit_data), len(failed_args), len(translit_data)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "dc8bead5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"({'id': '1.1',\n",
" 'script': 'dv',\n",
" 'body': 'धृतराष्ट्र उवाच\\n\\n\\n\\nधर्मक्षेत्रे कुरुक्षेत्रे समवेता युयुत्सवः।\\n\\n\\n\\nमामकाः पाण्डवाश्चैव किमकुर्वत सञ्जय।।1.1।।',\n",
" 'translations': []},\n",
" None)"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n",
"100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 7711/7711 [04:48<00:00, 8.77it/s]\u001b[A"
]
}
],
"source": [
"translit_data[0]"
]
},
{
"cell_type": "code",
"execution_count": 69,
"id": "12908809",
"metadata": {},
"outputs": [],
"source": [
"# combine the translation and transliteration data\n",
"all_rows = []\n",
"for x, _ in translation_data:\n",
" data = {\n",
" \"shloka_id\": x[\"id\"],\n",
" \"chapter\": x[\"id\"].split(\".\")[0],\n",
" \"sutra\": x[\"id\"].split(\".\")[1],\n",
" }\n",
" for t in x[\"translations\"]:\n",
" if (\"did not comment on this sloka.\" in t[\"content\"] or\n",
" \"No commentary\" in t[\"content\"]\n",
" ):\n",
" continue\n",
" data[\"trans-\" + t[\"trans\"]] = t[\"content\"]\n",
" shloka_lits = list(filter(\n",
" lambda y: y[0][\"id\"] == x[\"id\"],\n",
" translit_data\n",
" ))\n",
" for td, _ in shloka_lits:\n",
" data[f\"script-\" + td[\"script\"]] = td[\"body\"]\n",
" all_rows.append(data)"
]
},
{
"cell_type": "code",
"execution_count": 83,
"id": "b5e054fd",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n", " | shloka_id | \n", "chapter | \n", "sutra | \n", "trans-htrskd | \n", "trans-httyn | \n", "trans-hcchi | \n", "trans-hcrskd | \n", "trans-scang | \n", "trans-scram | \n", "trans-scanand | \n", "... | \n", "script-ml | \n", "script-or | \n", "script-ro | \n", "script-ta | \n", "script-te | \n", "trans-htshg | \n", "trans-scsh | \n", "trans-setgb | \n", "trans-scjaya | \n", "trans-scmad | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "1.1 | \n", "1 | \n", "1 | \n", "।।1.1।। धृतराष्ट्र बोले (टिप्पणी प0 1.2) - हे ... | \n", "।।1.1।।धृतराष्ट्र ने कहा -- हे संजय ! धर्मभूमि... | \n", "।।1.1।। सम्पूर्ण गीता में यही एक मात्र श्लोक अ... | \n", "1।।व्याख्या-- 'धर्मक्षेत्रे' 'कुरुक्षेत्रे'-- ... | \n", "।।1.1।।धर्मक्षेत्र इति। अत्र केचित् व्याख्याव... | \n", "।।1.1।।धृतराष्ट्र उवाच सञ्जय उवाच दुर्योधनः ... | \n", "।।1.1।।एवं गीताशास्त्रस्य साध्यसाधनभूतनिष्ठाद्... | \n", "... | \n", "ധൃതരാഷ്ട്ര ഉവാച\\n\\nധര്മക്ഷേത്രേ കുരുക്ഷേത്രേ സ... | \n", "ଧୃତରାଷ୍ଟ୍ର ଉବାଚ\\n\\nଧର୍ମକ୍ଷେତ୍ରେ କୁରୁକ୍ଷେତ୍ରେ ସ... | \n", "dhṛtarāṣṭra uvāca\\n\\ndharmakṣētrē kurukṣētrē s... | \n", "தரிதராஷ்ட்ர உவாச\\n\\nதர்மக்ஷேத்ரே குருக்ஷேத்ரே ... | \n", "ధృతరాష్ట్ర ఉవాచ\\n\\nధర్మక్షేత్రే కురుక్షేత్రే స... | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
1 | \n", "1.2 | \n", "1 | \n", "2 | \n", "।।1.2।। संजय बोले - उस समय वज्रव्यूह-से खड़ी ह... | \n", "।।1.2।।संजय ने कहा -- पाण्डव-सैन्य की व्यूह रच... | \n", "।।1.2।। इस श्लोक से आगे संजय ने कुरुक्षेत्र मे... | \n", "।।1.2।। व्याख्या-- 'तदा'-- जिस समय दोनों सेनाए... | \n", "।।1.2 1.9।।किं वा अनेन बहुपरिगणनेन (K omits ब... | \n", "।।1.2।।धृतराष्ट्र उवाच सञ्जय उवाच दुर्योधनः ... | \n", "।।1.2।।किमस्मदीयं प्रबलं बलं प्रतिलभ्य धीरपुरु... | \n", "... | \n", "സഞ്ജയ ഉവാച\\n\\nദൃഷ്ട്വാ തു പാണ്ഡവാനീകം വ്യൂഢം ദ... | \n", "ସଞ୍ଜଯ ଉବାଚ\\n\\nଦୃଷ୍ଟ୍ବା ତୁ ପାଣ୍ଡବାନୀକଂ ବ୍ଯୂଢଂ ଦ... | \n", "sañjaya uvāca\\n\\ndṛṣṭvā tu pāṇḍavānīkaṅ vyūḍha... | \n", "ஸஞ்ஜய உவாச\\n\\nதரிஷ்ட்வா து பாண்டவாநீகஂ வ்யூடஂ ... | \n", "సఞ్జయ ఉవాచ\\n\\nదృష్ట్వా తు పాణ్డవానీకం వ్యూఢం ద... | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
2 | \n", "1.3 | \n", "1 | \n", "3 | \n", "।।1.3।। हे आचार्य! आपके बुद्धिमान् शिष्य द्रुप... | \n", "।।1.3।।हे आचार्य ! आपके बुद्धिमान शिष्य द्रुपद... | \n", "।।1.3।। वास्तव में दुर्योधन की यह मूर्खता है क... | \n", "1.3।। व्याख्या--'आचार्य' द्रोणके लिये 'आचार्य'... | \n", "।।1.2 1.9।।किं वा अनेन बहुपरिगणनेन (K omits ब... | \n", "।।1.3।।धृतराष्ट्र उवाच सञ्जय उवाच दुर्योधनः ... | \n", "।।1.3।।तदेव वचनमुदाहरति पश्येति। एतामस्मदभ्... | \n", "... | \n", "പശ്യൈതാം പാണ്ഡുപുത്രാണാമാചാര്യ മഹതീം ചമൂമ്.\\n\\... | \n", "ପଶ୍ଯୈତାଂ ପାଣ୍ଡୁପୁତ୍ରାଣାମାଚାର୍ଯ ମହତୀଂ ଚମୂମ୍|\\n\\... | \n", "paśyaitāṅ pāṇḍuputrāṇāmācārya mahatīṅ camūm.\\n... | \n", "பஷ்யைதாஂ பாண்டுபுத்ராணாமாசார்ய மஹதீஂ சமூம்.\\n\\... | \n", "పశ్యైతాం పాణ్డుపుత్రాణామాచార్య మహతీం చమూమ్.\\n\\... | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
3 | \n", "1.4 | \n", "1 | \n", "4 | \n", "।।1.4 -- 1.6।। यहाँ (पाण्डवों की सेना में) बड़... | \n", "।।1.4।।इस सेना में महान् धनुर्धारी शूर योद्धा ... | \n", "NaN | \n", "।।1.4 --1.6।। व्याख्या--'अत्र शूरा महेष्वासा भ... | \n", "।।1.2 1.9।।किं वा अनेन बहुपरिगणनेन (K omits ब... | \n", "।।1.4।।धृतराष्ट्र उवाच सञ्जय उवाच दुर्योधनः ... | \n", "।।1.4।।अन्येऽपि प्रतिपक्षे पराक्रमभाजो बहवः सन... | \n", "... | \n", "അത്ര ശൂരാ മഹേഷ്വാസാ ഭീമാര്ജുനസമാ യുധി.\\n\\nയുയു... | \n", "ଅତ୍ର ଶୂରା ମହେଷ୍ବାସା ଭୀମାର୍ଜୁନସମା ଯୁଧି|\\n\\nଯୁଯୁ... | \n", "atra śūrā mahēṣvāsā bhīmārjunasamā yudhi.\\n\\ny... | \n", "அத்ர ஷூரா மஹேஷ்வாஸா பீமார்ஜுநஸமா யுதி.\\n\\nயுயு... | \n", "అత్ర శూరా మహేష్వాసా భీమార్జునసమా యుధి.\\n\\nయుయు... | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
4 rows × 41 columns
\n", "