{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Defaulting to user installation because normal site-packages is not writeable\n", "Collecting lxml\n", " Downloading lxml-4.9.3.tar.gz (3.6 MB)\n", "\u001b[K |████████████████████████████████| 3.6 MB 3.0 MB/s eta 0:00:01\n", "\u001b[?25hBuilding wheels for collected packages: lxml\n", " Building wheel for lxml (setup.py) ... \u001b[?25ldone\n", "\u001b[?25h Created wheel for lxml: filename=lxml-4.9.3-cp39-cp39-macosx_10_9_universal2.whl size=3109250 sha256=23007818bc62b33d4c6a6311fe35a09a7c0ad0b8150ed0a5b095f754a363c11e\n", " Stored in directory: /Users/adrienbufort/Library/Caches/pip/wheels/5c/05/aa/530f84480d476c5bb9ea09877eea78fb144ec047fbb00ee2ca\n", "Successfully built lxml\n", "Installing collected packages: lxml\n", "Successfully installed lxml-4.9.3\n", "\u001b[33mWARNING: You are using pip version 21.2.4; however, version 23.3.1 is available.\n", "You should consider upgrading via the '/Library/Developer/CommandLineTools/usr/bin/python3 -m pip install --upgrade pip' command.\u001b[0m\n" ] } ], "source": [ "!pip3 install lxml" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "# import xml parsing tools\n", "import lxml.etree as ET\n" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found 9 files\n", "Parsing Code de la consommation.xml\n", "Parsing Code civil.xml\n", "Parsing Code du travail.xml\n", "Parsing Code général des impôts.xml\n", "Parsing Code de la propriété intellectuelle.xml\n", "Parsing Code de la santé publique.xml\n", "Parsing Code de la sécurité sociale.xml\n", "Parsing Code pénal.xml\n", "Parsing Code de la route.xml\n" ] } ], "source": [ "def retrieve_every_article_xml(xml_file):\n", " \"\"\"\n", " This function retrieves all the articles from a xml file\n", " \"\"\"\n", " tree = ET.parse(xml_file)\n", " root = tree.getroot()\n", " \n", " # find all xml tags with the name \"article\"\n", " articles = root.findall(\".//article\")\n", " \n", " # find all the parents of the articles\n", " parents = [a.getparent() for a in articles]\n", " \n", " return articles, parents\n", "\n", "def parse_article_xml(article):\n", " \n", " # we we check if there is any children\n", " return \"\".join(article.itertext())\n", "\n", "def main(main_folder=\"/Users/adrienbufort/Documents/workspace/datasets/LoiLibre/raw\"):\n", " \n", " # npw we can list all the xml files in the folder\n", " import os\n", " files = os.listdir(main_folder)\n", " files = [f for f in files if f.endswith(\".xml\")]\n", " print(\"Found {} files\".format(len(files)))\n", " \n", " # we can now parse all the files \n", " df_articles = []\n", " \n", " for f in files:\n", " print(\"Parsing {}\".format(f))\n", " articles, parents = retrieve_every_article_xml(os.path.join(main_folder, f))\n", " \n", " \n", " articles_text = [parse_article_xml(a) for a in articles]\n", " articles_metadata = [a.attrib for a in articles]\n", " \n", " title_parent = [p.attrib[\"title\"] for p in parents]\n", " \n", " dataframe_articles = pd.DataFrame({\"text\": articles_text, \"title_parent\": title_parent})\n", " dataframe_metadata = pd.DataFrame(articles_metadata)\n", " \n", " # concatenate the columns\n", " dataframe_tmp = pd.concat([dataframe_articles, dataframe_metadata], axis=1)\n", " \n", " # add the file name\n", " dataframe_tmp[\"file_name\"] = f\n", " \n", " df_articles.append(dataframe_tmp)\n", " \n", " # concatenate all the dataframes\n", " df_articles = pd.concat(df_articles)\n", " \n", " return df_articles\n", "\n", "# we can now parse all the files\n", "articles = main()\n", " \n", " \n", " \n", "\n" ] }, { "cell_type": "code", "execution_count": 19, "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", " \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", "
texttitle_parentciddateetatidintOrdremodIdmodTitlenumfile_name
0\\n \\n Pour l'application du présent...Partie législative nouvelleLEGIARTI0000322282152021-12-23VIGUEURLEGIARTI0000445631031014089499JORFTEXT000044546235Ordonnance n°2021-1734 du 22 décembre 2021 - a...liminaireCode de la consommation.xml
1\\n Avant que le consommateur ne soi...Chapitre Ier : Obligation générale d'informati...LEGIARTI0000322209032021-09-30VIGUEURLEGIARTI000044142438195225786JORFTEXT000044125847Ordonnance n°2021-1247 du 29 septembre 2021 - ...L111-1Code de la consommation.xml
2\\n Outre les mentions prévues à l'a...Chapitre Ier : Obligation générale d'informati...LEGIARTI0000322209052016-03-16VIGUEURLEGIARTI000032227350390451572JORFTEXT000032209352Ordonnance n°2016-301 du 14 mars 2016 - art.L111-2Code de la consommation.xml
3\\n Les dispositions des articles L....Chapitre Ier : Obligation générale d'informati...LEGIARTI0000322209072017-04-07VIGUEURLEGIARTI000034388081585677358JORFTEXT000034379166Ordonnance n°2017-484 du 6 avril 2017 - art. 17L111-3Code de la consommation.xml
4\\n Le fabricant ou l'importateur de...Chapitre Ier : Obligation générale d'informati...LEGIARTI0000322209092021-11-16VIGUEURLEGIARTI000044330854780903144JORFTEXT000044327272LOI n°2021-1485 du 15 novembre 2021 - art. 18L111-4Code de la consommation.xml
....................................
1152\\n I.- Lorsque ces informations son...Chapitre 1er : Responsabilité pénaleLEGIARTI0000336684772021-10-21VIGUEURLEGIARTI0000442331231879048191JORFTEXT000044229080Arrêté du 13 octobre 2021 - art. 5A121-3Code de la route.xml
1153\\n Sont applicables en Nouvelle-Cal...Chapitre 3 : Dispositions applicables en Nouve...LEGIARTI0000336684912021-10-21VIGUEURLEGIARTI0000442331281073741823JORFTEXT000044229080Arrêté du 13 octobre 2021 - art. 6A143-1Code de la route.xml
1154\\n Le modèle de fiche descripti...Sous-section 1 : Dispositions généralesLEGIARTI0000425229382023-06-11VIGUEURLEGIARTI0000476667531073741823JORFTEXT000047664460Arrêté du 9 juin 2023 - art. 1A325-12Code de la route.xml
1155\\n Les données relatives à l'en...Sous-section 1 : Dispositions généralesLEGIARTI0000425229402021-03-28VIGUEURLEGIARTI0000433086151610612735JORFTEXT000043297189Arrêté du 15 mars 2021 - art. 1A325-13Code de la route.xml
1156\\n Le délai prévu au quatrième ...Sous-section 1 : Dispositions généralesLEGIARTI0000425229422022-06-18VIGUEURLEGIARTI0000459360671879048191JORFTEXT000045930087Arrêté du 10 juin 2022 - art. 1A325-14Code de la route.xml
\n", "

43534 rows × 11 columns

\n", "
" ], "text/plain": [ " text \\\n", "0 \\n \\n Pour l'application du présent... \n", "1 \\n Avant que le consommateur ne soi... \n", "2 \\n Outre les mentions prévues à l'a... \n", "3 \\n Les dispositions des articles L.... \n", "4 \\n Le fabricant ou l'importateur de... \n", "... ... \n", "1152 \\n I.- Lorsque ces informations son... \n", "1153 \\n Sont applicables en Nouvelle-Cal... \n", "1154 \\n Le modèle de fiche descripti... \n", "1155 \\n Les données relatives à l'en... \n", "1156 \\n Le délai prévu au quatrième ... \n", "\n", " title_parent cid \\\n", "0 Partie législative nouvelle LEGIARTI000032228215 \n", "1 Chapitre Ier : Obligation générale d'informati... LEGIARTI000032220903 \n", "2 Chapitre Ier : Obligation générale d'informati... LEGIARTI000032220905 \n", "3 Chapitre Ier : Obligation générale d'informati... LEGIARTI000032220907 \n", "4 Chapitre Ier : Obligation générale d'informati... LEGIARTI000032220909 \n", "... ... ... \n", "1152 Chapitre 1er : Responsabilité pénale LEGIARTI000033668477 \n", "1153 Chapitre 3 : Dispositions applicables en Nouve... LEGIARTI000033668491 \n", "1154 Sous-section 1 : Dispositions générales LEGIARTI000042522938 \n", "1155 Sous-section 1 : Dispositions générales LEGIARTI000042522940 \n", "1156 Sous-section 1 : Dispositions générales LEGIARTI000042522942 \n", "\n", " date etat id intOrdre \\\n", "0 2021-12-23 VIGUEUR LEGIARTI000044563103 1014089499 \n", "1 2021-09-30 VIGUEUR LEGIARTI000044142438 195225786 \n", "2 2016-03-16 VIGUEUR LEGIARTI000032227350 390451572 \n", "3 2017-04-07 VIGUEUR LEGIARTI000034388081 585677358 \n", "4 2021-11-16 VIGUEUR LEGIARTI000044330854 780903144 \n", "... ... ... ... ... \n", "1152 2021-10-21 VIGUEUR LEGIARTI000044233123 1879048191 \n", "1153 2021-10-21 VIGUEUR LEGIARTI000044233128 1073741823 \n", "1154 2023-06-11 VIGUEUR LEGIARTI000047666753 1073741823 \n", "1155 2021-03-28 VIGUEUR LEGIARTI000043308615 1610612735 \n", "1156 2022-06-18 VIGUEUR LEGIARTI000045936067 1879048191 \n", "\n", " modId modTitle \\\n", "0 JORFTEXT000044546235 Ordonnance n°2021-1734 du 22 décembre 2021 - a... \n", "1 JORFTEXT000044125847 Ordonnance n°2021-1247 du 29 septembre 2021 - ... \n", "2 JORFTEXT000032209352 Ordonnance n°2016-301 du 14 mars 2016 - art. \n", "3 JORFTEXT000034379166 Ordonnance n°2017-484 du 6 avril 2017 - art. 17 \n", "4 JORFTEXT000044327272 LOI n°2021-1485 du 15 novembre 2021 - art. 18 \n", "... ... ... \n", "1152 JORFTEXT000044229080 Arrêté du 13 octobre 2021 - art. 5 \n", "1153 JORFTEXT000044229080 Arrêté du 13 octobre 2021 - art. 6 \n", "1154 JORFTEXT000047664460 Arrêté du 9 juin 2023 - art. 1 \n", "1155 JORFTEXT000043297189 Arrêté du 15 mars 2021 - art. 1 \n", "1156 JORFTEXT000045930087 Arrêté du 10 juin 2022 - art. 1 \n", "\n", " num file_name \n", "0 liminaire Code de la consommation.xml \n", "1 L111-1 Code de la consommation.xml \n", "2 L111-2 Code de la consommation.xml \n", "3 L111-3 Code de la consommation.xml \n", "4 L111-4 Code de la consommation.xml \n", "... ... ... \n", "1152 A121-3 Code de la route.xml \n", "1153 A143-1 Code de la route.xml \n", "1154 A325-12 Code de la route.xml \n", "1155 A325-13 Code de la route.xml \n", "1156 A325-14 Code de la route.xml \n", "\n", "[43534 rows x 11 columns]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# get parent\n", "articles" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "# strip the text\n", "articles[\"text\"] = articles[\"text\"].str.strip()\n", "articles[\"file_name\"] = articles[\"file_name\"].str[:-4]" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "# save the dataframe\n", "articles.to_parquet(\"../articles.parquet\")" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Defaulting to user installation because normal site-packages is not writeable\n", "Collecting pyarrow\n", " Downloading pyarrow-14.0.1-cp39-cp39-macosx_11_0_arm64.whl (24.0 MB)\n", "\u001b[K |████████████████████████████████| 24.0 MB 14.7 MB/s eta 0:00:01\n", "\u001b[?25hRequirement already satisfied: numpy>=1.16.6 in /Users/adrienbufort/Library/Python/3.9/lib/python/site-packages (from pyarrow) (1.25.1)\n", "Installing collected packages: pyarrow\n", "Successfully installed pyarrow-14.0.1\n", "\u001b[33mWARNING: You are using pip version 21.2.4; however, version 23.3.1 is available.\n", "You should consider upgrading via the '/Library/Developer/CommandLineTools/usr/bin/python3 -m pip install --upgrade pip' command.\u001b[0m\n" ] } ], "source": [] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "text Par dérogation à l'article 167 du code de proc...\n", "title_parent Sous-section 9 : Expertise\n", "cid LEGIARTI000032223139\n", "date 2016-03-16\n", "etat VIGUEUR\n", "id LEGIARTI000032225017\n", "intOrdre 1789569700\n", "modId JORFTEXT000032209352\n", "modTitle Ordonnance n°2016-301 du 14 mars 2016 - art.\n", "num L512-48\n", "file_name Code de la consommation\n", "Name: 1000, dtype: object\n" ] }, { "data": { "text/plain": [ "\"Par dérogation à l'article 167 du code de procédure pénale, si les experts sont en désaccord, ou s'ils sont d'accord pour infirmer les conclusions du rapport du laboratoire d'Etat, le procureur de la République ou la juridiction, avant de statuer, donne à ce laboratoire connaissance du rapport d'expertise et lui fixe un délai pour faire parvenir éventuellement ses observations, sauf dans le cas où le directeur du laboratoire intéressé a participé lui-même à l'expertise en qualité d'expert.\"" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(articles.iloc[1000])\n", "articles.iloc[1000].text" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.9.6" } }, "nbformat": 4, "nbformat_minor": 2 }