1 package Text::Tradition::Analysis;
6 use Text::Tradition::Stemma;
9 my( $class, $args ) = @_;
11 # Our object needs to have a stemma graph and a variant table.
12 my( $svg, $variants ) = run_analysis( $args->{'file'}, $args->{'stemmadot'} );
13 $self->{'svg'} = $svg;
14 $self->{'variants'} = $variants;
16 bless( $self, $class );
21 my( $file, $stemmadot ) = @_;
26 # Read in the file and stemma
27 my $tradition = Text::Tradition->new(
32 my $stemma = Text::Tradition::Stemma->new(
33 'collation' => $tradition->collation,
36 # We will return the stemma picture
37 $svg = $stemma->as_svg;
39 $svg =~ s/transform=\"scale\(1 1\)/transform=\"scale\(0.7 0.7\)/;
41 # We have the collation, so get the alignment table with witnesses in rows.
42 # Also return the reading objects in the table, rather than just the words.
44 my $all_wits_table = $tradition->collation->make_alignment_table( 'refs' );
46 # For each column in the alignment table, we want to see if the existing
47 # groupings of witnesses match our stemma hypothesis. We also want, at the
48 # end, to produce an HTML table with all the variants.
51 my $total = 0; # Keep track of the total number of variant locations
53 # Strip the list of sigla and save it for correlation to the readings.
54 my $col_wits = shift @$all_wits_table;
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.
61 my $col_rdgs = shift @$all_wits_table;
63 foreach my $j ( 0 .. $#{$col_rdgs} ) {
64 my $rdg = $col_rdgs->[$j];
65 my $rdg_text = '(omitted)'; # Initialize in case of empty reading
67 $rdg_text = $rdg->is_lacuna ? undef : $rdg->text; # Don't count lacunae
68 # Get the rank from any real reading; they should be identical.
69 $rank = $rdg->rank unless $rank || $rdg->is_lacuna;
71 if( defined $rdg_text ) {
72 # Initialize the witness array if we haven't got one yet
73 $rdg_wits->{$rdg_text} = [] unless $rdg_wits->{$rdg_text};
74 # Add the relevant witness, subject to a.c. logic
75 add_variant_wit( $rdg_wits->{$rdg_text}, $col_wits->[$j],
76 $tradition->collation->ac_label );
80 # See if this column has any potentially genealogical variants.
81 # If not, skip to the next.
82 $total++ unless scalar keys %$rdg_wits == 1;
83 my( $groups, $readings ) = useful_variant( $rdg_wits );
84 next unless $groups && $readings;
86 # Initialize the data structure for the row that we will return
87 my $variant_row = {'id' => $rank, 'readings' => [] };
88 # Keep track of our widest row
89 $html_columns = scalar @$groups if scalar @$groups > $html_columns;
91 # We can already look up witnesses for a reading; we also want to look
92 # up readings for a given witness.
93 my $group_readings = {};
94 foreach my $x ( 0 .. $#$groups ) {
95 $group_readings->{wit_stringify( $groups->[$x] )} = $readings->[$x];
98 # For all the groups with more than one member, collect the list of all
99 # contiguous vertices needed to connect them.
100 # TODO: deal with a.c. reading logic
101 my $sc = analyze_variant_location( $group_readings, $groups, $stemma->apsp );
102 $variant_row->{'genealogical'} = keys %$sc ? 1 : undef;
103 foreach my $grp ( sort keys %$group_readings ) {
104 my $rdg = $group_readings->{$grp};
105 push( @{$variant_row->{'readings'}}, { 'text' => $rdg, 'group' => $grp } );
108 # Now run the same analysis given the calculated distance tree(s).
109 # foreach my $tree ( 0 .. $#{$stemma->distance_trees} ) {
110 # my $dc = analyze_variant_location( $group_readings, $groups,
111 # $stemma->distance_apsps->[$tree] );
112 # foreach my $rdg ( keys %$dc ) {
113 # my $var = $dc->{$rdg};
117 # Record that we used this variant in an analysis
118 push( @$variants, $variant_row );
121 # Go through our variant rows and add the number of empty columns we need.
122 foreach my $row ( @$variants ) {
123 my $empty = $html_columns - scalar @{$row->{'readings'}};
124 $row->{'empty'} = $empty;
127 return( $svg, $variants );
130 sub analyze_variant_location {
131 my( $group_readings, $groups, $apsp ) = @_;
134 foreach my $g ( sort { scalar @$b <=> scalar @$a } @$groups ) {
136 my $gst = wit_stringify( $g );
137 map { $contig{$_} = $gst } @members; # The witnesses need themselves to be
138 # in their collection.
139 next unless @members > 1;
140 my $curr = pop @members;
141 foreach my $m ( @members ) {
142 foreach my $v ( $apsp->path_vertices( $curr, $m ) ) {
143 $contig{$v} = $gst unless exists $contig{$v};
144 next if $contig{$v} eq $gst;
145 # print STDERR "Conflict at $v between group $gst and group "
146 # . $contig{$v} . "\n";
147 # Record what is conflicting.
148 $conflict->{$group_readings->{$gst}} = $group_readings->{$contig{$v}};
155 # Add the variant, subject to a.c. representation logic.
156 # This assumes that we will see the 'main' version before the a.c. version.
157 sub add_variant_wit {
158 my( $arr, $wit, $acstr ) = @_;
160 if( $wit =~ /^(.*)\Q$acstr\E$/ ) {
162 $skip = grep { $_ =~ /^\Q$real\E$/ } @$arr;
164 push( @$arr, $wit ) unless $skip;
167 # Return an answer if the variant is useful, i.e. if there are at least 2 variants
168 # with at least 2 witnesses each.
170 my( $readings ) = @_;
171 my $total = keys %$readings;
172 foreach my $var ( keys %$readings ) {
173 $total-- if @{$readings->{$var}} == 1;
175 return( undef, undef ) if $total <= 1;
176 my( $groups, $text );
177 foreach my $var ( keys %$readings ) {
178 push( @$groups, $readings->{$var} );
179 push( @$text, $var );
181 return( $groups, $text );
184 # Take an array of witness groupings and produce a string like
185 # ['A','B'] / ['C','D','E'] / ['F']
190 # If we were passed an array of witnesses instead of an array of
191 # groupings, then "group" the witnesses first.
192 unless( ref( $groups->[0] ) ) {
193 my $mkgrp = [ $groups ];
196 foreach my $g ( @$groups ) {
197 push( @gst, '[' . join( ',', map { "'$_'" } @$g ) . ']' );
199 return join( ' / ', @gst );