chiark / gitweb /
wip
[appendix-a6.git] / parse
1 #!/usr/bin/perl -w
2 use strict;
3 use utf8;
4
5 use Data::Printer;
6
7 binmode STDIN, 'encoding(UTF-8)' or die;
8 binmode STDOUT, 'encoding(UTF-8)' or die;
9 binmode STDERR, 'encoding(UTF-8)' or die;
10
11 our @choices;
12 our %choices;
13 our @invotes;
14
15 sub addchoice {
16     my $cho = shift @_;
17     $choices{$cho} = { @_, Index => (scalar @choices) };
18     push @choices, $cho;
19 }
20
21 while (<>) {
22     s/\s+$//;
23     next if m/^\s*\#/;
24     next unless m/\S/;
25     if (m/^([A-Z]+)\s*\=\s*(\S.*)$/) {
26         my ($cho, $desc) = ($1,$2);
27         my $smaj;
28         if ($desc =~ m/\[(\d+):(\d+)\]/) {
29             $smaj = [$1,$2];
30         } 
31         addchoice($cho, Desc => $desc, Smaj => $smaj);
32     } elsif (m/^V:\s+(\S+)\s+(\S.*)/) {
33         push @invotes, [ $1, $2 ];
34     } else {
35         die "invalid input";
36     }
37 }
38
39 if (!$choices{FD}) {
40     addchoice('FD', Desc => "Further Discussion");
41 }
42
43 our $chofd = $choices{FD};
44
45 die "FD has smaj?!" if $chofd->{Smaj};
46
47 our @vab; # $vab[$ia][$ib] = V(A,B)
48
49 foreach my $iv (@invotes) {
50     my ($votestr,$voter) = @$iv;
51     eval {
52         length $votestr eq @choices or die "wrong vote vector length";
53         my @vs = split //, $votestr;
54         foreach my $ix (0..$#vs) {
55             my $vchr = $vs[$ix];
56             if ($vchr eq '-') {
57                 $vs[$ix] = 1000;
58             } elsif ($vchr =~ m/[0-9a-z]/) {
59                 $vs[$ix] = ord($vchr);
60             } else {
61                 die "bad vote char";
62             }
63         }
64         foreach my $ia (0..$#vs) {
65             foreach my $ib (0..$#vs) {
66                 my $va = $vs[$ia];
67                 my $vb = $vs[$ib];
68                 if ($va < $vb) { $vab[$ia][$ib]++; }
69                 elsif ($vb < $va) { $vab[$ib][$ia]++; }
70             }
71         }
72     }
73     die "voter $voter $@" if $@;
74 }
75
76 print p %choices;
77 print p @vab;