{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "id": "14r06gMaaaxk" }, "outputs": [], "source": [ "from datasets import load_dataset\n", "import pickle\n", "import pandas as pd\n", "import numpy as np\n", "from datasets import DownloadConfig" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "download_config = DownloadConfig(delete_extracted=True)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 220, "referenced_widgets": [ "cd99776836b44fd1b7c1a372d47152b1", "d90270d2fa8346d394823f110e4da17e", "1f3c6ac8cf0448b58e201d2f0797509f", "89baf2a7f0534fc29c7d6a0f4ec013cf", "823176ac7c124e5985757d357502bd6a", "c14ddb0478c94491b61fec65ca9e3b5b", "353cf99dbbab40aeaf25835f27728873", "57a6c20ccdae410cbdaf239fc6c43295", "be4fa2958f5f4f6a8e4fc067dc3d6483", "c2f7554e0ce14b649462db03f31baed8", "4647ccdafeaa4d7a86c64abd4d9b3efc", "e88301d2e8004768a47b7d0bad824c9a", "68f6d87049bb40359263d9d12e1762c5", "5a500c7b174b42ed8409124ef4a9e702", "daae7cd4eaa9464385030c53ff12bf3e", "9aa946dda64e451d80069e67be323982", "19d8e051629d4585b24a3472453e071f", "92678362e2bd4272a68feca9ec6584d0", "eeda3f3f49f84b21871e30b3d090b329", "e553a5f5fca54aaea249a846331ae4ae", "d22acdf4773a412081c15a66d7df8e5f", "857cf53cd610490e9950a1032c3f3411", "1a763ce25cfa4971ba6242d21d89cc37", "caf30f364a9b491ab5467e4e26f5ffab", "4387cd67ae754397976fa157618da3eb", "7fd018779888459383b3b62dc2dda947", "171f1d88d7e2470db063bd46b2d5fc43", "ec08de7bb52647da887b2310472cdf26", "fae7930182f74cab8c6298aa0b41820f", "9f863de4dd9d4d4487821be6e008cf4a", "7cc0fd84f81f4b4497e200ef8cb1f91f", "70de37f395bd45a7bad9efaaa8f33811", "5693712e38744043aa6aacec1b06a883", "2316436e8a2743fd850062485b63aa22", "8893965604fa4d4d818f5b48d52f4f36", "d2feb9c6df294efe81e30685ce60a09d", "2e9652bae3c840dda6e52717b1c3ca5a", "643aa7a012b04d8d82bac9f29f476d42", "282b8e4574c64789b74b7b2c461f9d65", "619f8d2cd63a47f8b0290f7572926edb", "a28eb14c9c684b15ae99000410c3b451", "ca3283852e5e4ab39fa2778cd4a35410", "bb375eb8bcd449bc97fc7a6bd1ca957b", "284fd6e88f6440b5bd71c096d43f868b", "af28d22d00fe438c89d120396ec1f071", "2968c9db9c7b4688aa139888879f2457", "436a862858014758b4fd9c98ea0a390b", "d699073518d94016a9a7cc6409d20c6a", "46791137aeab479ca898b5ba79555e64", "30cf535653fe4dce94adeeb017113e1a", "3d6866f065444f958a4ab7f439d20828", "fc6433f603224676b197132ddff80b93", "eb8b87c1a4ef47da94dae5cf81f8de80", "52e6e31613b043c8821a9dcb5658e7b6", "61e5e687e7824c9694e927b85d81188f" ] }, "id": "8Sod42WWadRA", "outputId": "96b6fb42-9c04-42c3-bad9-e7a588304162" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Using custom data configuration default-0663d2a6043864fc\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Downloading and preparing dataset csv/default to /Users/dvanstrien/.cache/huggingface/datasets/csv/default-0663d2a6043864fc/0.0.0/51cce309a08df9c4d82ffd9363bbe090bf173197fc01a71b034e8594995a1a58...\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "ebd9db43605f42798bca44a5866629b8", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Downloading data files: 0%| | 0/1 [00:00 of the transform datasets.arrow_dataset.Dataset._map_single couldn't be hashed properly, a random hash was used instead. Make sure your transforms and parameters are serializable with pickle or dill for the dataset fingerprinting and caching to work. If you reuse this transform, the caching mechanism will consider it to be different from the previous calls and recompute everything. This warning is only showed once. Subsequent hashing failures won't be showed.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ " " ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "ae01361fd9cd432088b9f12a4dbcc614", "version_major": 2, "version_minor": 0 }, "text/plain": [ "#0: 0%| | 0/1247025 [00:00\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ppnfilenamelanguageconfidence
0PPN64642623000000045.xmlfr1.0
1PPN64642623000000218.xmlfr1.0
2PPN64642623000000394.xmlfr1.0
3PPN64642623000000071.xmlfr1.0
4PPN64642623000000317.xmlfr1.0
...............
7479PPN82189987200000017.xmlde1.0
7480PPN82189987200000510.xmlde1.0
7481PPN82189987200000487.xmlde1.0
7482PPN82189987200000187.xmlde1.0
7483PPN82189987200000274.xmlde1.0
\n", "

4734627 rows × 4 columns

\n", "" ], "text/plain": [ " ppn filename language confidence\n", "0 PPN646426230 00000045.xml fr 1.0\n", "1 PPN646426230 00000218.xml fr 1.0\n", "2 PPN646426230 00000394.xml fr 1.0\n", "3 PPN646426230 00000071.xml fr 1.0\n", "4 PPN646426230 00000317.xml fr 1.0\n", "... ... ... ... ...\n", "7479 PPN821899872 00000017.xml de 1.0\n", "7480 PPN821899872 00000510.xml de 1.0\n", "7481 PPN821899872 00000487.xml de 1.0\n", "7482 PPN821899872 00000187.xml de 1.0\n", "7483 PPN821899872 00000274.xml de 1.0\n", "\n", "[4734627 rows x 4 columns]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "data['in'] = data['ppn'] + \"_\" + data['filename']" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "data = data.drop_duplicates(subset=['in'])" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "id": "Pt4H7_8wPuOz" }, "outputs": [], "source": [ "data = data.set_index(\"in\")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ppnfilenamelanguageconfidence
in
PPN646426230_00000045.xmlPPN64642623000000045.xmlfr1.0
PPN646426230_00000218.xmlPPN64642623000000218.xmlfr1.0
PPN646426230_00000394.xmlPPN64642623000000394.xmlfr1.0
PPN646426230_00000071.xmlPPN64642623000000071.xmlfr1.0
PPN646426230_00000317.xmlPPN64642623000000317.xmlfr1.0
...............
PPN821899872_00000017.xmlPPN82189987200000017.xmlde1.0
PPN821899872_00000510.xmlPPN82189987200000510.xmlde1.0
PPN821899872_00000487.xmlPPN82189987200000487.xmlde1.0
PPN821899872_00000187.xmlPPN82189987200000187.xmlde1.0
PPN821899872_00000274.xmlPPN82189987200000274.xmlde1.0
\n", "

4697574 rows × 4 columns

\n", "
" ], "text/plain": [ " ppn filename language confidence\n", "in \n", "PPN646426230_00000045.xml PPN646426230 00000045.xml fr 1.0\n", "PPN646426230_00000218.xml PPN646426230 00000218.xml fr 1.0\n", "PPN646426230_00000394.xml PPN646426230 00000394.xml fr 1.0\n", "PPN646426230_00000071.xml PPN646426230 00000071.xml fr 1.0\n", "PPN646426230_00000317.xml PPN646426230 00000317.xml fr 1.0\n", "... ... ... ... ...\n", "PPN821899872_00000017.xml PPN821899872 00000017.xml de 1.0\n", "PPN821899872_00000510.xml PPN821899872 00000510.xml de 1.0\n", "PPN821899872_00000487.xml PPN821899872 00000487.xml de 1.0\n", "PPN821899872_00000187.xml PPN821899872 00000187.xml de 1.0\n", "PPN821899872_00000274.xml PPN821899872 00000274.xml de 1.0\n", "\n", "[4697574 rows x 4 columns]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "lookup_dict = data.to_dict('index')" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "def lookup_lang_confidence(example):\n", " ppn = example['ppn']\n", " fname = example['file name']\n", " if match := lookup_dict.get(f\"PPN{ppn}_{fname}\"):\n", " return {\"language\": match['language'],'language_confidence': match['confidence']}\n", " else:\n", " return {\"language\": None,\"language_confidence\": None}" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'language': 'fr', 'language_confidence': 1.0}" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lookup_lang_confidence(ds['train'][1])" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 49, "referenced_widgets": [ "ab7308a1b84146a18d74f603d100b2dd", "968838b1ba144df1ae7bb34279303e57", "d075e8c624ad46faad3c54dfc5834210", "8f42042ef34f4649b2c97a99140812a6", "5e22fa51c8b54c39a306a34c4a354b16", "98d2ef57a9f84bf3ab43fb0be936f6c1", "2bbb6c575f594c968e1c7db67d6b6c9a", "3a6cbc51c3324e5ab981b2e82b8ec0d5", "5c572b39b21b46bfa25e386c564869b9", "4655df7d1d7548e08bf823708377dc88", "b49d3d9226ba4436ba301ff9ebe3b791" ] }, "id": "Arr7ArLcdu2h", "outputId": "caf00be0-8351-4e1f-fb97-4c56692a6758" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " " ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "4f6c519d0bfd4bf9b013b33785a68088", "version_major": 2, "version_minor": 0 }, "text/plain": [ "#0: 0%| | 0/1247025 [00:00\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
publisherplacedatetitleautppn
0HampeliusGiessae Hessorum1620Collegium Ethicum, In Quo De Summo Hominis Bon...Liebenthal, ChristianusPPN1000000974
1EgenolphusMarpurgi1614De AuguriisNiphus, AugustinusPPN100000127X
2Breitkopf & HärtelLeipsic1806Concerto pour la Flûte avec Accompagnement de ...Danzi, FranzPPN1000006808
3Königliche privil. BuchdruckereyGlückstadt1719Schuldige Condolentz-Zeilen Womit seinem Gelie...Koltemann, Otto BenedictPPN1000056597
4OehmigkeBerlin1796Vorschlag zur Verbesserung des doppelten Leist...Böttcher, Johann FriedrichPPN1000059669
.....................
121690NoneNone1856Der WächterNonePPN74709716X
121691NoneNone1857Der WächterNonePPN747097208
121692NoneNone1858Der WächterNonePPN747097224
121693NoneNone1859Der WächterNonePPN747097240
121694NoneNone1860Der WächterNonePPN747097267
\n", "

121695 rows × 6 columns

\n", "" ], "text/plain": [ " publisher place date \\\n", "0 Hampelius Giessae Hessorum 1620 \n", "1 Egenolphus Marpurgi 1614 \n", "2 Breitkopf & Härtel Leipsic 1806 \n", "3 Königliche privil. Buchdruckerey Glückstadt 1719 \n", "4 Oehmigke Berlin 1796 \n", "... ... ... ... \n", "121690 None None 1856 \n", "121691 None None 1857 \n", "121692 None None 1858 \n", "121693 None None 1859 \n", "121694 None None 1860 \n", "\n", " title \\\n", "0 Collegium Ethicum, In Quo De Summo Hominis Bon... \n", "1 De Auguriis \n", "2 Concerto pour la Flûte avec Accompagnement de ... \n", "3 Schuldige Condolentz-Zeilen Womit seinem Gelie... \n", "4 Vorschlag zur Verbesserung des doppelten Leist... \n", "... ... \n", "121690 Der Wächter \n", "121691 Der Wächter \n", "121692 Der Wächter \n", "121693 Der Wächter \n", "121694 Der Wächter \n", "\n", " aut ppn \n", "0 Liebenthal, Christianus PPN1000000974 \n", "1 Niphus, Augustinus PPN100000127X \n", "2 Danzi, Franz PPN1000006808 \n", "3 Koltemann, Otto Benedict PPN1000056597 \n", "4 Böttcher, Johann Friedrich PPN1000059669 \n", "... ... ... \n", "121690 None PPN74709716X \n", "121691 None PPN747097208 \n", "121692 None PPN747097224 \n", "121693 None PPN747097240 \n", "121694 None PPN747097267 \n", "\n", "[121695 rows x 6 columns]" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "metadata_df" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "metadata_df = metadata_df.drop_duplicates(subset=['ppn'])" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "lookup_dict = metadata_df.set_index('ppn').to_dict('index')" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['publisher', 'place', 'date', 'title', 'aut'])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lookup_dict['PPN1000000974'].keys()" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "def lookup_metadata(example):\n", " ppn = example['ppn']\n", " if match := lookup_dict.get(f\"PPN{ppn}\"):\n", " return {\"publisher\": match['publisher'],'place': match['place'], 'date': match['date'], 'title': match['title'], 'aut': match['aut']}\n", " else:\n", " return {\"publisher\": None,\"place\": None, \"date\": None, \"title\": None, \"aut\": None}" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'publisher': 'Imprimerie de la Mission Catholique',\n", " 'place': 'Chang-hai',\n", " 'date': '1912',\n", " 'title': 'Les pratiques superstitieuses',\n", " 'aut': 'Doré, Henri'}" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lookup_metadata(ds['train'][100])" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " " ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "0b3aaea327a043379e090cd99e90eb53", "version_major": 2, "version_minor": 0 }, "text/plain": [ "#1: 0%| | 0/1247025 [00:00