#!/usr/bin/perl use strict; use warnings; use XML::Twig; use HTML::Entities; my $file_analysis = 'dadvsi_ams_analyse.txt'; my $file_votes = 'dadvsi_scrutins_publics.txt'; my $file_mps = 'mon_depute.html'; # Parse $file_mps to build hash of MPs my %mps; my $twig_mps = new XML::Twig(keep_encoding=>1); unless ($twig_mps->safe_parsefile($file_mps)) { die "Error parsing $file_mps: $@\n"; } my $body = $twig_mps->root->first_child('body'); foreach my $department ($body->children('table')) { (my $department_name = $department->prev_sibling('h2')->trimmed_text) =~ s/ \(\d+\)$//o; my $department_id = $department->prev_sibling('h2')->id; foreach my $mp_line ($department->children('tr')) { next if $mp_line->first_child->gi ne 'td'; my $constituency_elt = $mp_line->first_child('td'); my $name_elt = $constituency_elt->next_sibling('td'); (my $name = $name_elt->trimmed_text) =~ s/^([^ ]+ )//o; $name = decode_entities($name); my $civility = $1; my $grp_elt = $name_elt->next_sibling('td'); my $tel_elt = $grp_elt->next_sibling('td'); my $fax_elt = $tel_elt->next_sibling('td'); my $addr_elt = $fax_elt->next_sibling('td'); $mps{$name} = { dep_name => $department_name, dep_id => $department_id, const => $constituency_elt->trimmed_text, civ => $civility, grp => $grp_elt->trimmed_text, tel => $tel_elt->trimmed_text, fax => $fax_elt->trimmed_text, addr => $addr_elt->trimmed_text, ams => 0, votes => 0, }; } } # Parse $file_analysis open ANALYSIS, $file_analysis or die "Cannot read $file_analysis: $!\n"; while () { next unless /^{.+}/o; # skip non voted amendments next unless /adopté|retiré/o; # extract amendment infos my ($result, $score, $am_num, $list_mps) = /^{[+-]\[([^\]]+)\] %[^%]+%([.+ -]+) (\d+(?: \(Seconde délibération\))?)%% \(([^)]+)\)[+-]}$/o; my $score_mp = 0; if ($score eq '++') { $score_mp = 4; } elsif ($score eq '+') { $score_mp = 1; } elsif ($score eq '- -') { $score_mp = -4; } elsif ($score eq '-') { $score_mp = -1; } $score_mp *= 2 if $result eq 'adopté'; $score_mp *= 4 if $am_num =~ /Seconde/o; $list_mps =~ s/MM?\. //go; $list_mps =~ s/Mme //o; $list_mps =~ s/,[^,]+$//o; $list_mps =~ s/ et les membres.+$//o; $list_mps =~ s/, rapporteur$//o; $list_mps =~ s/ rapporteur, UMP Nord \+//o; foreach my $mp (split /(?:,| et) /, $list_mps) { next if $mp eq 'Gouvernement'; $mps{$mp}->{ams} += $score_mp; } } close ANALYSIS; # Parse votes my %weights = ( 882 => 10, 883 => 8, 884 => 4, 885 => 4, 886 => -4, 943 => 8, 946 => 8, 947 => 6, 948 => 0, 949 => -8, 950 => -4, 951 => 0, 953 => 2, 954 => 6, 955 => 6, 956 => 6, 957 => 8, 958 => -10, 960 => 4, 961 => -8, 962 => -10, 963 => 2, 964 => -10, ); open VOTES, $file_votes or die "Cannot read $file_votes: $!\n"; my $current_vote = 0; while () { if (/^- scrutin n° (\d\d\d)/) { $current_vote = $1; } if (s/^ (Pour|Contre|Abstention) : //o) { my $position = $1; $position = 1 if $position eq 'Pour'; $position = -1 if $position eq 'Contre'; $position = -0.5 if $position eq 'Abstention'; my $score_mp = $position * $weights{$current_vote}; s/^\d+\. - //o; s/MM?\. //go; s/Mmes? //go; s/\d+ membres? du groupe, présents? ou ayant délégué (?:leur|son) droit de vote//o; s/ ; présents? pendant la séance : //o; s/\.\n$//o; foreach my $mp (split /(?:,| et) /) { $mps{$mp}->{votes} += $score_mp; } } } close VOTES; foreach my $mp (keys %mps) { #die "ICI <$mp>\n" unless defined $mps{$mp}->{ams}; $mps{$mp}->{score} = (($mps{$mp}->{votes} + 136) * 12/272) + (($mps{$mp}->{ams} + 16) * 8/257); } my $rank = 1; foreach my $mp (sort {$mps{$b}->{score} <=> $mps{$a}->{score}} keys %mps) { printf "%3d %s: %.2f\n", $rank++, $mp, $mps{$mp}->{score}; #printf "%3d %s: %.2f = %.2f+%.2f\n", $rank++, $mp, $mps{$mp}->{score}, $mps{$mp}->{votes}, $mps{$mp}->{ams}; } #print "\n-------------------\n"; #foreach my $mp (sort {$mps{$b}->{ams} <=> $mps{$a}->{ams}} keys %mps) { # printf "%s: %.2f\n", $mp, $mps{$mp}->{ams} if $mps{$mp}->{ams}; #}