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
28 open( INFILE, "$file" ) or die "Could not read $file";
29 binmode INFILE, ':utf8';
33 my $tradition = Text::Tradition->new(
34 'Self' => join( '', @lines ),
37 my $stemma = Text::Tradition::Stemma->new(
38 'collation' => $tradition->collation,
41 # We will return the stemma picture
42 $svg = $stemma->as_svg;
44 $svg =~ s/transform=\"scale\(1 1\)/transform=\"scale\(0.7 0.7\)/;
46 # We have the collation, so get the alignment table with witnesses in rows.
47 # Also return the reading objects in the table, rather than just the words.
49 my $all_wits_table = $tradition->collation->make_alignment_table( 'refs' );
51 # For each column in the alignment table, we want to see if the existing
52 # groupings of witnesses match our stemma hypothesis. We also want, at the
53 # end, to produce an HTML table with all the variants.
56 my $total = 0; # Keep track of the total number of variant locations
58 # Strip the list of sigla and save it for correlation to the readings.
59 my $col_wits = shift @$all_wits_table;
61 # We will return a data structure, an array for each row that looks like:
62 # { id = X, genealogical = Y, readings = [ text = X, group = Y], empty = N }
63 foreach my $i ( 0 .. $#$all_wits_table ) {
64 # For each column in the table, group the readings by witness.
66 my $col_rdgs = shift @$all_wits_table;
68 foreach my $j ( 0 .. $#{$col_rdgs} ) {
69 my $rdg = $col_rdgs->[$j];
70 $rank = $rdg->rank if $rdg && !$rank; # Make a note of our rank
71 my $rdg_text = '(omitted)'; # Initialize in case of empty reading
73 $rdg_text = $rdg->is_lacuna ? undef : $rdg->text; # Don't count lacunae
75 if( defined $rdg_text ) {
76 # Initialize the witness array if we haven't got one yet
77 $rdg_wits->{$rdg_text} = [] unless $rdg_wits->{$rdg_text};
78 # Add the relevant witness, subject to a.c. logic
79 add_variant_wit( $rdg_wits->{$rdg_text}, $col_wits->[$j],
80 $tradition->collation->ac_label );
84 # See if this column has any potentially genealogical variants.
85 # If not, skip to the next.
86 $total++ unless scalar keys %$rdg_wits == 1;
87 my( $groups, $readings ) = useful_variant( $rdg_wits );
88 next unless $groups && $readings;
90 # Initialize the data structure for the row that we will return
91 my $variant_row = {'id' => $rank, 'readings' => [] };
92 # Keep track of our widest row
93 $html_columns = scalar @$groups if scalar @$groups > $html_columns;
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];
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 my $sc = analyze_variant_location( $group_readings, $groups, $stemma->apsp );
106 $variant_row->{'genealogical'} = keys %$sc ? 1 : undef;
107 foreach my $grp ( sort keys %$group_readings ) {
108 my $rdg = $group_readings->{$grp};
109 push( @{$variant_row->{'readings'}}, { 'text' => $rdg, 'group' => $grp } );
112 # Now run the same analysis given the calculated distance tree(s).
113 # foreach my $tree ( 0 .. $#{$stemma->distance_trees} ) {
114 # my $dc = analyze_variant_location( $group_readings, $groups,
115 # $stemma->distance_apsps->[$tree] );
116 # foreach my $rdg ( keys %$dc ) {
117 # my $var = $dc->{$rdg};
121 # Record that we used this variant in an analysis
122 push( @$variants, $variant_row );
125 # Go through our variant rows and add the number of empty columns we need.
126 foreach my $row ( @$variants ) {
127 my $empty = $html_columns - scalar @{$row->{'readings'}};
128 $row->{'empty'} = $empty;
131 return( $svg, $variants );
134 sub analyze_variant_location {
135 my( $group_readings, $groups, $apsp ) = @_;
138 foreach my $g ( sort { scalar @$b <=> scalar @$a } @$groups ) {
140 my $gst = wit_stringify( $g );
141 map { $contig{$_} = $gst } @members; # The witnesses need themselves to be
142 # in their collection.
143 next unless @members > 1;
144 my $curr = pop @members;
145 foreach my $m ( @members ) {
146 foreach my $v ( $apsp->path_vertices( $curr, $m ) ) {
147 $contig{$v} = $gst unless exists $contig{$v};
148 next if $contig{$v} eq $gst;
149 # print STDERR "Conflict at $v between group $gst and group "
150 # . $contig{$v} . "\n";
151 # Record what is conflicting.
152 $conflict->{$group_readings->{$gst}} = $group_readings->{$contig{$v}};
159 # Add the variant, subject to a.c. representation logic.
160 # This assumes that we will see the 'main' version before the a.c. version.
161 sub add_variant_wit {
162 my( $arr, $wit, $acstr ) = @_;
164 if( $wit =~ /^(.*)\Q$acstr\E$/ ) {
166 $skip = grep { $_ =~ /^\Q$real\E$/ } @$arr;
168 push( @$arr, $wit ) unless $skip;
171 # Return an answer if the variant is useful, i.e. if there are at least 2 variants
172 # with at least 2 witnesses each.
174 my( $readings ) = @_;
175 my $total = keys %$readings;
176 foreach my $var ( keys %$readings ) {
177 $total-- if @{$readings->{$var}} == 1;
179 return( undef, undef ) if $total <= 1;
180 my( $groups, $text );
181 foreach my $var ( keys %$readings ) {
182 push( @$groups, $readings->{$var} );
183 push( @$text, $var );
185 return( $groups, $text );
188 # Take an array of witness groupings and produce a string like
189 # ['A','B'] / ['C','D','E'] / ['F']
194 # If we were passed an array of witnesses instead of an array of
195 # groupings, then "group" the witnesses first.
196 unless( ref( $groups->[0] ) ) {
197 my $mkgrp = [ $groups ];
200 foreach my $g ( @$groups ) {
201 push( @gst, '[' . join( ',', map { "'$_'" } @$g ) . ']' );
203 return join( ' / ', @gst );