{ "cells": [ { "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 xml.etree.ElementTree as ET\n" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found 9 files\n", "Parsing Code de la consommation.xml\n", "Found 2164 articles\n", "Parsing Code civil.xml\n", "Found 2881 articles\n", "Parsing Code du travail.xml\n", "Found 11258 articles\n", "Parsing Code général des impôts.xml\n", "Found 2315 articles\n", "Parsing Code de la propriété intellectuelle.xml\n", "Found 1896 articles\n", "Parsing Code de la santé publique.xml\n", "Found 13242 articles\n", "Parsing Code de la sécurité sociale.xml\n", "Found 7318 articles\n", "Parsing Code pénal.xml\n", "Found 1303 articles\n", "Parsing Code de la route.xml\n", "Found 1157 articles\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", " print(\"Found {} articles\".format(len(articles)))\n", " \n", " return articles\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 = retrieve_every_article_xml(os.path.join(main_folder, f))\n", " articles_text = [parse_article_xml(a) for a in articles]\n", " articles_metadata = [a.attrib for a in articles]\n", " \n", " dataframe_articles = pd.DataFrame({\"text\": articles_text})\n", " dataframe_metadata = pd.DataFrame(articles_metadata)\n", " \n", " # concatenate the columns\n", " dataframe_tmp = pd.concat([dataframe_articles, dataframe_metadata], axis=1)\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": 61, "metadata": {}, "outputs": [], "source": [ "# strip the text\n", "articles[\"text\"] = articles[\"text\"].str.strip()" ] }, { "cell_type": "code", "execution_count": 69, "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": [ "!pip3 install pyarrow" ] }, { "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 }