CollateX: no ranks if nonlinear
[scpubgit/stemmatology.git] / lib / Text / Tradition / Analysis.pm
1 package Text::Tradition::Analysis;
2
3 use strict;
4 use warnings;
5 use Text::Tradition;
6 use Text::Tradition::Stemma;
7
8 sub new {
9         my( $class, $args ) = @_;
10         my $self = {};
11         bless( $self, $class ); 
12         $self->{'data'} = [];
13         foreach my $t ( @{$args->{'traditions'}} ) {
14             $self->run_analysis( $t->{'file'}, $t->{'stemmadot'} );
15         }
16         return $self;
17 }
18
19 sub run_analysis {
20         my( $self, $file, $stemmadot ) = @_;
21         # What we will return
22         my $svg;
23         my $variants = [];
24         my $data = {};
25         
26         # Read in the file and stemma   
27         my $tradition = Text::Tradition->new( 
28                 'input'  => 'Self',
29                 'file'   => $file,
30                 'linear' => 1,
31                 );
32         $data->{'title'} = $tradition->name;
33         
34         my $stemma = Text::Tradition::Stemma->new(
35                 'collation' => $tradition->collation,
36                 'dot' => $stemmadot,
37                 );
38         # We will return the stemma picture
39         $svg = $stemma->as_svg( { size => "8,7.5" } );;
40         $data->{'svg'} = $svg;
41         
42         # We have the collation, so get the alignment table with witnesses in rows.
43         # Also return the reading objects in the table, rather than just the words.
44         
45         my $all_wits_table = $tradition->collation->make_alignment_table( 'refs' );
46         
47         # For each column in the alignment table, we want to see if the existing
48         # groupings of witnesses match our stemma hypothesis. We also want, at the
49         # end, to produce an HTML table with all the variants.
50         my $html_columns = 0;
51         my ( $total, $genealogical, $conflicts ) = ( 0, 0, 0 );
52         
53         # Strip the list of sigla and save it for correlation to the readings.
54         my $col_wits = shift @$all_wits_table;
55         
56         # We will return a data structure, an array for each row that looks like:
57         # { id = X, genealogical = Y, readings = [ text = X, group = Y], empty = N }
58         foreach my $i ( 0 .. $#$all_wits_table ) {
59                 # For each column in the table, group the readings by witness.
60                 my $rdg_wits = {};
61                 my $col_rdgs = shift @$all_wits_table;
62                 my $rank;
63                 my $lacunose = [];
64                 foreach my $j ( 0 .. $#{$col_rdgs} ) {
65                         my $rdg = $col_rdgs->[$j];
66                         my $rdg_text = '(omitted)';  # Initialize in case of empty reading
67                         if( $rdg ) {
68                             if( $rdg->is_lacuna ) {
69                                 $rdg_text = undef;   # Don't count lacunae
70                                 push( @$lacunose, $col_wits->[$j] );
71                             } else {
72                                 $rdg_text = $rdg->text; 
73                                     # Get the rank from any real reading; they should be identical.
74                                     $rank = $rdg->rank;
75                                 }
76                         }
77                         if( defined $rdg_text ) {
78                                 # Initialize the witness array if we haven't got one yet
79                                 $rdg_wits->{$rdg_text} = [] unless $rdg_wits->{$rdg_text};
80                                 # Add the relevant witness, subject to a.c. logic
81                                 add_variant_wit( $rdg_wits->{$rdg_text}, $col_wits->[$j],
82                                         $tradition->collation->ac_label );
83                         }
84                 }
85                 
86                 # See if this column has any potentially genealogical variants.
87                 # If not, skip to the next.
88                 $total++ unless scalar keys %$rdg_wits == 1;
89                 my( $groups, $readings ) = useful_variant( $rdg_wits );
90                 next unless $groups && $readings;  
91                 
92                 # Keep track of our widest row
93                 $html_columns = scalar @$groups if scalar @$groups > $html_columns;
94                 
95                 # We can already look up witnesses for a reading; we also want to look
96                 # up readings for a given witness.
97                 my $group_readings = {};
98                 foreach my $x ( 0 .. $#$groups ) {
99                         $group_readings->{wit_stringify( $groups->[$x] )} = $readings->[$x];
100                 }
101                 
102                 # For all the groups with more than one member, collect the list of all
103                 # contiguous vertices needed to connect them.
104                 # TODO: deal with a.c. reading logic
105                 $DB::single = 1 if $rank == 25;
106                 my $variant_row = analyze_variant_location( $group_readings, $groups, 
107                     $stemma->apsp, $lacunose );
108                 $variant_row->{'id'} = $rank;
109                 $genealogical++ if $variant_row->{'genealogical'};
110                 $conflicts += grep { $_->{'conflict'} } @{$variant_row->{'readings'}};
111
112                 # Now run the same analysis given the calculated distance tree(s).
113 #               my @trees = @{$stemma->distance_trees};
114 #               if( @trees ) {
115 #             foreach my $tree ( 0 .. $#trees ) {
116 #                 my $dc = analyze_variant_location( $group_readings, $groups,    
117 #                                                    $stemma->distance_apsps->[$tree] );
118 #                 foreach my $rdg ( keys %$dc ) {
119 #                     my $var = $dc->{$rdg};
120 #                     # TODO Do something with this
121 #                 }
122 #             }
123 #           }
124
125                 # Record that we used this variant in an analysis
126                 push( @$variants, $variant_row );
127         }
128         
129         # Go through our variant rows, after we have seen all of them once,
130         # and add the number of empty columns needed by each.
131         foreach my $row ( @$variants ) {
132                 my $empty = $html_columns - scalar @{$row->{'readings'}};
133                 $row->{'empty'} = $empty;
134         }
135         
136         # Populate self with our analysis data.
137         $data->{'variants'} = $variants;
138         $data->{'variant_count'} = $total;
139         $data->{'conflict_count'} = $conflicts;
140         $data->{'genealogical_count'} = $genealogical;
141         push( @{$self->{'data'}}, $data );
142 }
143
144 # variant_row -> genealogical
145 #             -> readings [ { text, group, conflict, missing } ]
146
147 sub analyze_variant_location {
148     my( $group_readings, $groups, $apsp, $lacunose ) = @_;
149     my %contig;
150     my $conflict = {};
151     my %missing;
152     map { $missing{$_} = 1 } @$lacunose;
153     my $variant_row = { 'readings' => [] };
154     # Mark each ms as in its own group, first.
155     foreach my $g ( @$groups ) {
156         my $gst = wit_stringify( $g );
157         map { $contig{$_} = $gst } @$g;
158     }
159     foreach my $g ( sort { scalar @$b <=> scalar @$a } @$groups ) {
160         my @members = @$g;
161         my $gst = wit_stringify( $g ); # $gst is now the name of this group.
162         while( @members ) {
163             # Gather the list of vertices that are needed to join all members.
164             my $curr = pop @members;
165             foreach my $m ( @members ) {
166                 foreach my $v ( $apsp->path_vertices( $curr, $m ) ) {
167                     $contig{$v} = $gst unless exists $contig{$v};
168                     next if $contig{$v} eq $gst;
169                     # Record what is conflicting. TODO do we use this?
170                     $conflict->{$group_readings->{$gst}} = $group_readings->{$contig{$v}};
171                 }
172             }
173         }
174         # Write the reading.
175         my $reading = { 'text' => $group_readings->{$gst},
176                         'missing' => wit_stringify( $lacunose ),
177                         'conflict' => exists( $conflict->{$group_readings->{$gst}} ) };
178         if( $reading->{'conflict'} ) {
179             $reading->{'group'} = $gst;
180         } else {
181             my @all_vertices = grep { $contig{$_} eq $gst && !$missing{$_} } keys %contig;
182             $reading->{'group'} = wit_stringify( \@all_vertices );
183         }
184         push( @{$variant_row->{'readings'}}, $reading );
185     }
186     $variant_row->{'genealogical'} = keys %$conflict ? undef : 1;
187     return $variant_row;
188 }
189
190 # Add the variant, subject to a.c. representation logic.
191 # This assumes that we will see the 'main' version before the a.c. version.
192 sub add_variant_wit {
193     my( $arr, $wit, $acstr ) = @_;
194     my $skip;
195     if( $wit =~ /^(.*)\Q$acstr\E$/ ) {
196         my $real = $1;
197         $skip = grep { $_ =~ /^\Q$real\E$/ } @$arr;
198     } 
199     push( @$arr, $wit ) unless $skip;
200 }
201
202 # Return an answer if the variant is useful, i.e. if there are at least 2 variants
203 # with at least 2 witnesses each.
204 sub useful_variant {
205     my( $readings ) = @_;
206     my $total = keys %$readings;
207     foreach my $var ( keys %$readings ) {
208         $total-- if @{$readings->{$var}} == 1;
209     }
210     return( undef, undef ) if $total <= 1;
211     my( $groups, $text );
212     foreach my $var ( keys %$readings ) {
213         push( @$groups, $readings->{$var} );
214         push( @$text, $var );
215     }
216     return( $groups, $text );
217 }
218
219 # Take an array of witness groupings and produce a string like
220 # ['A','B'] / ['C','D','E'] / ['F']
221
222 sub wit_stringify {
223     my $groups = shift;
224     my @gst;
225     # If we were passed an array of witnesses instead of an array of 
226     # groupings, then "group" the witnesses first.
227     unless( ref( $groups->[0] ) ) {
228         my $mkgrp = [ $groups ];
229         $groups = $mkgrp;
230     }
231     foreach my $g ( @$groups ) {
232         push( @gst, '[' . join( ',', map { "'$_'" } @$g ) . ']' );
233     }
234     return join( ' / ', @gst );
235 }
236     
237 1;