File size: 5,318 Bytes
a0077ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "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
}