1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | $file = $url = shift; |
---|
4 | #use HTML::TokeParser; |
---|
5 | $url =~ s/^[^\/]+\///; |
---|
6 | $url =~ s/_/\//g; |
---|
7 | $source = $url; |
---|
8 | |
---|
9 | if ($url =~ /\/(\d{4})\-(\d{4})[\/\-]/) { |
---|
10 | $session = $1.$2; |
---|
11 | } |
---|
12 | |
---|
13 | open(FILE, $file) ; |
---|
14 | @string = <FILE>; |
---|
15 | $string = "@string"; |
---|
16 | close FILE; |
---|
17 | |
---|
18 | #Si italique dans gras, on vire (pb fonction) |
---|
19 | if ($string =~ /M[me\.]+[ \&][^<]+<\/a>\.[^<]*<\/b>[^<]*<i>([^<]+)</ && $1 =~ /rapporteur|president/i) { |
---|
20 | $string =~ s/(M[me\.]+[ \&][^<]+<\/a>)\.[^<]*<\/b>[^<]*<i>/$1,<\/b><i>/g; |
---|
21 | } |
---|
22 | $string =~ s/([^\.])\s*<\/b>\s*<i>([^<]+)<\/i>/$1 $2<\/b>/g; |
---|
23 | |
---|
24 | $string =~ s/<\/?b>/|/g; |
---|
25 | $string =~ s/<\/?i>/\//g; |
---|
26 | $string =~ s/\r//g; |
---|
27 | |
---|
28 | $mois{'janvier'} = '01'; |
---|
29 | $mois{'février'} = '02'; |
---|
30 | $mois{'mars'} = '03'; |
---|
31 | $mois{'avril'} = '04'; |
---|
32 | $mois{'mai'} = '05'; |
---|
33 | $mois{'juin'} = '06'; |
---|
34 | $mois{'juillet'} = '07'; |
---|
35 | $mois{'août'} = '08'; |
---|
36 | $mois{'septembre'} = '09'; |
---|
37 | $mois{'octobre'} = '10'; |
---|
38 | $mois{'novembre'} = '11'; |
---|
39 | $mois{'décembre'} = '12'; |
---|
40 | |
---|
41 | $heure{'neuf'} = '09'; |
---|
42 | $heure{'dix'} = '10'; |
---|
43 | $heure{'onze'} = '11'; |
---|
44 | $heure{'douze'} = '12'; |
---|
45 | $heure{'treize'} = '13'; |
---|
46 | $heure{'quatorze'} = '14'; |
---|
47 | $heure{'quinze'} = '15'; |
---|
48 | $heure{'seize'} = '16'; |
---|
49 | $heure{'dix-sept'} = '17'; |
---|
50 | $heure{'dix-huit'} = '18'; |
---|
51 | $heure{'dix-neuf'} = '19'; |
---|
52 | $heure{'vingt'} = '20'; |
---|
53 | $heure{'vingt et une'} = '21'; |
---|
54 | $heure{'vingt-deux'} = '22'; |
---|
55 | $heure{'quarante'} = '45'; |
---|
56 | $heure{'quarante-cinq'} = '45'; |
---|
57 | $heure{'trente'} = '30'; |
---|
58 | $heure{'trente-cinq'} = '35'; |
---|
59 | $heure{'quinze'} = '15'; |
---|
60 | $heure{'zéro'} = '00'; |
---|
61 | $heure{'cinq'} = '00'; |
---|
62 | $heure{''} = '00'; |
---|
63 | |
---|
64 | if ($string =~ /ance est ouverte/i) { |
---|
65 | $heure = '09:30'; |
---|
66 | } |
---|
67 | if ($string =~ /ouverte[^\.]+à ([^\.]+) heures?\s*([^\.]*)\./) { |
---|
68 | $heure = $heure{$1}.':'.$heure{$2}; |
---|
69 | } |
---|
70 | |
---|
71 | sub savepLoi() { |
---|
72 | $no =~ s/ / /g; |
---|
73 | $no =~ s/\s*et\s*/,/g; |
---|
74 | $no =~ s/[^\d,]//g; |
---|
75 | @no = split(/,/, $no); |
---|
76 | $no = ''; |
---|
77 | foreach (@no) { |
---|
78 | s/(\d{4})(\d{4})/$1,$2/g; |
---|
79 | s/(\d{3})(\d{3})/$1,$2/g; |
---|
80 | s/^0+//; |
---|
81 | s/,0+//; |
---|
82 | $no .= $_.','; |
---|
83 | } |
---|
84 | chop $no; |
---|
85 | if ($no) { |
---|
86 | # print "TEST3 $titre -_- $no\n"; |
---|
87 | $ploi{$titre} = $no; |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | $string =~ s/’/'/g; |
---|
92 | $string =~ s/<\/?sup>//g; |
---|
93 | $string =~ s/<!--[^A-Z]+-->//g; |
---|
94 | #Recherche des numéros de de loi |
---|
95 | while($string =~ /#item#\d+\.?\s*([^#]+)\(n\D+(\d+[^\)]+)\)\s*#\/item#/ig) { |
---|
96 | # print "TEST1 $1 -_- $2 \n"; |
---|
97 | $titre = lc $1; |
---|
98 | $no = $2; |
---|
99 | savepLoi(); |
---|
100 | } |
---|
101 | while($string =~ /ordre du jour([^<]+\W(proposition|loi)\W[^<]+)\(n\D+(\d+[^\)]+)\)/ig) { |
---|
102 | if ($1 =~ /#item#/i) { |
---|
103 | next; |
---|
104 | } |
---|
105 | # print "TEST2 $1 -_- $2 -_- $3\n"; |
---|
106 | $titre = lc $1; |
---|
107 | $no = $3; |
---|
108 | $titre =~ s/[^<]+ loi,? //; |
---|
109 | savepLoi(); |
---|
110 | } |
---|
111 | |
---|
112 | sub getProjetLoi { |
---|
113 | $titre_cleaned = $titre = lc shift; |
---|
114 | return unless ($titre); |
---|
115 | return $ploi{$titre} if (defined($ploi{$titre})); |
---|
116 | $titre_cleaned =~ s/[^a-z]+/ /g; |
---|
117 | while ($titre_cleaned) { |
---|
118 | foreach $k (keys %ploi) { |
---|
119 | $_ = $k; |
---|
120 | s/[^a-z]+/ /g; |
---|
121 | if (/$titre_cleaned/) { |
---|
122 | $ploi{$titre} = $ploi{$k}; |
---|
123 | return $ploi{$k}; |
---|
124 | } |
---|
125 | } |
---|
126 | $titre_cleaned =~ s/^\s?\S+\s*//; |
---|
127 | } |
---|
128 | $ploi{$titre} = ''; |
---|
129 | return ; |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | $cpt = 0; |
---|
134 | sub checkout { |
---|
135 | $cpt+=10; |
---|
136 | $contexte = $titre1; |
---|
137 | if ($titre2) { |
---|
138 | $contexte .= ' > '.$titre2; |
---|
139 | } |
---|
140 | $out = '{"contexte": "'.$contexte.'", "intervention": "'.$intervention.'", "timestamp": "'.$cpt.'", "date": "'.$date.'", "source": "'.$source.'", "heure":"'.$heure.'", "session": "'.$session.'", '; |
---|
141 | if (($ploi = getProjetLoi($titre1)) && $contexte !~ /questions?\sau|ordre\sdu\sjour|nomination|suspension\sde\séance|rappels?\sau\srèglement/i) { |
---|
142 | $out .= "\"numeros_loi\": \"$ploi\", "; |
---|
143 | } |
---|
144 | if ($amendements) { |
---|
145 | $out .= '"amendements": "'.$amendements.'", '; |
---|
146 | } |
---|
147 | |
---|
148 | if ($intervenant) { |
---|
149 | if ($intervenant =~ s/( et|, )(\s*M[mes\.]*|)\s*([A-Z].*)//) { |
---|
150 | print $out.'"intervenant": "'.$3."\"}\n"; |
---|
151 | } |
---|
152 | if ($inter2fonction{$intervenant} =~ s/( et|, )(\s*M[mes\.]*|)\s*([A-Z].*)//g) { |
---|
153 | print $out.'"intervenant": "'.$3."\"}\n"; |
---|
154 | $inter2fonction{$intervenant} = ''; |
---|
155 | } |
---|
156 | print $out.'"intervenant": "'.$intervenant.'", "fonction": "'.$inter2fonction{$intervenant}.'", "intervenant_url": "'.$intervenant_url."\"}\n"; |
---|
157 | }elsif($intervention) { |
---|
158 | print $out.'"intervenant":"'."\"}\n"; |
---|
159 | }else { |
---|
160 | return ; |
---|
161 | } |
---|
162 | $commentaire = ""; |
---|
163 | $intervenant = ""; |
---|
164 | $intervenant_url = ""; |
---|
165 | $intervention = ""; |
---|
166 | $amendements = join ',', @pre_amendements; |
---|
167 | } |
---|
168 | |
---|
169 | sub setFonction { |
---|
170 | my $fonction = shift; |
---|
171 | my $intervenant = shift; |
---|
172 | my $kfonction = lc($fonction); |
---|
173 | $kfonction =~ s/[^a-z]+/ /gi; |
---|
174 | $intervenant =~ s/\W+$//; |
---|
175 | $fonction2inter{$kfonction} = $intervenant; |
---|
176 | # print "$fonction ($kfonction) => $intervenant \n"; |
---|
177 | if (!$inter2fonction{$intervenant}) { |
---|
178 | $inter2fonction{$intervenant} = $fonction; |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | sub setIntervenant { |
---|
183 | my $intervenant = shift; |
---|
184 | # print "$intervenant\n"; |
---|
185 | $intervenant =~ s/^(M(\.|me))(\S)/$1 $2/; |
---|
186 | $intervenant =~ s/[\|\/]//g; |
---|
187 | $intervenant =~ s/\s*\&\#8211\;\s*$//; |
---|
188 | $intervenant =~ s/\s*[\.\:]\s*$//; |
---|
189 | $intervenant =~ s/Madame/Mme/; |
---|
190 | $intervenant =~ s/Monsieur/M./; |
---|
191 | $intervenant =~ s/et M\. /et M /; |
---|
192 | $intervenant =~ s/^M[\.mes]*\s//i; |
---|
193 | $intervenant =~ s/\s*\..*$//; |
---|
194 | $intervenant =~ s/L([ea])\s/l$1 /i; |
---|
195 | $intervenant =~ s/\s+\(/, /g; |
---|
196 | $intervenant =~ s/\)//g; |
---|
197 | $intervenant =~ s/[\.\,\s]+$//; |
---|
198 | $intervenant =~ s/^\s+//; |
---|
199 | $intervenant =~ s/É+/é/gi; |
---|
200 | $intervenant =~ s/\&\#8217\;/'/g; |
---|
201 | $intervenant =~ s/([^\s\,])\s+rapporteur/$1, rapporteur/i; |
---|
202 | $intervenant =~ s/M\. /M /; |
---|
203 | if ($intervenant =~ s/\,\s*(.*)//) { |
---|
204 | setFonction($1, $intervenant); |
---|
205 | } |
---|
206 | if ($intervenant =~ /^[a-z]/) { |
---|
207 | $intervenant =~ s/^l[ea]\s+//i; |
---|
208 | if ($intervenant =~ /([pP]résidente?|[rR]apporteur[a-zé\s]+)\s([A-Z].+)/) { #\s([A-Z].*)/i) { |
---|
209 | setFonction($1, $2); |
---|
210 | return $2; |
---|
211 | } |
---|
212 | $conv = $fonction2inter{$intervenant}; |
---|
213 | if (!$conv) { |
---|
214 | $tmp = $intervenant; |
---|
215 | $tmp =~ s/[^a-z]+/ /gi; |
---|
216 | $conv = $fonction2inter{$tmp}; |
---|
217 | } |
---|
218 | # print "conv: '$conv' '$intervenant'\n"; |
---|
219 | if ($conv) { |
---|
220 | $intervenant = $conv; |
---|
221 | }else { |
---|
222 | $test = lc($intervenant); |
---|
223 | $test =~ s/[^a-z]+/ /gi; |
---|
224 | foreach $fonction (keys %fonction2inter) { |
---|
225 | if ($fonction =~ /$test/) { |
---|
226 | $inter = $fonction2inter{$fonction}; |
---|
227 | last; |
---|
228 | } |
---|
229 | } |
---|
230 | if (!$inter) { |
---|
231 | foreach $fonction (keys %fonction2inter) { |
---|
232 | if ($test =~ /$fonction/) { |
---|
233 | $inter = $fonction2inter{$fonction}; |
---|
234 | last; |
---|
235 | } |
---|
236 | } |
---|
237 | } |
---|
238 | if ($inter) { |
---|
239 | $fonction2inter{lc($intervenant)} = $inter; |
---|
240 | $intervenant = $inter; |
---|
241 | } |
---|
242 | } |
---|
243 | } |
---|
244 | return $intervenant; |
---|
245 | } |
---|
246 | |
---|
247 | $string =~ s/\r//g; |
---|
248 | $string =~ s/ / /g; |
---|
249 | $string =~ s/œ|œ+/oe/g; |
---|
250 | $string =~ s/’/'/g; |
---|
251 | $string =~ s/\|(\W+)\|/$1/g; |
---|
252 | $majIntervenant = 0; |
---|
253 | $debut = 0; |
---|
254 | |
---|
255 | $string =~ s/<br>\n*//gi; |
---|
256 | $string =~ s/<\/?orateur>\n*//gi; |
---|
257 | $string =~ s/\| et \|/ et /gi; |
---|
258 | |
---|
259 | #print "$string\n"; exit; |
---|
260 | |
---|
261 | $donetitre1 = 0; |
---|
262 | foreach $line (split /\n/, $string) |
---|
263 | { |
---|
264 | if ($line =~ /DEBUT_SEANCE|séance est ouverte/) { |
---|
265 | $debut = 1; |
---|
266 | } |
---|
267 | #recherche amendements |
---|
268 | if ($line =~ /\<\!\-\- AMEND_/) { |
---|
269 | @pre_amendements = (); |
---|
270 | while ($line =~ /\<\!\-\- AMEND_(\d+)\D/g) { |
---|
271 | push @pre_amendements, $1; |
---|
272 | } |
---|
273 | } |
---|
274 | |
---|
275 | #suppression des commentaires |
---|
276 | $line =~ s/\<\!\-\-[^\>]+\>//g; |
---|
277 | #si deux intervenant en même temps |
---|
278 | $line =~ s/\|\s*et\s*\|/ et /gi; |
---|
279 | #si italique ou gras sans raison on supprime |
---|
280 | $line =~ s/\/\s*\// /g; |
---|
281 | $line =~ s/\|\s*\|/ /g; |
---|
282 | |
---|
283 | #récupère les ancres pour de meilleurs liens sources |
---|
284 | if ($line =~ /\<[a]/i) { |
---|
285 | if ($line =~ s/<a name=["']([^"']+)["'][^<]+<[^>]+>/<</g) { |
---|
286 | $source = $url."#$1"; |
---|
287 | } |
---|
288 | } |
---|
289 | |
---|
290 | if ($line =~ /<h[1-9]+/i || $line =~ /"(sompresidence|sstitreinfo)"/) { |
---|
291 | if ($line =~ /pr..?sidence de (M[^<\,]+)[<,]/i && $line !~ /sarkozy/i) { |
---|
292 | $prez = $1; |
---|
293 | # print "Présidence de $prez\n"; |
---|
294 | if ($prez =~ /^Mm/) { |
---|
295 | setFonction('présidente', $prez); |
---|
296 | }else { |
---|
297 | setFonction('président', $prez); |
---|
298 | } |
---|
299 | }elsif($line =~ /h2 class="titre[23]"><*([^<\(]+)\s*/ || $line =~ /class="sstitreinfo">\/([^\/]+)\//) { |
---|
300 | checkout(); |
---|
301 | if (($1 !~ /suspension/ && $1 !~ /séance/) || $1 =~ /demande/i){ |
---|
302 | $titre2 = $1; |
---|
303 | } |
---|
304 | $titre2 =~ s/a href[^>]+>//g; |
---|
305 | $titre2 =~ s/\///g; |
---|
306 | $titre2 =~ s/\s+$//; |
---|
307 | $amendements = @pre_amendements = (); |
---|
308 | $line = "<p>|$titre2|</p>"; |
---|
309 | $donetitre1 = 0; |
---|
310 | }elsif(!$donetitre1 && $line =~ /h2 class="titre1">(.+)<\/h2/i) { |
---|
311 | checkout(); |
---|
312 | $titre = $1; |
---|
313 | $titre =~ s/<\/?[^>]+>//g; |
---|
314 | $titre =~ s/<//g; |
---|
315 | $titre =~ s/[\(\/][^\)\/]+[\)\/]//; |
---|
316 | $titre =~ s/\///g; |
---|
317 | $titre =~ s/\s+$//; |
---|
318 | unless ($titre) { |
---|
319 | next; |
---|
320 | } |
---|
321 | if ($titre =~ /^[\/\s]*[\wéè]+ \s*partie[\/\s]*(suite[\/\s]*|)$/i || $titre =~ /^\s*[\(\/]+.*[\/\)]+\s*$/) { |
---|
322 | next |
---|
323 | } |
---|
324 | $donetitre1 = 1; |
---|
325 | $titre1 = $titre; |
---|
326 | $titre2 = ''; |
---|
327 | $amendements = @pre_amendements = (); |
---|
328 | $line = "<p>|$titre1|</p>"; |
---|
329 | }elsif($line =~ /h1 class="seance"/) { |
---|
330 | if ($line =~ /(\d{1,2})[ermd]*\s+([a-zéùû]+)\s+(\d{4})/) { |
---|
331 | $date = $3.'-'.$mois{$2}.'-'.sprintf('%02d', $1); |
---|
332 | } |
---|
333 | }elsif($line =~ /h5 class="numencad"/) { |
---|
334 | $donetitre1 = 0; |
---|
335 | } |
---|
336 | } |
---|
337 | |
---|
338 | next unless ($debut); |
---|
339 | |
---|
340 | $line =~ s/<<//g; |
---|
341 | |
---|
342 | # print "$titre1 > $titre2 : $line\n" ; next; |
---|
343 | $line =~ s/\|\///; |
---|
344 | if ($line =~ /\<[p]/i) { |
---|
345 | $last_href = ''; |
---|
346 | if ($line =~ /href=["']([^"']+)["']/) { |
---|
347 | $last_href = $1; |
---|
348 | } |
---|
349 | $line =~ s/\s*\<\/?[^\>]+\>//g; |
---|
350 | last if ($line =~ /^\|annexe/i); |
---|
351 | next if ($line !~ /\w/); |
---|
352 | |
---|
353 | #si italique ou tout gras => commentaire |
---|
354 | if ($line =~ /^\s*\|.*\|\s*$/ || $line =~ /^\s*\/.*\/\s$/) { |
---|
355 | checkout() if ($intervenant); |
---|
356 | }elsif ($line =~ s/^\s*\|\s*(M[^\|\/\:]+)[\|\/\:]// ) { |
---|
357 | checkout(); |
---|
358 | $majIntervenant = 1; |
---|
359 | $intervenant = setIntervenant($1); |
---|
360 | $intervenant_url = $last_href; |
---|
361 | $found = 1; |
---|
362 | }elsif ($line =~ /^\s*\|/) { |
---|
363 | checkout() if ($intervenant); |
---|
364 | } |
---|
365 | $line =~ s/^\s+//; |
---|
366 | $line =~ s/[\|\/]//g; |
---|
367 | $line =~ s/^[\.\:]\s*//; |
---|
368 | $intervention .= "<p>$line</p>"; |
---|
369 | } |
---|
370 | } |
---|
371 | checkout(); |
---|