Commit | Line | Data |
d71100ed |
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 | # 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; |
15 | |
16 | bless( $self, $class ); |
17 | return $self; |
18 | } |
19 | |
20 | sub run_analysis { |
21 | my( $file, $stemmadot ) = @_; |
22 | # What we will return |
23 | my $svg; |
24 | my $variants = []; |
25 | |
3d79e248 |
26 | # Read in the file and stemma |
d71100ed |
27 | my $tradition = Text::Tradition->new( |
3d79e248 |
28 | 'input' => 'Self', |
29 | 'file' => $file, |
d71100ed |
30 | 'linear' => 1, |
31 | ); |
32 | my $stemma = Text::Tradition::Stemma->new( |
33 | 'collation' => $tradition->collation, |
34 | 'dot' => $stemmadot, |
35 | ); |
36 | # We will return the stemma picture |
37 | $svg = $stemma->as_svg; |
38 | ### DIRTY HACK |
39 | $svg =~ s/transform=\"scale\(1 1\)/transform=\"scale\(0.7 0.7\)/; |
40 | |
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. |
43 | |
44 | my $all_wits_table = $tradition->collation->make_alignment_table( 'refs' ); |
45 | |
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. |
49 | my $html_columns = 0; |
50 | my $html_data = []; |
51 | my $total = 0; # Keep track of the total number of variant locations |
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; |
c4e11e3f |
63 | my $lacunose = []; |
d71100ed |
64 | foreach my $j ( 0 .. $#{$col_rdgs} ) { |
65 | my $rdg = $col_rdgs->[$j]; |
d71100ed |
66 | my $rdg_text = '(omitted)'; # Initialize in case of empty reading |
67 | if( $rdg ) { |
c4e11e3f |
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 | } |
d71100ed |
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 | # Initialize the data structure for the row that we will return |
93 | my $variant_row = {'id' => $rank, 'readings' => [] }; |
94 | # Keep track of our widest row |
95 | $html_columns = scalar @$groups if scalar @$groups > $html_columns; |
96 | |
97 | # We can already look up witnesses for a reading; we also want to look |
98 | # up readings for a given witness. |
99 | my $group_readings = {}; |
100 | foreach my $x ( 0 .. $#$groups ) { |
101 | $group_readings->{wit_stringify( $groups->[$x] )} = $readings->[$x]; |
102 | } |
103 | |
104 | # For all the groups with more than one member, collect the list of all |
105 | # contiguous vertices needed to connect them. |
106 | # TODO: deal with a.c. reading logic |
107 | my $sc = analyze_variant_location( $group_readings, $groups, $stemma->apsp ); |
108 | $variant_row->{'genealogical'} = keys %$sc ? 1 : undef; |
109 | foreach my $grp ( sort keys %$group_readings ) { |
110 | my $rdg = $group_readings->{$grp}; |
c4e11e3f |
111 | push( @{$variant_row->{'readings'}}, |
112 | { 'text' => $rdg, 'group' => $grp, |
113 | 'missing' => wit_stringify( $lacunose ) } ); |
d71100ed |
114 | } |
115 | |
116 | # Now run the same analysis given the calculated distance tree(s). |
117 | # foreach my $tree ( 0 .. $#{$stemma->distance_trees} ) { |
118 | # my $dc = analyze_variant_location( $group_readings, $groups, |
119 | # $stemma->distance_apsps->[$tree] ); |
120 | # foreach my $rdg ( keys %$dc ) { |
121 | # my $var = $dc->{$rdg}; |
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 and add the number of empty columns we need. |
130 | foreach my $row ( @$variants ) { |
131 | my $empty = $html_columns - scalar @{$row->{'readings'}}; |
132 | $row->{'empty'} = $empty; |
133 | } |
134 | |
135 | return( $svg, $variants ); |
136 | } |
137 | |
138 | sub analyze_variant_location { |
139 | my( $group_readings, $groups, $apsp ) = @_; |
140 | my %contig; |
141 | my $conflict = {}; |
142 | foreach my $g ( sort { scalar @$b <=> scalar @$a } @$groups ) { |
143 | my @members = @$g; |
144 | my $gst = wit_stringify( $g ); |
145 | map { $contig{$_} = $gst } @members; # The witnesses need themselves to be |
146 | # in their collection. |
147 | next unless @members > 1; |
148 | my $curr = pop @members; |
149 | foreach my $m ( @members ) { |
150 | foreach my $v ( $apsp->path_vertices( $curr, $m ) ) { |
151 | $contig{$v} = $gst unless exists $contig{$v}; |
152 | next if $contig{$v} eq $gst; |
153 | # print STDERR "Conflict at $v between group $gst and group " |
154 | # . $contig{$v} . "\n"; |
155 | # Record what is conflicting. |
156 | $conflict->{$group_readings->{$gst}} = $group_readings->{$contig{$v}}; |
157 | } |
158 | } |
159 | } |
160 | return $conflict; |
161 | } |
162 | |
163 | # Add the variant, subject to a.c. representation logic. |
164 | # This assumes that we will see the 'main' version before the a.c. version. |
165 | sub add_variant_wit { |
166 | my( $arr, $wit, $acstr ) = @_; |
167 | my $skip; |
168 | if( $wit =~ /^(.*)\Q$acstr\E$/ ) { |
169 | my $real = $1; |
170 | $skip = grep { $_ =~ /^\Q$real\E$/ } @$arr; |
171 | } |
172 | push( @$arr, $wit ) unless $skip; |
173 | } |
174 | |
175 | # Return an answer if the variant is useful, i.e. if there are at least 2 variants |
176 | # with at least 2 witnesses each. |
177 | sub useful_variant { |
178 | my( $readings ) = @_; |
179 | my $total = keys %$readings; |
180 | foreach my $var ( keys %$readings ) { |
181 | $total-- if @{$readings->{$var}} == 1; |
182 | } |
183 | return( undef, undef ) if $total <= 1; |
184 | my( $groups, $text ); |
185 | foreach my $var ( keys %$readings ) { |
186 | push( @$groups, $readings->{$var} ); |
187 | push( @$text, $var ); |
188 | } |
189 | return( $groups, $text ); |
190 | } |
191 | |
192 | # Take an array of witness groupings and produce a string like |
193 | # ['A','B'] / ['C','D','E'] / ['F'] |
194 | |
195 | sub wit_stringify { |
196 | my $groups = shift; |
197 | my @gst; |
198 | # If we were passed an array of witnesses instead of an array of |
199 | # groupings, then "group" the witnesses first. |
200 | unless( ref( $groups->[0] ) ) { |
201 | my $mkgrp = [ $groups ]; |
202 | $groups = $mkgrp; |
203 | } |
204 | foreach my $g ( @$groups ) { |
205 | push( @gst, '[' . join( ',', map { "'$_'" } @$g ) . ']' ); |
206 | } |
207 | return join( ' / ', @gst ); |
208 | } |
209 | |
210 | 1; |