1 package Text::Tradition::Analysis;
6 use Encode qw/ encode_utf8 /;
8 use JSON qw/ encode_json decode_json /;
11 use Text::Tradition::Stemma;
13 use vars qw/ @EXPORT_OK /;
14 @EXPORT_OK = qw/ run_analysis group_variants analyze_variant_location wit_stringify /;
18 Text::Tradition::Analysis - functions for stemma analysis of a tradition
23 use Text::Tradition::Analysis qw/ run_analysis analyze_variant_location /;
24 my $t = Text::Tradition->new(
25 'name' => 'this is a text',
27 'file' => '/path/to/tei_parallel_seg_file.xml' );
28 $t->add_stemma( 'dotfile' => $stemmafile );
30 my $variant_data = run_analysis( $tradition );
31 # Recalculate rank $n treating all orthographic variants as equivalent
32 my $reanalyze = analyze_variant_location( $tradition, $n, 0, 'orthographic' );
36 Text::Tradition is a library for representation and analysis of collated
37 texts, particularly medieval ones. The Collation is the central feature of
38 a Tradition, where the text, its sequence of readings, and its relationships
39 between readings are actually kept.
43 =head2 run_analysis( $tradition, %opts )
45 Runs the analysis described in analyze_variant_location on every location in the
46 collation of the given tradition, with the given options. These include:
50 =item * stemma_id - Specify which of the tradition's stemmata to use. Default
51 is 0 (i.e. the first).
53 =item * ranks - Specify a list of location ranks to analyze; exclude the rest.
55 =item * merge_types - Specify a list of relationship types, where related readings
56 should be treated as identical for the purposes of analysis.
63 use Text::Tradition::Analysis qw/ run_analysis analyze_variant_location /;
65 my $datafile = 't/data/florilegium_tei_ps.xml';
66 my $tradition = Text::Tradition->new( 'input' => 'TEI',
68 'file' => $datafile );
69 my $s = $tradition->add_stemma( 'dotfile' => 't/data/florilegium.dot' );
70 is( ref( $s ), 'Text::Tradition::Stemma', "Added stemma to tradition" );
72 my %expected_genealogical = (
103 my $data = run_analysis( $tradition );
104 foreach my $row ( @{$data->{'variants'}} ) {
105 # Account for rows that used to be "not useful"
106 unless( exists $expected_genealogical{$row->{'id'}} ) {
107 $expected_genealogical{$row->{'id'}} = 1;
109 is( $row->{'genealogical'}, $expected_genealogical{$row->{'id'}},
110 "Got correct genealogical flag for row " . $row->{'id'} );
112 is( $data->{'conflict_count'}, 34, "Got right conflict count" );
113 is( $data->{'variant_count'}, 58, "Got right total variant number" );
120 my( $tradition, %opts ) = @_;
121 my $c = $tradition->collation;
123 my $stemma_id = $opts{'stemma_id'} || 0;
124 my @ranks = ref( $opts{'ranks'} ) eq 'ARRAY' ? @{$opts{'ranks'}} : ();
125 my @collapse = ref( $opts{'merge_types'} ) eq 'ARRAY' ? @{$opts{'merge_types'}} : ();
128 my $stemma = $tradition->stemma( $stemma_id );
129 # Figure out which witnesses we are working with
130 my @lacunose = $stemma->hypotheticals;
131 my @tradition_wits = map { $_->sigil } $tradition->witnesses;
132 map { push( @tradition_wits, $_->sigil."_ac" ) if $_->is_layered }
133 $tradition->witnesses;
134 push( @lacunose, _symmdiff( [ $stemma->witnesses ], \@tradition_wits ) );
136 # Find and mark 'common' ranks for exclusion, unless they were
137 # explicitly specified.
140 foreach my $rdg ( $c->common_readings ) {
141 $common_rank{$rdg->rank} = 1;
143 @ranks = grep { !$common_rank{$_} } ( 1 .. $c->end->rank-1 );
146 # Group the variants to send to the solver
149 foreach my $rank ( @ranks ) {
150 my $missing = [ @lacunose ];
151 push( @groups, group_variants( $tradition, $rank, $missing, \@collapse ) );
152 $lacunae{$rank} = $missing;
156 my $answer = solve_variants( $stemma, @groups );
158 # Do further analysis on the answer
159 my $conflict_count = 0;
160 foreach my $idx ( 0 .. $#ranks ) {
161 my $location = $answer->{'variants'}->[$idx];
162 # Add the rank back in
163 $location->{'id'} = $ranks[$idx];
164 # Add the lacunae back in
165 $location->{'missing'} = $lacunae{$ranks[$idx]};
166 # Run the extra analysis we need.
167 analyze_location( $tradition, $stemma->graph, $location );
168 # Add the reading text back in
169 foreach my $rdghash ( @{$location->{'readings'}} ) {
171 if exists $rdghash->{'conflict'} && $rdghash->{'conflict'};
172 my $rdg = $c->reading( $rdghash->{'readingid'} );
173 $rdghash->{'text'} = $rdg ? $rdg->text : $rdghash->{'readingid'};
176 $answer->{'conflict_count'} = $conflict_count;
181 =head2 group_variants( $tradition, $rank, $lacunose, @merge_relationship_types )
183 Groups the variants at the given $rank of the collation, treating any
184 relationships in @merge_relationship_types as equivalent. $lacunose should
185 be a reference to an array, to which the sigla of lacunose witnesses at this
186 rank will be appended.
188 Returns two ordered lists $readings, $groups, where $readings->[$n] is attested
189 by the witnesses listed in $groups->[$n].
193 # Return group_readings, groups, lacunose
195 my( $tradition, $rank, $lacunose, $collapse ) = @_;
196 my $c = $tradition->collation;
197 my $aclabel = $c->ac_label;
198 # Get the alignment table readings
199 my %readings_at_rank;
201 foreach my $tablewit ( @{$c->alignment_table->{'alignment'}} ) {
202 my $rdg = $tablewit->{'tokens'}->[$rank-1];
203 my $wit = $tablewit->{'witness'};
204 $wit =~ s/^(.*)\Q$aclabel\E$/${1}_ac/;
205 if( $rdg && $rdg->{'t'}->is_lacuna ) {
206 _add_to_witlist( $wit, $lacunose, '_ac' );
208 $readings_at_rank{$rdg->{'t'}->text} = $rdg->{'t'};
210 _add_to_witlist( $wit, \@gap_wits, '_ac' );
214 # Group the readings, collapsing groups by relationship if needed
215 my %grouped_readings;
216 foreach my $rdg ( sort { $b->witnesses <=> $a->witnesses } values %readings_at_rank ) {
217 # Skip readings that have been collapsed into others.
218 next if exists $grouped_readings{$rdg->id} && !$grouped_readings{$rdg->id};
219 my @wits = $rdg->witnesses;
220 map { s/\Q$aclabel\E$/_ac/ } @wits;
222 my $filter = sub { my $r = $_[0]; grep { $_ eq $r->type } @$collapse; };
223 foreach my $other ( $rdg->related_readings( $filter ) ) {
224 my @otherwits = $other->witnesses;
225 map { s/\Q$aclabel\E$/_ac/ } @otherwits;
226 push( @wits, @otherwits );
227 $grouped_readings{$other->id} = 0;
230 $grouped_readings{$rdg->id} = \@wits;
232 $grouped_readings{'(omitted)'} = \@gap_wits if @gap_wits;
233 # Get rid of our collapsed readings
234 map { delete $grouped_readings{$_} unless $grouped_readings{$_} }
235 keys %grouped_readings
238 return \%grouped_readings;
241 =head2 solve_variants( $graph, @groups )
243 Sends the set of groups to the external graph solver service and returns
244 a cleaned-up answer, adding the rank IDs back where they belong.
246 The JSON has the form
247 { "graph": [ stemmagraph DOT string without newlines ],
248 "groupings": [ array of arrays of groups, one per rank ] }
250 The answer has the form
251 { "variants" => [ array of variant location structures ],
252 "variant_count" => total,
253 "conflict_count" => number of conflicts detected,
254 "genealogical_count" => number of solutions found }
259 my( $stemma, @groups ) = @_;
261 # Make the json with stemma + groups
262 my $jsonstruct = { 'graph' => $stemma->editable( ' ' ), 'groupings' => [] };
263 foreach my $ghash ( @groups ) {
265 foreach my $k ( sort keys %$ghash ) {
266 push( @grouping, $ghash->{$k} );
268 push( @{$jsonstruct->{'groupings'}}, \@grouping );
270 my $json = encode_json( $jsonstruct );
272 # Send it off and get the result
273 my $solver_url = 'http://byzantini.st/cgi-bin/graphcalc.cgi';
274 my $ua = LWP::UserAgent->new();
275 my $resp = $ua->post( $solver_url, 'Content-Type' => 'application/json',
276 'Content' => $json );
279 if( $resp->is_success ) {
280 $answer = decode_json( $resp->content );
282 # Fall back to the old method.
283 warn "IDP solver returned " . $resp->status_line . " / " . $resp->content
284 . "; falling back to perl method";
285 $answer = perl_solver( $stemma, @{$jsonstruct->{'groupings'}} );
288 # Fold the result back into what we know about the groups.
290 my $genealogical = 0;
291 foreach my $idx ( 0 .. $#groups ) {
292 my( $calc_groups, $result ) = @{$answer->[$idx]};
293 $genealogical++ if $result;
294 my $input_group = $groups[$idx];
295 foreach my $k ( sort keys %$input_group ) {
296 my $cg = shift @$calc_groups;
297 $input_group->{$k} = $cg;
300 'genealogical' => $result,
303 foreach my $k ( keys %$input_group ) {
304 push( @{$vstruct->{'readings'}},
305 { 'readingid' => $k, 'group' => $input_group->{$k}} );
307 push( @$variants, $vstruct );
310 return { 'variants' => $variants,
311 'variant_count' => scalar @$variants,
312 'genealogical_count' => $genealogical };
315 =head2 analyze_location ( $tradition, $graph, $location_hash )
317 Given the tradition, its stemma graph, and the solution from the graph solver,
318 work out the rest of the information we want. For each reading we need missing,
319 conflict, reading_parents, independent_occurrence, followed, not_followed, and follow_unknown. Alters the location_hash in place.
323 sub analyze_location {
324 my ( $tradition, $graph, $variant_row ) = @_;
326 # Make a hash of all known node memberships, and make the subgraphs.
328 my $reading_roots = {};
330 foreach my $rdghash ( @{$variant_row->{'readings'}} ) {
331 my $rid = $rdghash->{'readingid'};
332 map { $contig->{$_} = $rid } @{$rdghash->{'group'}};
335 my $part = $graph->copy;
337 map { $these_vertices{$_} = 1 } @{$rdghash->{'group'}};
338 $part->delete_vertices( grep { !$these_vertices{$_} } $part->vertices );
339 $subgraph->{$rid} = $part;
340 # Get the reading roots.
341 map { $reading_roots->{$_} = $rid } $part->predecessorless_vertices;
344 # Now that we have all the node group memberships, calculate followed/
345 # non-followed/unknown values for each reading. Also figure out the
346 # reading's evident parent(s).
347 foreach my $rdghash ( @{$variant_row->{'readings'}} ) {
348 # Group string key - TODO do we need this?
349 my $gst = wit_stringify( $rdghash->{'group'} );
350 my $rid = $rdghash->{'readingid'};
352 my $part = $subgraph->{$rid};
354 # Start figuring things out.
355 my @roots = $part->predecessorless_vertices;
356 $rdghash->{'independent_occurrence'} = scalar @roots;
357 $rdghash->{'followed'} = scalar( $part->vertices ) - scalar( @roots );
358 # Find the parent readings, if any, of this reading.
360 foreach my $wit ( @roots ) {
361 # Look in the main stemma to find this witness's extant or known-reading
362 # immediate ancestor(s), and look up the reading that each ancestor olds.
363 my @check = $graph->predecessors( $wit );
366 foreach my $wparent( @check ) {
367 my $preading = $contig->{$wparent};
369 $rdgparents{$preading} = 1;
371 push( @next, $graph->predecessors( $wparent ) );
377 $rdghash->{'reading_parents'} = [ keys %rdgparents ];
379 # Find the number of times this reading was altered, and the number of
380 # times we're not sure.
381 my( %nofollow, %unknownfollow );
382 foreach my $wit ( $part->vertices ) {
383 foreach my $wchild ( $graph->successors( $wit ) ) {
384 next if $part->has_vertex( $wchild );
385 if( $reading_roots->{$wchild} && $contig->{$wchild} ) {
386 # It definitely changed here.
387 $nofollow{$wchild} = 1;
388 } elsif( !($contig->{$wchild}) ) {
389 # The child is a hypothetical node not definitely in
390 # any group. Answer is unknown.
391 $unknownfollow{$wchild} = 1;
392 } # else it's a non-root node in a known group, and therefore
393 # is presumed to have its reading from its group, not this link.
396 $rdghash->{'not_followed'} = keys %nofollow;
397 $rdghash->{'follow_unknown'} = keys %unknownfollow;
399 # Now say whether this reading represents a conflict.
400 unless( $variant_row->{'genealogical'} ) {
401 $rdghash->{'conflict'} = @roots != 1;
407 =head2 perl_solver( $tradition, $rank, $stemma_id, @merge_relationship_types )
409 ** NOTE ** This method should hopefully not be called - it is not guaranteed
410 to be correct. Serves as a backup for the real solver.
412 Runs an analysis of the given tradition, at the location given in $rank,
413 against the graph of the stemma specified in $stemma_id. The argument
414 @merge_relationship_types is an optional list of relationship types for
415 which readings so related should be treated as equivalent.
417 Returns a nested array data structure as follows:
419 [ [ group_list, is_genealogical ], [ group_list, is_genealogical ] ... ]
421 where the group list is the array of arrays passed in for each element of @groups,
422 possibly with the addition of hypothetical readings.
428 my( $stemma, @groups ) = @_;
429 my $graph = $stemma->graph;
431 foreach my $g ( @groups ) {
432 push( @answer, _solve_variant_location( $graph, $g ) );
437 sub _solve_variant_location {
438 my( $graph, $groups ) = @_;
445 # Mark each ms as in its own group, first.
446 foreach my $g ( @$groups ) {
447 my $gst = wit_stringify( $g );
448 map { $contig->{$_} = $gst } @$g;
451 # Now for each unmarked node in the graph, initialize an array
452 # for possible group memberships. We will use this later to
453 # resolve potential conflicts.
454 map { $contig->{$_} = [] unless $contig->{$_} } $graph->vertices;
455 foreach my $g ( sort { scalar @$b <=> scalar @$a } @$groups ) {
456 my $gst = wit_stringify( $g ); # This is the group name
457 # Copy the graph, and delete all non-members from the new graph.
458 my $part = $graph->copy;
460 $part->delete_vertices(
461 grep { !ref( $contig->{$_} ) && $contig->{$_} ne $gst } $graph->vertices );
463 # Now look to see if our group is connected.
465 # We have to take directionality into account.
466 # How many root nodes do we have?
467 my @roots = grep { ref( $contig->{$_} ) || $contig->{$_} eq $gst }
468 $part->predecessorless_vertices;
469 # Assuming that @$g > 1, find the first root node that has at
470 # least one successor belonging to our group. If this reading
471 # is genealogical, there should be only one, but we will check
472 # that implicitly later.
473 foreach my $root ( @roots ) {
474 # Prune the tree to get rid of extraneous hypotheticals.
475 $root = _prune_subtree( $part, $root, $contig );
477 # Save this root for our group.
478 push( @group_roots, $root );
479 # Get all the successor nodes of our root.
482 # Dispense with the trivial case of one reading.
484 @group_roots = ( $wit );
485 foreach my $v ( $part->vertices ) {
486 $part->delete_vertex( $v ) unless $v eq $wit;
490 if( @group_roots > 1 ) {
491 $conflict->{$gst} = 1;
494 # Paint the 'hypotheticals' with our group.
495 foreach my $wit ( $part->vertices ) {
496 if( ref( $contig->{$wit} ) ) {
497 push( @{$contig->{$wit}}, $gst );
498 } elsif( $contig->{$wit} ne $gst ) {
499 warn "How did we get here?";
504 # Save the relevant subgraph.
505 $subgraph->{$gst} = $part;
508 # For each of our hypothetical readings, flatten its 'contig' array if
509 # the array contains zero or one group. If we have any unflattened arrays,
510 # we may need to run the resolution process. If the reading is already known
511 # to have a conflict, flatten the 'contig' array to nothing; we won't resolve
514 foreach my $wit ( keys %$contig ) {
515 next unless ref( $contig->{$wit} );
516 if( @{$contig->{$wit}} > 1 ) {
517 if( $is_conflicted ) {
518 $contig->{$wit} = ''; # We aren't going to decide.
520 push( @resolve, $wit );
523 my $gst = pop @{$contig->{$wit}};
524 $contig->{$wit} = $gst || '';
529 my $still_contig = {};
530 foreach my $h ( @resolve ) {
531 # For each of the hypothetical readings with more than one possibility,
532 # try deleting it from each of its member subgraphs in turn, and see
533 # if that breaks the contiguous grouping.
534 # TODO This can still break in a corner case where group A can use
535 # either vertex 1 or 2, and group B can use either vertex 2 or 1.
536 # Revisit this if necessary; it could get brute-force nasty.
537 foreach my $gst ( @{$contig->{$h}} ) {
538 my $gpart = $subgraph->{$gst}->copy();
539 # If we have come this far, there is only one root and everything
540 # is reachable from it.
541 my( $root ) = $gpart->predecessorless_vertices;
543 map { $reachable->{$_} = 1 } $gpart->vertices;
545 # Try deleting the hypothetical node.
546 $gpart->delete_vertex( $h );
548 # See if we still have a single root.
549 my @roots = $gpart->predecessorless_vertices;
550 warn "This shouldn't have happened" unless @roots;
552 # $h is needed by this group.
553 if( exists( $still_contig->{$h} ) ) {
555 $conflict->{$gst} = 1;
556 $still_contig->{$h} = '';
558 $still_contig->{$h} = $gst;
562 # $h is somewhere in the middle. See if everything
563 # else can still be reached from the root.
564 my %still_reachable = ( $root => 1 );
565 map { $still_reachable{$_} = 1 }
566 $gpart->all_successors( $root );
567 foreach my $v ( keys %$reachable ) {
569 if( !$still_reachable{$v}
570 && ( $contig->{$v} eq $gst
571 || ( exists $still_contig->{$v}
572 && $still_contig->{$v} eq $gst ) ) ) {
574 if( exists $still_contig->{$h} ) {
576 $conflict->{$gst} = 1;
577 $still_contig->{$h} = '';
579 $still_contig->{$h} = $gst;
582 } # else we don't need $h in this group.
584 } # endif $h eq $root
588 # Now we have some hypothetical vertices in $still_contig that are the
589 # "real" group memberships. Replace these in $contig.
590 foreach my $v ( keys %$contig ) {
591 next unless ref $contig->{$v};
592 $contig->{$v} = $still_contig->{$v};
596 my $is_genealogical = keys %$conflict ? JSON::false : JSON::true;
597 my $variant_row = [ [], $is_genealogical ];
598 # Fill in the groupings from $contig.
599 foreach my $g ( @$groups ) {
600 my $gst = wit_stringify( $g );
601 my @realgroup = grep { $contig->{$_} eq $gst } keys %$contig;
602 push( @{$variant_row->[0]}, \@realgroup );
608 my( $tree, $root, $contighash ) = @_;
609 # First, delete hypothetical leaves / orphans until there are none left.
610 my @orphan_hypotheticals = grep { ref( $contighash->{$_} ) }
611 $tree->successorless_vertices;
612 while( @orphan_hypotheticals ) {
613 $tree->delete_vertices( @orphan_hypotheticals );
614 @orphan_hypotheticals = grep { ref( $contighash->{$_} ) }
615 $tree->successorless_vertices;
617 # Then delete a hypothetical root with only one successor, moving the
618 # root to the first child that has no other predecessors.
619 while( $tree->successors( $root ) == 1 && ref $contighash->{$root} ) {
620 my @nextroot = $tree->successors( $root );
621 $tree->delete_vertex( $root );
622 ( $root ) = grep { $tree->is_predecessorless_vertex( $_ ) } @nextroot;
624 # The tree has been modified in place, but we need to know the new root.
625 $root = undef unless $root && $tree->has_vertex( $root );
628 # Add the variant, subject to a.c. representation logic.
629 # This assumes that we will see the 'main' version before the a.c. version.
630 sub add_variant_wit {
631 my( $arr, $wit, $acstr ) = @_;
633 if( $wit =~ /^(.*)\Q$acstr\E$/ ) {
635 $skip = grep { $_ =~ /^\Q$real\E$/ } @$arr;
637 push( @$arr, $wit ) unless $skip;
640 sub _useful_variant {
641 my( $group_readings, $graph, $acstr ) = @_;
643 # TODO Decide what to do with AC witnesses
645 # Sort by group size and return
647 my( @readings, @groups ); # The sorted groups for our answer.
648 foreach my $rdg ( sort { @{$group_readings->{$b}} <=> @{$group_readings->{$a}} }
649 keys %$group_readings ) {
650 push( @readings, $rdg );
651 push( @groups, $group_readings->{$rdg} );
652 if( @{$group_readings->{$rdg}} > 1 ) {
655 my( $wit ) = @{$group_readings->{$rdg}};
656 $wit =~ s/^(.*)\Q$acstr\E$/$1/;
657 $is_useful++ unless( $graph->is_sink_vertex( $wit ) );
660 if( $is_useful > 1 ) {
661 return( \@readings, \@groups );
667 =head2 wit_stringify( $groups )
669 Takes an array of witness groupings and produces a string like
670 ['A','B'] / ['C','D','E'] / ['F']
677 # If we were passed an array of witnesses instead of an array of
678 # groupings, then "group" the witnesses first.
679 unless( ref( $groups->[0] ) ) {
680 my $mkgrp = [ $groups ];
683 foreach my $g ( @$groups ) {
684 push( @gst, '[' . join( ',', map { "'$_'" } @$g ) . ']' );
686 return join( ' / ', @gst );
689 # Helper function to ensure that X and X a.c. never appear in the same list.
690 sub _add_to_witlist {
691 my( $wit, $list, $acstr ) = @_;
694 map { $inlist{$_} = $idx++ } @$list;
695 if( $wit =~ /^(.*)\Q$acstr\E$/ ) {
697 unless( exists $inlist{$acwit} ) {
698 push( @$list, $acwit.$acstr );
701 if( exists( $inlist{$wit.$acstr} ) ) {
702 # Replace the a.c. version with the main witness
703 my $i = $inlist{$wit.$acstr};
706 push( @$list, $wit );
712 my( $lista, $listb ) = @_;
715 map { $union{$_} = 1; $scalars{$_} = $_ } @$lista;
716 map { $union{$_} += 1; $scalars{$_} = $_ } @$listb;
717 my @set = grep { $union{$_} == 1 } keys %union;
718 return map { $scalars{$_} } @set;
725 This package is free software and is provided "as is" without express
726 or implied warranty. You can redistribute it and/or modify it under
727 the same terms as Perl itself.
731 Tara L Andrews E<lt>aurum@cpan.orgE<gt>