Analysis.pm

filter: Statementstmt 59.5% 309 / 519 Branchbran 38.4% 80 / 208 Conditioncond 33.3% 19 / 57 MC/DCmcdc 25.0% 9 / 36 Subroutinesub 86.8% 33 / 38 Podpod 85.7% 6 / 7 totaltotal 52.7% 456 / 865 CC 161 SCAR 47.0
CC
161
Cov
52%
_resolve_parent_relationships
552.0
analyze_location
255.1
similar
72.0
CRAP 109.4
↑ Summary StemmaUtil.pm »
1 package Text::Tradition::Analysis;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Algorithm::Diff; # for word similarity measure
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Encode qw/ decode_utf8 encode_utf8 /;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Graph;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use JSON qw/ to_json decode_json /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Set::Scalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Text::Tradition::Analysis::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Text::Tradition::Stemma;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use TryCatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use vars qw/ @EXPORT_OK $VERSION /;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 @EXPORT_OK = qw/ run_analysis group_variants analyze_variant_location wit_stringify /;
18 1 $VERSION = "2.0.4";
19  
20  
21 1 my $DEFAULT_SOLVER_URL = 'http://perf.stemmaweb.net/cgi-bin/graphcalc.cgi';
22 1 my $unsolved_problems = {};
23  
24 =head1 NAME
25  
26 Text::Tradition::Analysis - functions for stemma analysis of a tradition
27  
28 =head1 DESCRIPTION
29  
30 Text::Tradition is a library for representation and analysis of collated
31 texts, particularly medieval ones. Where the Collation is the central
32 feature of a Tradition, it may also have one or more stemmata associated
33 with it, and these stemmata may be analyzed. This package provides the
34 following modules:
35  
36 =over 4
37  
38 =item * L<Text::Tradition::HasStemma> - a role that will be composed into
39 Text::Tradition objects, providing the ability for Text::Tradition::Stemma
40 objects to be associated with them.
41  
42 =item * L<Text::Tradition::Stemma> - an object class that represents stemma
43 hypotheses, both rooted (with a single archetype) and unrooted (e.g.
44 phylogenetic trees).
45  
46 =item * Text::Tradition::Analysis (this package). Provides functions for
47 the analysis of a given stemma against the collation within a given
48 Tradition.
49  
50 =back
51  
52 =head1 SYNOPSIS
53  
54 use Text::Tradition;
55 use Text::Tradition::Analysis qw/ run_analysis analyze_variant_location /;
56 my $t = Text::Tradition->new(
57 'name' => 'this is a text',
58 'input' => 'TEI',
59 'file' => '/path/to/tei_parallel_seg_file.xml' );
60 $t->add_stemma( 'dotfile' => $stemmafile );
61  
62 my $variant_data = run_analysis( $tradition );
63
64 =head1 SUBROUTINES
65  
66 =head2 run_analysis( $tradition, %opts )
67  
68 Runs the analysis described in analyze_variant_location on every location in the
69 collation of the given tradition, with the given options. These include:
70  
71 =over 4
72  
73 =item * stemma_id - Specify which of the tradition's stemmata to use. Default
74 is 0 (i.e. the first).
75  
76 =item * ranks - Specify a list of location ranks to analyze; exclude the rest.
77  
78 =item * merge_types - Specify a list of relationship types, where related readings
79 should be treated as identical for the purposes of analysis.
80  
81 =item * exclude_type1 - Exclude those ranks whose groupings have only type-1 variants.
82  
83 =back
84  
85 =begin testing
86  
87 use Text::Tradition;
88 use Text::Tradition::Analysis qw/ run_analysis analyze_variant_location /;
89  
90 my $datafile = 't/data/florilegium_tei_ps.xml';
91 my $tradition = Text::Tradition->new( 'input' => 'TEI',
92 'name' => 'test0',
93 'file' => $datafile );
94 my $s = $tradition->add_stemma( 'dotfile' => 't/data/florilegium.dot' );
95 is( ref( $s ), 'Text::Tradition::Stemma', "Added stemma to tradition" );
96  
97 my %expected_genealogical = (
98 1 => 0,
99 2 => 1,
100 3 => 0,
101 5 => 0,
102 7 => 0,
103 8 => 0,
104 10 => 0,
105 13 => 1,
106 33 => 0,
107 34 => 0,
108 37 => 0,
109 60 => 0,
110 81 => 1,
111 84 => 0,
112 87 => 0,
113 101 => 0,
114 102 => 0,
115 122 => 1,
116 157 => 0,
117 166 => 1,
118 169 => 1,
119 200 => 0,
120 216 => 1,
121 217 => 1,
122 219 => 1,
123 241 => 1,
124 242 => 1,
125 243 => 1,
126 );
127  
128 my $data = run_analysis( $tradition, calcdsn => 'dbi:SQLite:dbname=t/data/analysis.db' );
129 my $c = $tradition->collation;
130 foreach my $row ( @{$data->{'variants'}} ) {
131 # Account for rows that used to be "not useful"
132 unless( exists $expected_genealogical{$row->{'id'}} ) {
133 $expected_genealogical{$row->{'id'}} = 1;
134 }
135 my $gen_bool = $row->{'genealogical'} ? 1 : 0;
136 is( $gen_bool, $expected_genealogical{$row->{'id'}},
137 "Got correct genealogical flag for row " . $row->{'id'} );
138 # Check that we have the right row with the right groups
139 my $rank = $row->{'id'};
140 foreach my $rdghash ( @{$row->{'readings'}} ) {
141 # Skip 'readings' that aren't really
142 next unless $c->reading( $rdghash->{'readingid'} );
143 # Check the rank
144 is( $c->reading( $rdghash->{'readingid'} )->rank, $rank,
145 "Got correct reading rank" );
146 # Check the witnesses
147 my @realwits = sort $c->reading_witnesses( $rdghash->{'readingid'} );
148 my @sgrp = sort @{$rdghash->{'group'}};
149 is_deeply( \@sgrp, \@realwits, "Reading analyzed with correct groups" );
150 }
151 }
152 is( $data->{'variant_count'}, 58, "Got right total variant number" );
153 # TODO Make something meaningful of conflict count, maybe test other bits
154  
155 =end testing
156  
157 =cut
158  
159 3 ▶sub run_analysis {
Subroutine run_analysis: called CC=29 SCAR=36.5
Pod documented
160 3 my( $tradition, %opts ) = @_;
161 3 my $c = $tradition->collation;
162 3 my $aclabel = $c->ac_label;
163  
164 3 ▶ my $stemma_id = $opts{'stemma_id'} || 0;
Condition50%1/2
$opts{'stemma_id'} || 0
l!l
03
MC/DC0%0/1
$opts{'stemma_id'} || 0
$opts{'stemma_id'}
Truth table50%1/2
A
A= $opts{'stemma_id'}
Aresult
11
00
165 0 ▶ my @ranks = ref( $opts{'ranks'} ) eq 'ARRAY' ? @{$opts{'ranks'}} : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $opts{'ranks'} eq 'ARRAY' ? : 0 3 3
166 3 my $collapse = Set::Scalar->new();
167 3 ▶ if( $opts{'merge_types'} && ref( $opts{'merge_types'} ) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts{'merge_types'} and ref $opts{'merge_types'} eq 'ARRAY') { } 1 2 3
elsif ($opts{'merge_types'}) { } 0 2 2
Condition66%2/3
$opts{"merge_types"} and ref $opts{"merge_types"} eq "ARRAY"
!ll&&!rl&&r
201
MC/DC50%1/2
$opts{"merge_types"} and ref $opts{"merge_types"} eq "ARRAY"
$opts{"merge_types"} ref $opts{"merge_types"} eq "ARRAY"
Truth table66%2/3
A and B
A= $opts{"merge_types"}B= ref $opts{"merge_types"} eq "ARRAY"
ABresult
0X0
100
111
168 1 $collapse->insert( @{$opts{'merge_types'}} );
169 } elsif( $opts{'merge_types'} ) {
170 0 $collapse->insert( $opts{'merge_types'} );
171 }
172
173 # If we have specified a local lookup DB for graph calculation results,
174 # make sure it exists and connect to it.
175 3 my $dir;
176 3 ▶ if ( exists $opts{'calcdsn'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $opts{'calcdsn'}) { } 0 3 3
elsif (not exists $opts{'solver_url'}) { } 0 3 3
177 0 eval { require Text::Tradition::Directory };
178 0 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
179 0 throw( "Could not instantiate a directory for " . $opts{'calcdsn'}
180 . ": $@" );
181 }
182 0 $opts{'dir'} = Text::Tradition::Directory->new( dsn => $opts{'calcdsn'} );
183 } elsif( !exists $opts{'solver_url'} ) {
184 0 $opts{'solver_url'} = $DEFAULT_SOLVER_URL;
185 }
186  
187 # Get the stemma
188 3 my $stemma = $tradition->stemma( $stemma_id );
189  
190 # Figure out which witnesses we are working with - that is, the ones that
191 # appear both in the stemma and in the tradition. All others are 'lacunose'
192 # for our purposes.
193 3 my $lacunose = Set::Scalar->new( $stemma->hypotheticals );
194 3 my $stemma_wits = Set::Scalar->new( $stemma->witnesses );
195 3 my $tradition_wits = Set::Scalar->new( map { $_->sigil } $tradition->witnesses );
196 3 $lacunose->insert( $stemma_wits->symmetric_difference( $tradition_wits )->members );
197  
198 # Find and mark 'common' ranks for exclusion, unless they were
199 # explicitly specified.
200 3 ▶ unless( @ranks ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@ranks) 3 0 3
201 3 my %common_rank;
202 3 foreach my $rdg ( $c->common_readings ) {
203 2598 $common_rank{$rdg->rank} = 1;
204 }
205 3 @ranks = grep { !$common_rank{$_} } ( 1 .. $c->end->rank-1 );
206 }
207
208 # Group the variants to send to the solver
209 3 my @groups;
210 my @use_ranks;
211 3 my %lacunae;
212 3 my $moved = {};
213 3 foreach my $rank ( @ranks ) {
214 441 my $missing = $lacunose->clone();
215 441 my $rankgroup = group_variants( $tradition, $rank, $missing, $moved, $collapse );
216 # Filter out any empty rankgroups
217 # (e.g. from the later rank for a transposition)
218 441 ▶ next unless keys %$rankgroup;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$rankgroup 3 438 441
219 # Get the graph for this rankgroup
220 438 my $rankgraph = _graph_for_grouping( $stemma, $rankgroup, $missing, $aclabel );
221 438 ▶ if( $opts{'exclude_type1'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"exclude_type1"}) 292 146 438
222 # Check to see whether this is a "useful" group.
223 292 ▶ next unless _useful_variant( $rankgroup, $rankgraph, $aclabel );
Branch: 1 branch
BranchTrueFalseTotal
unless _useful_variant($rankgroup, $rankgraph, $aclabel) 190 102 292
224 }
225 248 push( @use_ranks, $rank );
226 248 push( @groups, { grouping => $rankgroup, graph => $rankgraph } );
227 248 $lacunae{$rank} = $missing;
228 }
229 # Run the solver
230 3 my $answer;
231 1 ▶ try {
Subroutine BEGIN: called CC=1 SCAR=0.0
232 3 $answer = solve_variants( \%opts, @groups );
233 1 ▶ } catch ( Text::Tradition::Error $e ) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Branch: 2 branches
BranchTrueFalseTotal
if ($TryCatch::Error) 3 0 3
if ('TryCatch'->check_tc('Text::Tradition::Error')) { } 3 0 3
234 3 ▶ if( $e->message =~ /IDP/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->message =~ /IDP/) { } 3 0 3
235 # Something is wrong with the solver; make the variants table anyway
236 3 $answer->{'variants'} = [];
237 3 map { push( @{$answer->{'variants'}}, _init_unsolved( $_, 'IDP error' ) ) }
238 @groups;
239 } else {
240 # Something else is wrong; error out.
241 0 $e->throw;
242 }
243 }
244  
245 0 # Do further analysis on the answer
246 3 my $conflict_count = 0;
247 3 my $reversion_count = 0;
248 248 foreach my $idx ( 0 .. $#use_ranks ) {
249 my $location = $answer->{'variants'}->[$idx];
250 248 # Add the rank back in
251 248 my $rank = $use_ranks[$idx];
252 $location->{'id'} = $rank;
253 248 # Note what our lacunae are
254 248 my %lmiss;
255 248 map { $lmiss{$_} = 1 } @{$lacunae{$use_ranks[$idx]}};
256 $location->{'missing'} = [ keys %lmiss ];
257
258 # Run the extra analysis we need.
259 ## TODO We run through all the variants in this call, so
260 248 ## why not add the reading data there instead of here below?
261 248 my $graph = $groups[$idx]->{graph};
262 analyze_location( $tradition, $graph, $location, \%lmiss );
263 248  
264 my @layerwits;
265 248 # Do the final post-analysis tidying up of the data.
266 523 ▶ foreach my $rdghash ( @{$location->{'readings'}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if $rdghash->{"is_conflict"} 0 523 523
267 523 ▶ $conflict_count++ if $rdghash->{'is_conflict'};
Branch: 1 branch
BranchTrueFalseTotal
if $rdghash->{"is_reverted"} 0 523 523
268 $reversion_count++ if $rdghash->{'is_reverted'};
269 523 # Add the reading text back in, setting display value as needed
270 523 ▶ my $rdg = $c->reading( $rdghash->{'readingid'} );
Branch: 1 branch
BranchTrueFalseTotal
if ($rdg) 423 100 523
271 423 ▶ if( $rdg ) {
Branch: 1 branch
BranchTrueFalseTotal
$rdg->rank == $rank ? : 423 0 423
272 $rdghash->{'text'} = $rdg->text .
273 423 ▶ ( $rdg->rank == $rank ? '' : ' [' . $rdg->rank . ']' );
Branch: 1 branch
BranchTrueFalseTotal
if ($rdg->does("Text::Tradition::Morphology")) 0 423 423
274 0 if( $rdg->does( 'Text::Tradition::Morphology' ) ) {
275 0 $rdghash->{'is_ungrammatical'} = $rdg->grammar_invalid;
276 $rdghash->{'is_nonsense'} = $rdg->is_nonsense;
277 }
278 }
279 # Remove lacunose witnesses from this reading's list now that the
280 523 # analysis is done
281 523 ▶ my @realgroup;
Branch: 1 branch
BranchTrueFalseTotal
unless $lmiss{$_} 3098 0 3098
282 523 map { push( @realgroup, $_ ) unless $lmiss{$_} } @{$rdghash->{'group'}};
283 $rdghash->{'group'} = \@realgroup;
284 523 # Note any layered witnesses that appear in this group
285 3098 ▶ foreach( @realgroup ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^(.*)\Q$aclabel\E$/) 0 3098 3098
286 0 if( $_ =~ /^(.*)\Q$aclabel\E$/ ) {
287 push( @layerwits, $1 );
288 }
289 }
290 248 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
if @layerwits 0 248 248
291 $location->{'layerwits'} = \@layerwits if @layerwits;
292 3 }
293 3 $answer->{'conflict_count'} = $conflict_count;
294 $answer->{'reversion_count'} = $reversion_count;
295 3
296 return $answer;
297 }
298  
299 =head2 group_variants( $tradition, $rank, $lacunose, $transposed, $merge_relationship_types )
300  
301 Groups the variants at the given $rank of the collation, treating any
302 relationships in the set $merge_relationship_types as equivalent.
303 $lacunose should be a reference to an array, to which the sigla of lacunose
304 witnesses at this rank will be appended; $transposed should be a reference
305 to a hash, wherein the identities of transposed readings and their
306 relatives will be stored.
307  
308 Returns a hash $group_readings where $rdg is attested by the witnesses listed
309 in $group_readings->{$rdg}.
310  
311 =cut
312  
313 441 ▶# Return group_readings, groups, lacunose
Subroutine group_variants: called CC=23 SCAR=31.6
Pod documented
314 441 sub group_variants {
315 441 my( $tradition, $rank, $lacunose, $transposed, $collapse ) = @_;
316 441 my $c = $tradition->collation;
317 441 my $aclabel = $c->ac_label;
318 my $table = $c->alignment_table;
319 441 # Get the alignment table readings
320 441 my %readings_at_rank;
321 441 my $check_for_gaps = Set::Scalar->new();
322 my %moved_wits;
323 441 my $has_transposition;
324 441 my @transp_acgap;
325 5733 foreach my $tablewit ( @{$table->{'alignment'}} ) {
326 5733 my $rdg = $tablewit->{'tokens'}->[$rank-1];
327 my $wit = $tablewit->{'witness'};
328 # Exclude the witness if it is "lacunose" which if we got here
329 5733 ▶ # means "not in the stemma".
Branch: 1 branch
BranchTrueFalseTotal
if _is_lacunose($wit, $lacunose, $aclabel) 0 5733 5733
330 next if _is_lacunose( $wit, $lacunose, $aclabel );
331 5733 ▶ # Note if the witness is actually in a lacuna
Branch: 2 branches
BranchTrueFalseTotal
if ($rdg and $rdg->{'t'}->is_lacuna) { } 270 5463 5733
elsif ($rdg) { } 5013 450 5463
Condition100%3/3
$rdg and $rdg->{"t"}->is_lacuna
!ll&&!rl&&r
4505013270
MC/DC100%2/2
$rdg and $rdg->{"t"}->is_lacuna
$rdg $rdg->{"t"}->is_lacuna
Truth table100%3/3
A and B
A= $rdgB= $rdg->{"t"}->is_lacuna
ABresult
0X0
100
111
332 270 if( $rdg && $rdg->{'t'}->is_lacuna ) {
333 _add_to_witlist( $wit, $lacunose, $aclabel );
334 # Otherwise the witness either has a positive reading...
335 } elsif( $rdg ) {
336 5013 ▶ # If the reading has been counted elsewhere as a transposition, ignore it.
Branch: 1 branch
BranchTrueFalseTotal
if ($$transposed{$rdg->{"t"}->id}) 36 4977 5013
337 if( $transposed->{$rdg->{'t'}->id} ) {
338 36 # TODO Does this cope with three-way transpositions?
339 36 map { $moved_wits{$_} = 1 } @{$transposed->{$rdg->{'t'}->id}};
340 next;
341 }
342 4977 # Otherwise, record it...
343 $readings_at_rank{$rdg->{'t'}->id} = $rdg->{'t'};
344 4977 # ...and grab any transpositions, and their relations.
345 4977 my @transp = grep { $_->rank != $rank } _all_related( $rdg->{'t'} );
346 36 ▶ foreach my $trdg ( @transp ) {
Branch: 1 branch
BranchTrueFalseTotal
if exists $readings_at_rank{$trdg->id} 30 6 36
347 6 next if exists $readings_at_rank{$trdg->id};
348 6 $has_transposition = 1;
349 my @affected_wits = _table_witnesses(
350 6 ▶ $table, $trdg->rank, $trdg, $lacunose, $aclabel );
Branch: 1 branch
BranchTrueFalseTotal
unless @affected_wits 0 6 6
351 6 next unless @affected_wits;
352 6 map { $moved_wits{$_} = 1 } @affected_wits;
353 my @thisloc_wits = _table_witnesses( $table, $rank, $rdg->{'t'},
354 $lacunose, $aclabel );
355 # Check to see if our affected wits have layers that do something
356 6 # wacky.
357 0 my %transploc_gaps;
358 map { $transploc_gaps{$_} = 1 }
359 6 _table_witnesses( $table, $trdg->rank, undef, $lacunose, $aclabel );
360 36 ▶ foreach my $aw ( @affected_wits ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($transploc_gaps{$aw . $aclabel}) 0 36 36
361 0 if( $transploc_gaps{$aw.$aclabel} ) {
362 0 push( @thisloc_wits, $aw.$aclabel );
363 push( @transp_acgap, $aw.$aclabel );
364 }
365 }
366 # Record which witnesses we should count as already analyzed when we
367 6 # get to the transposed reading's own rank.
368 6 $transposed->{$trdg->id} = \@thisloc_wits;
369 $readings_at_rank{$trdg->id} = $trdg;
370 }
371 # ...or it is empty, ergo a gap.
372 450 } else {
373 _add_to_witlist( $wit, $check_for_gaps, $aclabel );
374 }
375 }
376 441 # Push all the transposition layer gaps onto our list
377 $check_for_gaps->insert( @transp_acgap );
378 441 # Now remove from our 'gaps' any witnesses known to have been dealt with elsewhere.
379 441 my $gap_wits = Set::Scalar->new();
380 450 ▶ map { _add_to_witlist( $_, $gap_wits, $aclabel )
Branch: 1 branch
BranchTrueFalseTotal
unless $moved_wits{$_} 450 0 450
381 unless $moved_wits{$_} } $check_for_gaps->members;
382
383 441 # Group the readings, collapsing groups by relationship if needed.
384 441 my $grouped_readings = {};
385 foreach my $rdg ( values %readings_at_rank ) {
386 # Skip readings that have been collapsed into others.
387 678 ▶ next if exists $grouped_readings->{$rdg->id}
Branch: 1 branch
BranchTrueFalseTotal
if exists $$grouped_readings{$rdg->id} and $$grouped_readings{$rdg->id} eq "COLLAPSE" 28 650 678
Condition66%2/3
exists $$grouped_readings{$rdg->id} and $$grouped_readings{$rdg->id} eq "COLLAPSE"
!ll&&!rl&&r
650028
MC/DC50%1/2
exists $$grouped_readings{$rdg->id} and $$grouped_readings{$rdg->id} eq "COLLAPSE"
exists $$grouped_readings{$rdg->id} $$grouped_readings{$rdg->id} eq "COLLAPSE"
Truth table66%2/3
A and B
A= exists $$grouped_readings{$rdg->id}B= $$grouped_readings{$rdg->id} eq "COLLAPSE"
ABresult
0X0
100
111
388 && $grouped_readings->{$rdg->id} eq 'COLLAPSE';
389 650 # Get the witness list, including from readings collapsed into this one.
390 650 ▶ my @wits = _table_witnesses( $table, $rdg->rank, $rdg, $lacunose, $aclabel );
Branch: 1 branch
BranchTrueFalseTotal
if ($collapse and $collapse->size) 198 452 650
Condition66%2/3
$collapse and $collapse->size
!ll&&!rl&&r
4520198
MC/DC50%1/2
$collapse and $collapse->size
$collapse $collapse->size
Truth table66%2/3
A and B
A= $collapseB= $collapse->size
ABresult
0X0
100
111
391 198 ▶ if( $collapse && $collapse->size ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
392 198 my $filter = sub { $collapse->has( $_[0]->type ) };
393 28 foreach my $other ( $rdg->related_readings( $filter ) ) {
394 28 my @otherwits = _table_witnesses( $table, $other->rank, $other, $lacunose, $aclabel );
395 28 push( @wits, @otherwits );
396 $grouped_readings->{$other->id} = 'COLLAPSE';
397 }
398 650 }
399 $grouped_readings->{$rdg->id} = Set::Scalar->new( @wits );
400 441 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
if ($gap_wits->members) 234 207 441
401 234 if( $gap_wits->members ) {
402 $grouped_readings->{'(omitted)'} = $gap_wits;
403 }
404
405 441 # Get rid of our collapsed readings
406 map { delete $grouped_readings->{$_} if(
407 912 ▶ $grouped_readings->{$_} eq 'COLLAPSE'
Branch: 1 branch
BranchTrueFalseTotal
if $grouped_readings->{$_} eq "COLLAPSE" or $grouped_readings->{$_}->is_empty 28 884 912
Condition66%2/3
$grouped_readings->{$_} eq "COLLAPSE" or $grouped_readings->{$_}->is_empty
l!l&&r!l&&!r
280884
MC/DC50%1/2
$grouped_readings->{$_} eq "COLLAPSE" or $grouped_readings->{$_}->is_empty
$grouped_readings->{$_} eq "COLLAPSE" $grouped_readings->{$_}->is_empty
Truth table66%2/3
A or B
A= $grouped_readings->{$_} eq "COLLAPSE"B= $grouped_readings->{$_}->is_empty
ABresult
1X1
011
000
408 || $grouped_readings->{$_}->is_empty ) }
409 keys %$grouped_readings;
410
411 441 ▶ # If something was transposed, check the groups for doubled-up readings
Branch: 1 branch
BranchTrueFalseTotal
if ($has_transposition) 3 438 441
412 if( $has_transposition ) {
413 # print STDERR "Group for rank $rank:\n";
414 # map { print STDERR "\t$_: " . join( ' ' , @{$grouped_readings->{$_}} ) . "\n" }
415 3 # keys %$grouped_readings;
416 _check_transposed_consistency( $c, $rank, $transposed, $grouped_readings );
417 }
418
419 441 # Return the result
420 return $grouped_readings;
421 }
422 4977 ▶ 
Subroutine _all_related: called CC=3 SCAR=11.0
423 sub _all_related {
424 4977 # Except by repetition
425 4977 my $rdg = shift;
426 4977 my $c = $rdg->collation;
427 4977 my @check = ( $rdg );
428 4977 ▶ my %seen;
Branch: 1 branch
BranchTrueFalseTotal
if (@check) 7116 4977 12093
429 7116 while( @check ) {
430 7116 my @next;
431 7266 foreach my $ck ( @check ) {
432 4878 $seen{"$ck"} = 1;
433 7266 ▶ push( @next, grep { !$seen{"$_"} }
Subroutine __ANON__: called CC=1 SCAR=0.0
434 $ck->related_readings( sub { $_[0]->type ne 'repetition' } ) );
435 7116 }
436 @check = @next;
437 }
438
439 4977
440 4977 my @all = map { $c->reading( $_ ) } keys %seen;
441 return @all;
442 }
443
444  
445 # Helper function to query the alignment table for all witnesses (a.c. included)
446 696 ▶# that have a given reading at its rank.
Subroutine _table_witnesses: called CC=11 SCAR=25.6
447 696 sub _table_witnesses {
448 696 my( $table, $rank, $trdg, $lacunose, $aclabel ) = @_;
449 696 my $tableidx = $rank - 1;
450 696 my $has_reading = Set::Scalar->new();
451 9048 foreach my $row ( @{$table->{'alignment'}} ) {
452 9048 ▶ my $wit = $row->{'witness'};
Branch: 1 branch
BranchTrueFalseTotal
if _is_lacunose($wit, $lacunose, $aclabel) 348 8700 9048
453 8700 next if _is_lacunose( $wit, $lacunose, $aclabel );
454 8700 ▶ my $rdg = $row->{'tokens'}->[$tableidx];
Branch: 1 branch
BranchTrueFalseTotal
if ($trdg) { } 8622 78 8700
455 if( $trdg ) {
456 8622 ▶ # We have some positive reading we want.
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rdg->{"t"} and defined $rdg->{"t"} 450 8172 8622
Condition66%2/3
exists $rdg->{"t"} and defined $rdg->{"t"}
!ll&&!rl&&r
45008172
MC/DC50%1/2
exists $rdg->{"t"} and defined $rdg->{"t"}
exists $rdg->{"t"} defined $rdg->{"t"}
Truth table66%2/3
A and B
A= exists $rdg->{"t"}B= defined $rdg->{"t"}
ABresult
0X0
100
111
457 8172 ▶ next unless exists $rdg->{'t'} && defined $rdg->{'t'};
Branch: 1 branch
BranchTrueFalseTotal
if ($trdg->is_lacuna) { } 0 8172 8172
458 if( $trdg->is_lacuna ) {
459 0 ▶ _add_to_witlist( $wit, $has_reading, $aclabel )
Branch: 1 branch
BranchTrueFalseTotal
if $rdg->{"t"}->is_lacuna 0 0 0
460 if $rdg->{'t'}->is_lacuna;
461 } else {
462 8172 ▶ _add_to_witlist( $wit, $has_reading, $aclabel )
Branch: 1 branch
BranchTrueFalseTotal
if $rdg->{"t"}->id eq $trdg->id 5085 3087 8172
463 if $rdg->{'t'}->id eq $trdg->id;
464 }
465 } else {
466 78 ▶ # We want the omissions.
Branch: 1 branch
BranchTrueFalseTotal
if exists $rdg->{"t"} and defined $rdg->{"t"} 78 0 78
Condition33%1/3
exists $rdg->{"t"} and defined $rdg->{"t"}
!ll&&!rl&&r
0078
MC/DC0%0/2
exists $rdg->{"t"} and defined $rdg->{"t"}
exists $rdg->{"t"} defined $rdg->{"t"}
Truth table33%1/3
A and B
A= exists $rdg->{"t"}B= defined $rdg->{"t"}
ABresult
0X0
100
111
467 0 next if exists $rdg->{'t'} && defined $rdg->{'t'};
468 _add_to_witlist( $wit, $has_reading, $aclabel )
469 }
470 696 }
471 return $has_reading->members;
472 }
473  
474 # Helper function to see if a witness is lacunose even if we are asking about
475 14781 ▶# the a.c. version
Subroutine _is_lacunose: called CC=2 SCAR=7.6
476 14781 sub _is_lacunose {
477 14781 ▶ my ( $wit, $lac, $acstr ) = @_;
Branch: 1 branch
BranchTrueFalseTotal
if ($wit =~ /^(.*)\Q$acstr\E$/) 0 14781 14781
478 0 if( $wit =~ /^(.*)\Q$acstr\E$/ ) {
479 $wit = $1;
480 14781 }
481 return $lac->has( $wit );
482 }
483  
484 6255 ▶# Helper function to ensure that X and X a.c. never appear in the same list.
Subroutine _add_to_witlist: called CC=3 SCAR=13.3
485 6255 sub _add_to_witlist {
486 6255 ▶ my( $wit, $list, $acstr ) = @_;
Branch: 1 branch
BranchTrueFalseTotal
if ($wit =~ /^(.*)\Q$acstr\E$/) { } 0 6255 6255
487 if( $wit =~ /^(.*)\Q$acstr\E$/ ) {
488 0 ▶ # Don't add X a.c. if we already have X
Branch: 1 branch
BranchTrueFalseTotal
if $list->has($1) 0 0 0
489 return if $list->has( $1 );
490 } else {
491 6255 # Delete X a.c. if we are about to add X
492 $list->delete( $wit.$acstr );
493 6255 }
494 $list->insert( $wit );
495 }
496 3 ▶ 
Subroutine _check_transposed_consistency: called CC=16 SCAR=38.0
497 3 sub _check_transposed_consistency {
498 3 my( $c, $rank, $transposed, $groupings ) = @_;
499 my %seen_wits;
500 my %thisrank;
501 # Note which readings are actually at this rank, and which witnesses
502 3 # belong to which reading.
503 12 foreach my $rdg ( keys %$groupings ) {
504 my $rdgobj = $c->reading( $rdg );
505 12 ▶ # Count '(omitted)' as a reading at this rank
Branch: 1 branch
BranchTrueFalseTotal
if not $rdgobj or $rdgobj->rank == $rank 6 6 12
Condition66%2/3
not $rdgobj or $rdgobj->rank == $rank
l!l&&r!l&&!r
066
MC/DC50%1/2
not $rdgobj or $rdgobj->rank == $rank
not $rdgobj $rdgobj->rank == $rank
Truth table66%2/3
not(A) or B
A= not $rdgobjB= $rdgobj->rank == $rank
ABresult
1X1
011
000
506 12 $thisrank{$rdg} = 1 if !$rdgobj || $rdgobj->rank == $rank;
507 map { push( @{$seen_wits{$_}}, $rdg ) } @{$groupings->{$rdg}};
508 }
509 # Our work is done if we have no witness belonging to more than one
510 3 # reading.
511 3 ▶ my @doubled = grep { scalar @{$seen_wits{$_}} > 1 } keys %seen_wits;
Branch: 1 branch
BranchTrueFalseTotal
unless @doubled 0 3 3
512 return unless @doubled;
513 3 ▶ # If we have a symmetric related transposition, drop the non-rank readings.
Branch: 1 branch
BranchTrueFalseTotal
if (@doubled == scalar keys %seen_wits) { } 3 0 3
514 3 if( @doubled == scalar keys %seen_wits ) {
515 12 ▶ foreach my $rdg ( keys %$groupings ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($thisrank{$rdg}) 6 6 12
516 if( !$thisrank{$rdg} ) {
517 6 # Groupings are Set::Scalar objects so we can compare them outright.
518 my ( $matched ) = grep { $groupings->{$rdg} == $groupings->{$_} }
519 6 keys %thisrank;
520 delete $groupings->{$rdg};
521 # If we found a group match, assume there is a symmetry happening.
522 # TODO think more about this
523 6 ▶ # print STDERR "*** Deleting symmetric reading $rdg\n";
Branch: 1 branch
BranchTrueFalseTotal
unless ($matched) 0 6 6
524 0 unless( $matched ) {
525 0 delete $transposed->{$rdg};
526 warn "Found problem in evident symmetry with reading $rdg";
527 }
528 }
529 }
530 # Otherwise 'unhook' the transposed reading(s) that have duplicates.
531 0 } else {
532 0 foreach my $dup ( @doubled ) {
533 0 ▶ foreach my $rdg ( @{$seen_wits{$dup}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if $thisrank{$rdg} 0 0 0
534 0 ▶ next if $thisrank{$rdg};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $groupings->{$rdg} 0 0 0
535 next unless exists $groupings->{$rdg};
536 0 # print STDERR "*** Deleting asymmetric doubled-up reading $rdg\n";
537 0 delete $groupings->{$rdg};
538 delete $transposed->{$rdg};
539 }
540 }
541 0 # and put any now-orphaned readings into an 'omitted' reading.
542 0 ▶ foreach my $wit ( keys %seen_wits ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {exists $groupings->{$_};} @{$seen_wits{$wit};}) 0 0 0
543 unless( grep { exists $groupings->{$_} } @{$seen_wits{$wit}} ) {
544 0 ▶ $groupings->{'(omitted)'} = Set::Scalar->new()
Branch: 1 branch
BranchTrueFalseTotal
unless exists $groupings->{"(omitted)"} 0 0 0
545 0 unless exists $groupings->{'(omitted)'};
546 _add_to_witlist( $wit, $groupings->{'(omitted)'}, $c->ac_label );
547 }
548 }
549 }
550 }
551  
552 438 ▶# For the given grouping, return its situation graph based on the stemma.
Subroutine _graph_for_grouping: called CC=7 SCAR=21.9
553 438 sub _graph_for_grouping {
554 438 my( $stemma, $grouping, $lacunose, $aclabel ) = @_;
555 438 my $acwits = [];
556 438 my $extant = {};
557 foreach my $gs ( values %$grouping ) {
558 878 ▶ map {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^(.*)\Q$aclabel\E$/) { } 0 5427 5427
559 0 ▶ if( $_ =~ /^(.*)\Q$aclabel\E$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless $lacunose->has($1) 0 0 0
560 push( @$acwits, $1 ) unless $lacunose->has( $1 );
561 5427 ▶ } else {
Branch: 1 branch
BranchTrueFalseTotal
unless $lacunose->has($_) 5427 0 5427
562 $extant->{$_} = 1 unless $lacunose->has( $_ );
563 }
564 } $gs->members;
565 438 }
566 1 ▶ my $graph;
Subroutine BEGIN: called CC=1 SCAR=0.0
567 try {
568 # contig contains all extant wits and all hypothetical wits
569 438 # needed to make up the groups.
570 0 ▶ $graph = $stemma->situation_graph( $extant, $acwits, $aclabel );
Subroutine BEGIN: called CC=1 SCAR=1.6
Branch: 2 branches
BranchTrueFalseTotal
if ($TryCatch::Error) 0 438 438
if ('TryCatch'->check_tc('Text::Tradition::Error')) { } 0 0 0
571 0 } catch ( Text::Tradition::Error $e ) {
572 throw( "Could not extend graph with given extant and a.c. witnesses: "
573 1 ▶ . $e->message );
Subroutine BEGIN: called CC=1 SCAR=0.0
574 0 } catch {
575 throw( "Could not extend graph with a.c. witnesses @$acwits" );
576 438 }
577 return $graph;
578 }
579  
580 =head2 solve_variants( $calcdir, @groups )
581  
582 Looks up the set of groups in the answers provided by the external graph solver
583 service and returns a cleaned-up answer, adding the rank IDs back where they belong.
584  
585 The answer has the form
586 { "variants" => [ array of variant location structures ],
587 "variant_count" => total,
588 "conflict_count" => number of conflicts detected,
589 "genealogical_count" => number of solutions found }
590
591 =cut
592 3 ▶ 
Subroutine solve_variants: called CC=14 SCAR=42.4
Pod documented
593 3 sub solve_variants {
594 my( $opts, @groups ) = @_;
595
596 3 # Are we using a local result directory?
597 my $dir = $opts->{dir};
598  
599 ## For each graph/group combo, make a Text::Tradition::Analysis::Result
600 3 ## object so that we can send it off for IDP lookup.
601 3 my $variants = [];
602 my $genealogical = 0; # counter
603 3 # TODO Optimize for unique graph problems
604 3 my %problems;
605 foreach my $graphproblem ( @groups ) {
606 # Construct the calc result key and look up its answer
607 my $problem = Text::Tradition::Analysis::Result->new(
608 248 graph => $graphproblem->{'graph'},
609 248 ▶ setlist => [ values %{$graphproblem->{'grouping'}} ] );
Branch: 1 branch
BranchTrueFalseTotal
if (exists $problems{$problem->object_key}) { } 152 96 248
610 152 if( exists $problems{$problem->object_key} ) {
611 $problem = $problems{$problem->object_key};
612 96 } else {
613 $problems{$problem->object_key} = $problem;
614 248 }
615 $graphproblem->{'object'} = $problem;
616 }
617 3
618 3 ▶ my %results;
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) { } 0 3 3
619 0 if( $dir ) {
620 0 ▶ my $scope = $dir->new_scope;
Condition0%0/3
$dir->lookup($_) || $problems{$_}
l!l&&r!l&&!r
000
MC/DC0%0/2
$dir->lookup($_) || $problems{$_}
$dir->lookup($_) $problems{$_}
Truth table0%0/3
A || B
A= $dir->lookup($_)B= $problems{$_}
ABresult
1X1
011
000
621 map { $results{$_} = $dir->lookup( $_ ) || $problems{$_} } keys %problems;
622 } else {
623 3 # print STDERR "Using solver at " . $opts->{solver_url} . "\n";
624 my $json = JSON->new->allow_blessed->convert_blessed->utf8->encode(
625 [ values %problems ] );
626 # Send it off and get the result
627 3 # print STDERR "Sending request: " . decode_utf8( $json ) . "\n";
628 3 my $ua = LWP::UserAgent->new();
629 my $resp = $ua->post( $opts->{solver_url}, 'Content-Type' => 'application/json',
630 3 'Content' => $json );
631 3 ▶ my $answer;
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->is_success) { } 0 3 3
632 0 if( $resp->is_success ) {
633 0 ▶ $answer = decode_json( $resp->content );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $answer eq "ARRAY" 0 0 0
634 throw( "Unexpected answer from IDP: $answer" ) unless ref( $answer ) eq 'ARRAY';
635 3 } else {
636 throw( "IDP solver returned " . $resp->status_line . " / " . $resp->content
637 . "; cannot run graph analysis" );
638 }
639 0 ▶ # One more sanity check
Branch: 1 branch
BranchTrueFalseTotal
unless keys %problems == @$answer 0 0 0
640 throw( "Something went wrong with answer symmetricity" )
641 unless keys( %problems ) == @$answer;
642 0 # Convert the results
643 0 foreach my $a ( @$answer ) {
644 0 my $r = Text::Tradition::Analysis::Result->new( $a );
645 $results{$r->object_key} = $r;
646 }
647 }
648
649 # We now have a single JSON-encoded Result object per problem sent. Fold its
650 0 # answers into our variant info structure.
651 foreach my $graphproblem ( @groups ) {
652 0 ▶ my $result = $results{$graphproblem->{'object'}->object_key}
Condition0%0/3
$results{$graphproblem->{'object'}->object_key} || $graphproblem->{'object'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$results{$graphproblem->{'object'}->object_key} || $graphproblem->{'object'}
$results{$graphproblem->{'object'}->object_key} $graphproblem->{'object'}
Truth table0%0/3
A || B
A= $results{$graphproblem->{'object'}->object_key}B= $graphproblem->{'object'}
ABresult
1X1
011
000
653 || $graphproblem->{'object'};
654
655 0 # Initialize the result structure for this graph problem
656 0 ▶ my $vstruct;
Branch: 1 branch
BranchTrueFalseTotal
if ($result->status eq 'OK') { } 0 0 0
657 0 if( $result->status eq 'OK' ) {
658 0 $vstruct = { readings => [] };
659 push( @$variants, $vstruct );
660 0 } else {
661 0 push( @$variants, _init_unsolved( $graphproblem, $result->status ) );
662 next;
663 }
664
665 0 # 1. Did the group evaluate as genealogical?
666 0 ▶ $vstruct->{genealogical} = $result->is_genealogical;
Branch: 1 branch
BranchTrueFalseTotal
if $result->is_genealogical 0 0 0
667 $genealogical++ if $result->is_genealogical;
668
669 0 # 2. What are the calculated minimum groupings for each variant loc?
670 0 foreach my $rid ( keys %{$graphproblem->{grouping}} ) {
671 0 my $inputset = $graphproblem->{grouping}->{$rid};
672 0 my $minset = $result->minimum_grouping_for( $inputset );
673 push( @{$vstruct->{readings}}, { readingid => $rid, group => $minset } );
674 }
675
676 0 # 3. What are the sources and classes calculated for each witness?
677 0 $vstruct->{witcopy_types} = { $result->classes };
678 0 $vstruct->{reading_roots} = {};
679 map { $vstruct->{reading_roots}->{$_} = 1 } $result->sources;
680
681 }
682 0
683 return { 'variants' => $variants,
684 'variant_count' => scalar @$variants,
685 'genealogical_count' => $genealogical };
686 }
687 248 ▶ 
Subroutine _init_unsolved: called CC=2 SCAR=6.9
688 248 sub _init_unsolved {
689 248 my( $graphproblem, $status ) = @_;
690 248 my $vstruct = { 'readings' => [] };
691 248 $vstruct->{'unsolved'} = $status;
692 523 foreach my $rid ( keys %{$graphproblem->{grouping}} ) {
693 523 push( @{$vstruct->{readings}}, { readingid => $rid,
694 group => [ $graphproblem->{grouping}->{$rid}->members ] } );
695 248 }
696 return $vstruct;
697 }
698  
699 =head2 analyze_location ( $tradition, $graph, $location_hash )
700  
701 Given the tradition, its stemma graph, and the solution from the graph solver,
702 work out the rest of the information we want. For each reading we need missing,
703 conflict, reading_parents, independent_occurrence, followed, not_followed,
704 and follow_unknown. Alters the location_hash in place.
705  
706 =cut
707 248 ▶ 
Subroutine analyze_location: called CC=17 SCAR=55.4
Pod documented
708 248 sub analyze_location {
709 248 my ( $tradition, $graph, $variant_row, $lacunose ) = @_;
710 my $c = $tradition->collation;
711 248 ▶
Branch: 1 branch
BranchTrueFalseTotal
if (exists $variant_row->{"unsolved"}) 248 0 248
712 248 if( exists $variant_row->{'unsolved'} ) {
713 return;
714 0 }
715 0 my $reading_roots = delete $variant_row->{'reading_roots'};
716 my $classinfo = delete $variant_row->{'witcopy_types'};
717
718 0 # Make a hash of all known node memberships, and make the subgraphs.
719 0 my $contig = {};
720 0 my $subgraph = {};
721 0 my $acstr = $c->ac_label;
722 my @acwits;
723
724 # Note which witnesses positively belong to which group. This information
725 # comes ultimately from the IDP solver.
726 0 # Also make a note of the reading's roots.
727 0 foreach my $rdghash ( @{$variant_row->{'readings'}} ) {
728 0 my $rid = $rdghash->{'readingid'};
729 0 my @roots;
730 0 foreach my $wit ( @{$rdghash->{'group'}} ) {
731 0 ▶ $contig->{$wit} = $rid;
Branch: 1 branch
BranchTrueFalseTotal
if ($wit =~ /^(.*)\Q$acstr\E$/) 0 0 0
732 0 if( $wit =~ /^(.*)\Q$acstr\E$/ ) {
733 push( @acwits, $1 );
734 0 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $reading_roots->{$wit} and $reading_roots->{$wit}) 0 0 0
Condition0%0/3
exists $reading_roots->{$wit} and $reading_roots->{$wit}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $reading_roots->{$wit} and $reading_roots->{$wit}
exists $reading_roots->{$wit} $reading_roots->{$wit}
Truth table0%0/3
A and B
A= exists $reading_roots->{$wit}B= $reading_roots->{$wit}
ABresult
0X0
100
111
735 0 if( exists $reading_roots->{$wit} && $reading_roots->{$wit} ) {
736 push( @roots, $wit );
737 }
738 0 }
739 $rdghash->{'independent_occurrence'} = \@roots;
740 }
741
742 # Now that we have all the node group memberships, calculate followed/
743 # non-followed/unknown values for each reading. Also figure out the
744 0 # reading's evident parent(s).
745 0 foreach my $rdghash ( @{$variant_row->{'readings'}} ) {
746 0 my $rid = $rdghash->{'readingid'};
747 0 my $rdg = $c->reading( $rid );
748 0 my @roots = @{$rdghash->{'independent_occurrence'}};
749 0 ▶ my @reversions;
Branch: 1 branch
BranchTrueFalseTotal
if ($classinfo) 0 0 0
750 0 if( $classinfo ) {
751 0 @reversions = grep { $classinfo->{$_} eq 'revert' }
752 0 $rdghash->{'group'}->members;
753 $rdghash->{'reversions'} = \@reversions;
754 0 }
755 my @group = @{$rdghash->{'group'}};
756
757 0 # Start figuring things out.
758 $rdghash->{'followed'} = scalar( @group )
759 - ( scalar( @roots ) + scalar( @reversions ) );
760 0 # Find the parent readings, if any, of this reading.
761 my $sourceparents = _find_reading_parents( $rid, $graph, $contig, @roots );
762 0 # Work out relationships between readings and their non-followed parent.
763 0 _resolve_parent_relationships( $c, $rid, $rdg, $sourceparents );
764 $rdghash->{'source_parents'} = $sourceparents;
765 0 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
if (@reversions) 0 0 0
766 0 if( @reversions ) {
767 0 my $revparents = _find_reading_parents( $rid, $graph, $contig, @reversions );
768 0 _resolve_parent_relationships( $c, $rid, $rdg, $revparents );
769 $rdghash->{'reversion_parents'} = $revparents;
770 }
771
772 # Find the number of times this reading was altered, and the number of
773 0 # times we're not sure.
774 0 my( %nofollow, %unknownfollow );
775 0 foreach my $wit ( @{$rdghash->{'group'}} ) {
776 0 ▶ foreach my $wchild ( $graph->successors( $wit ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($reading_roots->{$wchild} and $contig->{$wchild} and $contig->{$wchild} ne $rid) { } 0 0 0
elsif (not $contig->{$wchild}) { } 0 0 0
Condition0%0/6
$reading_roots->{$wchild} and $contig->{$wchild} and $contig->{$wchild} ne $rid
!ll&&!rl&&r
000
$reading_roots->{$wchild} and $contig->{$wchild}
!ll&&!rl&&r
000
MC/DC0%0/3
$reading_roots->{$wchild} and $contig->{$wchild} and $contig->{$wchild} ne $rid
$reading_roots->{$wchild} $contig->{$wchild} $contig->{$wchild} ne $rid
Truth table0%0/4
A and B and C
A= $reading_roots->{$wchild}B= $contig->{$wchild}C= $contig->{$wchild} ne $rid
ABCresult
0XX0
10X0
1100
1111
777 if( $reading_roots->{$wchild} && $contig->{$wchild}
778 && $contig->{$wchild} ne $rid ) {
779 0 # It definitely changed here.
780 $nofollow{$wchild} = 1;
781 } elsif( !($contig->{$wchild}) ) {
782 # The child is a hypothetical node not definitely in
783 0 # any group. Answer is unknown.
784 $unknownfollow{$wchild} = 1;
785 } # else it is either in our group, or it is a non-root node in a
786 # known group and therefore is presumed to have its reading from
787 # its group, not this link.
788 }
789 0 }
790 0 $rdghash->{'not_followed'} = keys %nofollow;
791 $rdghash->{'follow_unknown'} = keys %unknownfollow;
792
793 0 ▶ # Now say whether this reading represents a conflict.
Branch: 1 branch
BranchTrueFalseTotal
unless ($variant_row->{"genealogical"}) 0 0 0
794 0 unless( $variant_row->{'genealogical'} ) {
795 0 $rdghash->{'is_conflict'} = @roots != 1;
796 $rdghash->{'is_reverted'} = scalar @reversions;
797 }
798 }
799 }
800 0 ▶ 
Subroutine _find_reading_parents: not called CC=6 SCAR=37.4
801 0 sub _find_reading_parents {
802 0 my( $rid, $graph, $contig, @list ) = @_;
803 0 my $parenthash = {};
804 foreach my $wit ( @list ) {
805 # Look in the stemma graph to find this witness's extant or known-reading
806 0 # immediate ancestor(s), and look up the reading that each ancestor holds.
807 0 ▶ my @check = $graph->predecessors( $wit );
Branch: 1 branch
BranchTrueFalseTotal
if (@check) 0 0 0
808 0 while( @check ) {
809 0 my @next;
810 0 foreach my $wparent( @check ) {
811 0 ▶ my $preading = $contig->{$wparent};
Branch: 1 branch
BranchTrueFalseTotal
if ($preading and $preading ne $rid) { } 0 0 0
Condition0%0/3
$preading and $preading ne $rid
!ll&&!rl&&r
000
MC/DC0%0/2
$preading and $preading ne $rid
$preading $preading ne $rid
Truth table0%0/3
A and B
A= $preadingB= $preading ne $rid
ABresult
0X0
100
111
812 0 if( $preading && $preading ne $rid ) {
813 $parenthash->{$preading} = 1;
814 0 } else {
815 push( @next, $graph->predecessors( $wparent ) );
816 }
817 0 }
818 @check = @next;
819 }
820 0 }
821 return $parenthash;
822 }
823 0 ▶ 
Subroutine _resolve_parent_relationships: not called CC=23 SCAR=63.1
824 0 sub _resolve_parent_relationships {
825 0 my( $c, $rid, $rdg, $rdgparents ) = @_;
826 foreach my $p ( keys %$rdgparents ) {
827 # Resolve the relationship of the parent to the reading, and
828 0 # save it in our hash.
829 0 ▶ my $pobj = $c->reading( $p );
Branch: 1 branch
BranchTrueFalseTotal
$pobj ? : 0 0 0
830 0 my $prep = $pobj ? $pobj->id . ' (' . $pobj->text . ')' : $p;
831 0 ▶ my $phash = { 'label' => $prep };
Branch: 2 branches
BranchTrueFalseTotal
if ($pobj) { } 0 0 0
elsif ($p eq '(omitted)') { } 0 0 0
832 if( $pobj ) {
833 0 ▶ # Get the attributes of the parent object while we are here
Branch: 1 branch
BranchTrueFalseTotal
if $pobj 0 0 0
834 0 ▶ $phash->{'text'} = $pobj->text if $pobj;
Branch: 1 branch
BranchTrueFalseTotal
if ($pobj and $pobj->does("Text::Tradition::Morphology")) 0 0 0
Condition0%0/3
$pobj and $pobj->does("Text::Tradition::Morphology")
!ll&&!rl&&r
000
MC/DC0%0/2
$pobj and $pobj->does("Text::Tradition::Morphology")
$pobj $pobj->does("Text::Tradition::Morphology")
Truth table0%0/3
A and B
A= $pobjB= $pobj->does("Text::Tradition::Morphology")
ABresult
0X0
100
111
835 0 if( $pobj && $pobj->does('Text::Tradition::Morphology') ) {
836 0 $phash->{'is_nonsense'} = $pobj->is_nonsense;
837 $phash->{'is_ungrammatical'} = $pobj->grammar_invalid;
838 }
839 0 # Now look at the relationship
840 0 ▶ my $rel = $c->get_relationship( $p, $rid );
Branch: 1 branch
BranchTrueFalseTotal
if ($rel and $rel->type eq "collated") 0 0 0
Condition0%0/3
$rel and $rel->type eq "collated"
!ll&&!rl&&r
000
MC/DC0%0/2
$rel and $rel->type eq "collated"
$rel $rel->type eq "collated"
Truth table0%0/3
A and B
A= $relB= $rel->type eq "collated"
ABresult
0X0
100
111
841 0 if( $rel && $rel->type eq 'collated' ) {
842 $rel = undef;
843 0 ▶ }
Branch: 2 branches
BranchTrueFalseTotal
if ($rel) { } 0 0 0
elsif ($rdg) { } 0 0 0
844 0 if( $rel ) {
845 _add_to_hash( $rel, $phash );
846 } elsif( $rdg ) {
847 0 ▶ # First check for a transposed relationship
Branch: 1 branch
BranchTrueFalseTotal
if ($rdg->rank != $pobj->rank) 0 0 0
848 0 if( $rdg->rank != $pobj->rank ) {
849 0 ▶ foreach my $ti ( $rdg->related_readings( 'transposition' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless $ti->text eq $rdg->text 0 0 0
850 0 next unless $ti->text eq $rdg->text;
851 0 ▶ $rel = $c->get_relationship( $ti, $pobj );
Branch: 1 branch
BranchTrueFalseTotal
if ($rel) 0 0 0
852 0 if( $rel ) {
853 0 _add_to_hash( $rel, $phash, 1 );
854 last;
855 }
856 0 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
unless ($rel) 0 0 0
857 0 unless( $rel ) {
858 0 ▶ foreach my $ti ( $pobj->related_readings( 'transposition' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless $ti->text eq $pobj->text 0 0 0
859 0 next unless $ti->text eq $pobj->text;
860 0 ▶ $rel = $c->get_relationship( $ti, $rdg );
Branch: 1 branch
BranchTrueFalseTotal
if ($rel) 0 0 0
861 0 if( $rel ) {
862 0 _add_to_hash( $rel, $phash, 1 );
863 last;
864 }
865 }
866 }
867 0 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
unless ($rel) 0 0 0
868 unless( $rel ) {
869 0 # and then check for sheer word similarity.
870 0 my $rtext = $rdg->text;
871 0 ▶ my $ptext = $pobj->text;
Branch: 1 branch
BranchTrueFalseTotal
if (similar($rtext, $ptext)) 0 0 0
872 if( similar( $rtext, $ptext ) ) {
873 0 # say STDERR "Words $rtext and $ptext judged similar";
874 $phash->{relation} = { type => 'wordsimilar' };
875 }
876 }
877 0 } else {
878 $phash->{relation} = { type => 'deletion' };
879 }
880 } elsif( $p eq '(omitted)' ) {
881 0 # Check to see if the reading in question is a repetition.
882 0 ▶ my @reps = $rdg->related_readings( 'repetition' );
Branch: 1 branch
BranchTrueFalseTotal
if (@reps) { } 0 0 0
883 0 if( @reps ) {
884 $phash->{relation} = { type => 'repetition',
885 annotation => "of reading @reps" };
886 0 } else {
887 $phash->{relation} = { type => 'addition' };
888 }
889 }
890 0 # Save it
891 $rdgparents->{$p} = $phash;
892 }
893 }
894 0 ▶ 
Subroutine _add_to_hash: not called CC=5 SCAR=34.0
895 0 sub _add_to_hash {
896 0 my( $rel, $phash, $is_transposed ) = @_;
897 0 ▶ $phash->{relation} = { type => $rel->type };
Branch: 1 branch
BranchTrueFalseTotal
if $is_transposed 0 0 0
898 0 ▶ $phash->{relation}->{transposed} = 1 if $is_transposed;
Branch: 1 branch
BranchTrueFalseTotal
if $rel->has_annotation 0 0 0
899 $phash->{relation}->{annotation} = $rel->annotation
900 if $rel->has_annotation;
901 0 # Get all the relevant relationship info.
902 0 foreach my $prop ( qw/ non_independent is_significant / ) {
903 $phash->{relation}->{$prop} = $rel->$prop;
904 }
905 0 # Figure out if the variant was judged revertible.
906 0 ▶ my $is_a = $rel->reading_a eq $phash->{text};
Branch: 1 branch
BranchTrueFalseTotal
$is_a ? : 0 0 0
907 $phash->{revertible} = $is_a
908 ? $rel->a_derivable_from_b : $rel->b_derivable_from_a;
909 }
910  
911 =head2 similar( $word1, $word2 )
912  
913 Use Algorithm::Diff to get a sense of how close the words are to each other.
914 This will hopefully handle substitutions a bit more nicely than Levenshtein.
915  
916 =cut
917  
918 #!/usr/bin/env perl
919 0 ▶ 
Subroutine similar: not called CC=8 SCAR=42.8
Pod documented
920 0 sub similar {
921 0 my( $word1, $word2 ) = sort { length($a) <=> length($b) } @_;
922 0 my @let1 = split( '', lc( $word1 ) );
923 0 my @let2 = split( '', lc( $word2 ) );
924 0 my $diff = Algorithm::Diff->new( \@let1, \@let2 );
925 0 ▶ my $mag = 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($diff->Next) 0 0 0
926 0 ▶ while( $diff->Next ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($diff->Same) { } 0 0 0
elsif (not $diff->Items(1)) { } 0 0 0
elsif (not $diff->Items(2)) { } 0 0 0
927 if( $diff->Same ) {
928 0 # Take off points for longer strings
929 0 ▶ my $cs = $diff->Range(1) - 2;
Branch: 1 branch
BranchTrueFalseTotal
if $cs < 0 0 0 0
930 0 $cs = 0 if $cs < 0;
931 $mag -= $cs;
932 0 } elsif( !$diff->Items(1) ) {
933 $mag += $diff->Range(2);
934 0 } elsif( !$diff->Items(2) ) {
935 $mag += $diff->Range(1);
936 } else {
937 0 ▶ # Split the difference for substitutions
Condition0%0/2
$diff->Range(1) || 1
l!l
00
MC/DC0%0/1
$diff->Range(1) || 1
$diff->Range(1)
Truth table0%0/2
A
A= $diff->Range(1)
Aresult
11
00
938 0 ▶ my $c1 = $diff->Range(1) || 1;
Condition0%0/2
$diff->Range(2) || 1
l!l
00
MC/DC0%0/1
$diff->Range(2) || 1
$diff->Range(2)
Truth table0%0/2
A
A= $diff->Range(2)
Aresult
11
00
939 0 my $c2 = $diff->Range(2) || 1;
940 0 my $cd = ( $c1 + $c2 ) / 2;
941 $mag += $cd;
942 }
943 0 }
944 return ( $mag <= length( $word1 ) / 2 );
945 }
946 292 ▶ 
Subroutine _useful_variant: called CC=5 SCAR=16.1
947 292 sub _useful_variant {
948 my( $rankgroup, $rankgraph, $acstr ) = @_;
949  
950 292 # Sort by group size and return
951 292 my $is_useful = 0;
952 576 foreach my $rdg ( keys %$rankgroup ) {
953 576 ▶ my @wits = $rankgroup->{$rdg}->members;
Branch: 1 branch
BranchTrueFalseTotal
if (@wits > 1) { } 358 218 576
954 358 if( @wits > 1 ) {
955 $is_useful++;
956 218 ▶ } else {
Branch: 1 branch
BranchTrueFalseTotal
unless $rankgraph->is_sink_vertex($wits[0]) or $wits[0] =~ /\Q$acstr\E$/ 43 175 218
Condition66%2/3
$rankgraph->is_sink_vertex($wits[0]) or $wits[0] =~ /\Q$acstr\E$/
l!l&&r!l&&!r
175043
MC/DC50%1/2
$rankgraph->is_sink_vertex($wits[0]) or $wits[0] =~ /\Q$acstr\E$/
$rankgraph->is_sink_vertex($wits[0]) $wits[0] =~ /\Q$acstr\E$/
Truth table66%2/3
A or B
A= $rankgraph->is_sink_vertex($wits[0])B= $wits[0] =~ /\Q$acstr\E$/
ABresult
1X1
011
000
957 $is_useful++ unless( $rankgraph->is_sink_vertex( $wits[0] )
958 || $wits[0] =~ /\Q$acstr\E$/ );
959 }
960 292 }
961 return $is_useful > 1;
962 }
963  
964 =head2 wit_stringify( $groups )
965  
966 Takes an array of witness groupings and produces a string like
967 ['A','B'] / ['C','D','E'] / ['F']
968  
969 =cut
970 0 ▶ 
Subroutine wit_stringify: not called CC=3 SCAR=24.8
Pod documented
971 0 sub wit_stringify {
972 0 my $groups = shift;
973 my @gst;
974 # If we were passed an array of witnesses instead of an array of
975 0 ▶ # groupings, then "group" the witnesses first.
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $groups->[0]) 0 0 0
976 0 unless( ref( $groups->[0] ) ) {
977 0 my $mkgrp = [ $groups ];
978 $groups = $mkgrp;
979 0 }
980 0 foreach my $g ( @$groups ) {
981 push( @gst, '[' . join( ',', map { "'$_'" } @$g ) . ']' );
982 0 }
983 return join( ' / ', @gst );
984 }
985 1  
986 1;
987 3 ▶ 
Subroutine throw: called CC=1 SCAR=0.0
Pod undocumented
988 3 sub throw {
989 Text::Tradition::Error->throw(
990 'ident' => 'Analysis error',
991 'message' => $_[0],
992 );
993 }
994  
995 =head1 LICENSE
996  
997 This package is free software and is provided "as is" without express
998 or implied warranty. You can redistribute it and/or modify it under
999 the same terms as Perl itself.
1000  
1001 =head1 AUTHOR
1002  
1003 Tara L Andrews E<lt>aurum@cpan.orgE<gt>
↑ Summary StemmaUtil.pm »