1 package Text::Tradition::Collation;
3 use Encode qw( decode_utf8 );
6 use IPC::Run qw( run binary );
8 use Text::Tradition::Collation::Reading;
9 use Text::Tradition::Collation::RelationshipStore;
16 default => sub { Graph->new() },
24 isa => 'Text::Tradition::Collation::RelationshipStore',
26 relationships => 'relationships',
27 related_readings => 'related_readings',
29 writer => '_set_relations',
34 isa => 'Text::Tradition',
39 isa => 'HashRef[Text::Tradition::Collation::Reading]',
43 _add_reading => 'set',
44 del_reading => 'delete',
45 has_reading => 'exists',
48 default => sub { {} },
51 has 'wit_list_separator' => (
60 default => 'base text',
77 isa => 'Text::Tradition::Collation::Reading',
78 writer => '_set_start',
84 isa => 'Text::Tradition::Collation::Reading',
91 Text::Tradition::Collation - a software model for a text collation
96 my $t = Text::Tradition->new(
97 'name' => 'this is a text',
99 'file' => '/path/to/tei_parallel_seg_file.xml' );
101 my $c = $t->collation;
102 my @readings = $c->readings;
103 my @paths = $c->paths;
104 my @relationships = $c->relationships;
106 my $svg_variant_graph = $t->collation->as_svg();
110 Text::Tradition is a library for representation and analysis of collated
111 texts, particularly medieval ones. The Collation is the central feature of
112 a Tradition, where the text, its sequence of readings, and its relationships
113 between readings are actually kept.
119 The constructor. Takes a hash or hashref of the following arguments:
123 =item * tradition - The Text::Tradition object to which the collation
126 =item * linear - Whether the collation should be linear; that is, whether
127 transposed readings should be treated as two linked readings rather than one,
128 and therefore whether the collation graph is acyclic. Defaults to true.
130 =item * baselabel - The default label for the path taken by a base text
131 (if any). Defaults to 'base text'.
133 =item * wit_list_separator - The string to join a list of witnesses for
134 purposes of making labels in display graphs. Defaults to ', '.
136 =item * ac_label - The extra label to tack onto a witness sigil when
137 representing another layer of path for the given witness - that is, when
138 a text has more than one possible reading due to scribal corrections or
139 the like. Defaults to ' (a.c.)'.
149 =head2 wit_list_separator
155 Simple accessors for collation attributes.
159 The meta-reading at the start of every witness path.
163 The meta-reading at the end of every witness path.
167 Returns all Reading objects in the graph.
169 =head2 reading( $id )
171 Returns the Reading object corresponding to the given ID.
173 =head2 add_reading( $reading_args )
175 Adds a new reading object to the collation.
176 See L<Text::Tradition::Collation::Reading> for the available arguments.
178 =head2 del_reading( $object_or_id )
180 Removes the given reading from the collation, implicitly removing its
181 paths and relationships.
183 =head2 merge_readings( $main, $second )
185 Merges the $second reading into the $main one.
186 The arguments may be either readings or reading IDs.
188 =head2 has_reading( $id )
190 Predicate to see whether a given reading ID is in the graph.
192 =head2 reading_witnesses( $object_or_id )
194 Returns a list of sigils whose witnesses contain the reading.
198 Returns all reading paths within the document - that is, all edges in the
199 collation graph. Each path is an arrayref of [ $source, $target ] reading IDs.
201 =head2 add_path( $source, $target, $sigil )
203 Links the given readings in the collation in sequence, under the given witness
204 sigil. The readings may be specified by object or ID.
206 =head2 del_path( $source, $target, $sigil )
208 Links the given readings in the collation in sequence, under the given witness
209 sigil. The readings may be specified by object or ID.
211 =head2 has_path( $source, $target );
213 Returns true if the two readings are linked in sequence in any witness.
214 The readings may be specified by object or ID.
218 Returns all Relationship objects in the collation.
220 =head2 add_relationship( $reading, $other_reading, $options )
222 Adds a new relationship of the type given in $options between the two readings,
223 which may be specified by object or ID. Returns a value of ( $status, @vectors)
224 where $status is true on success, and @vectors is a list of relationship edges
225 that were ultimately added.
226 See L<Text::Tradition::Collation::Relationship> for the available options.
232 $self->_set_relations( Text::Tradition::Collation::RelationshipStore->new( 'collation' => $self ) );
233 $self->_set_start( $self->add_reading( { 'collation' => $self, 'is_start' => 1 } ) );
234 $self->_set_end( $self->add_reading( { 'collation' => $self, 'is_end' => 1 } ) );
237 ### Reading construct/destruct functions
240 my( $self, $reading ) = @_;
241 unless( ref( $reading ) eq 'Text::Tradition::Collation::Reading' ) {
242 my %args = %$reading;
243 $reading = Text::Tradition::Collation::Reading->new(
244 'collation' => $self,
247 # First check to see if a reading with this ID exists.
248 if( $self->reading( $reading->id ) ) {
249 warn "Collation already has a reading with id " . $reading->id;
252 $self->_add_reading( $reading->id => $reading );
253 # Once the reading has been added, put it in both graphs.
254 $self->sequence->add_vertex( $reading->id );
255 $self->relations->add_reading( $reading->id );
259 around del_reading => sub {
264 if( ref( $arg ) eq 'Text::Tradition::Collation::Reading' ) {
267 # Remove the reading from the graphs.
268 $self->sequence->delete_vertex( $arg );
269 $self->relations->delete_reading( $arg );
272 $self->$orig( $arg );
275 # merge_readings( $main, $to_be_deleted );
280 # We only need the IDs for adding paths to the graph, not the reading
281 # objects themselves.
282 my( $kept, $deleted, $combine_char ) = $self->_stringify_args( @_ );
284 # The kept reading should inherit the paths and the relationships
285 # of the deleted reading.
286 foreach my $path ( $self->sequence->edges_at( $deleted ) ) {
287 my @vector = ( $kept );
288 push( @vector, $path->[1] ) if $path->[0] eq $deleted;
289 unshift( @vector, $path->[0] ) if $path->[1] eq $deleted;
290 next if $vector[0] eq $vector[1]; # Don't add a self loop
291 my %wits = %{$self->sequence->get_edge_attributes( @$path )};
292 $self->sequence->add_edge( @vector );
293 my $fwits = $self->sequence->get_edge_attributes( @vector );
294 @wits{keys %$fwits} = values %$fwits;
295 $self->sequence->set_edge_attributes( @vector, \%wits );
297 $self->relations->merge_readings( $kept, $deleted, $combine_char );
299 # Do the deletion deed.
300 if( $combine_char ) {
301 my $kept_obj = $self->reading( $kept );
302 my $new_text = join( $combine_char, $kept_obj->text,
303 $self->reading( $deleted )->text );
304 $kept_obj->alter_text( $new_text );
306 $self->del_reading( $deleted );
310 # Helper function for manipulating the graph.
311 sub _stringify_args {
312 my( $self, $first, $second, $arg ) = @_;
314 if ref( $first ) eq 'Text::Tradition::Collation::Reading';
315 $second = $second->id
316 if ref( $second ) eq 'Text::Tradition::Collation::Reading';
317 return( $first, $second, $arg );
320 # Helper function for manipulating the graph.
321 sub _objectify_args {
322 my( $self, $first, $second, $arg ) = @_;
323 $first = $self->reading( $first )
324 unless ref( $first ) eq 'Text::Tradition::Collation::Reading';
325 $second = $self->reading( $second )
326 unless ref( $second ) eq 'Text::Tradition::Collation::Reading';
327 return( $first, $second, $arg );
334 # We only need the IDs for adding paths to the graph, not the reading
335 # objects themselves.
336 my( $source, $target, $wit ) = $self->_stringify_args( @_ );
338 # Connect the readings
339 $self->sequence->add_edge( $source, $target );
340 # Note the witness in question
341 $self->sequence->set_edge_attribute( $source, $target, $wit, 1 );
347 if( ref( $_[0] ) eq 'ARRAY' ) {
354 # We only need the IDs for adding paths to the graph, not the reading
355 # objects themselves.
356 my( $source, $target, $wit ) = $self->_stringify_args( @args );
358 if( $self->sequence->has_edge_attribute( $source, $target, $wit ) ) {
359 $self->sequence->delete_edge_attribute( $source, $target, $wit );
361 unless( keys %{$self->sequence->get_edge_attributes( $source, $target )} ) {
362 $self->sequence->delete_edge( $source, $target );
367 # Extra graph-alike utility
370 my( $source, $target, $wit ) = $self->_stringify_args( @_ );
371 return undef unless $self->sequence->has_edge( $source, $target );
372 return $self->sequence->has_edge_attribute( $source, $target, $wit );
375 =head2 clear_witness( @sigil_list )
377 Clear the given witnesses out of the collation entirely, removing references
378 to them in paths, and removing readings that belong only to them. Should only
379 be called via $tradition->del_witness.
384 my( $self, @sigils ) = @_;
386 # Clear the witness(es) out of the paths
387 foreach my $e ( $self->paths ) {
388 foreach my $sig ( @sigils ) {
389 $self->del_path( $e, $sig );
393 # Clear out the newly unused readings
394 foreach my $r ( $self->readings ) {
395 unless( $self->reading_witnesses( $r ) ) {
396 $self->del_reading( $r );
401 sub add_relationship {
403 my( $source, $target, $opts ) = $self->_stringify_args( @_ );
404 my( $ret, @vectors ) = $self->relations->add_relationship( $source,
405 $self->reading( $source ), $target, $self->reading( $target ), $opts );
406 # Force a full rank recalculation every time. Yuck.
407 $self->calculate_ranks() if $ret && $self->end->has_rank;
408 return( $ret, @vectors );
411 =head2 reading_witnesses( $reading )
413 Return a list of sigils corresponding to the witnesses in which the reading appears.
417 sub reading_witnesses {
418 my( $self, $reading ) = @_;
419 # We need only check either the incoming or the outgoing edges; I have
420 # arbitrarily chosen "incoming". Thus, special-case the start node.
421 if( $reading eq $self->start ) {
422 return map { $_->sigil } $self->tradition->witnesses;
425 foreach my $e ( $self->sequence->edges_to( $reading ) ) {
426 my $wits = $self->sequence->get_edge_attributes( @$e );
427 @all_witnesses{ keys %$wits } = 1;
429 return keys %all_witnesses;
432 =head1 OUTPUT METHODS
436 Returns an SVG string that represents the graph, via as_dot and graphviz.
443 my @cmd = qw/dot -Tsvg/;
445 my $dotfile = File::Temp->new();
447 # $dotfile->unlink_on_destroy(0);
448 binmode $dotfile, ':utf8';
449 print $dotfile $self->as_dot();
450 push( @cmd, $dotfile->filename );
451 run( \@cmd, ">", binary(), \$svg );
452 $svg = decode_utf8( $svg );
456 =head2 svg_subgraph( $from, $to )
458 Returns an SVG string that represents the portion of the graph given by the
459 specified range. The $from and $to variables refer to ranks within the graph.
464 my( $self, $from, $to ) = @_;
466 my $dot = $self->as_dot( $from, $to );
468 warn "Could not output a graph with range $from - $to";
472 my @cmd = qw/dot -Tsvg/;
474 my $dotfile = File::Temp->new();
476 # $dotfile->unlink_on_destroy(0);
477 binmode $dotfile, ':utf8';
479 push( @cmd, $dotfile->filename );
480 run( \@cmd, ">", binary(), \$svg );
481 $svg = decode_utf8( $svg );
486 =head2 as_dot( $from, $to )
488 Returns a string that is the collation graph expressed in dot
489 (i.e. GraphViz) format. If $from or $to is passed, as_dot creates
490 a subgraph rather than the entire graph.
495 my( $self, $startrank, $endrank ) = @_;
497 # Check the arguments
499 return if $endrank && $startrank > $endrank;
500 return if $startrank > $self->end->rank;
502 if( defined $endrank ) {
503 return if $endrank < 0;
504 $endrank = undef if $endrank == $self->end->rank;
507 # TODO consider making some of these things configurable
508 my $graph_name = $self->tradition->name;
509 $graph_name =~ s/[^\w\s]//g;
510 $graph_name = join( '_', split( /\s+/, $graph_name ) );
518 'fillcolor' => 'white',
523 'arrowhead' => 'open',
524 'color' => '#000000',
525 'fontcolor' => '#000000',
528 my $dot = sprintf( "digraph %s {\n", $graph_name );
529 $dot .= "\tgraph " . _dot_attr_string( \%graph_attrs ) . ";\n";
530 $dot .= "\tnode " . _dot_attr_string( \%node_attrs ) . ";\n";
532 # Output substitute start/end readings if necessary
534 $dot .= "\t\"#SUBSTART#\" [ label=\"...\" ];\n";
537 $dot .= "\t\"#SUBEND#\" [ label=\"...\" ];\n";
539 my %used; # Keep track of the readings that actually appear in the graph
540 foreach my $reading ( $self->readings ) {
541 # Only output readings within our rank range.
542 next if $startrank && $reading->rank < $startrank;
543 next if $endrank && $reading->rank > $endrank;
544 $used{$reading->id} = 1;
545 # Need not output nodes without separate labels
546 next if $reading->id eq $reading->text;
547 my $label = $reading->text;
548 $label =~ s/\"/\\\"/g;
549 $dot .= sprintf( "\t\"%s\" [ label=\"%s\" ];\n", $reading->id, $label );
553 my @edges = $self->paths;
554 my( %substart, %subend );
555 foreach my $edge ( @edges ) {
556 # Do we need to output this edge?
557 if( $used{$edge->[0]} && $used{$edge->[1]} ) {;
558 my $label = $self->path_display_label( $self->path_witnesses( $edge ) );
559 my $variables = { %edge_attrs, 'label' => $label };
560 # Account for the rank gap if necessary
561 my $rankgap = $self->reading( $edge->[1] )->rank
562 - $self->reading( $edge->[0] )->rank;
563 $variables->{'minlen'} = $rankgap if $rankgap > 1;
564 my $varopts = _dot_attr_string( $variables );
565 $dot .= sprintf( "\t\"%s\" -> \"%s\" %s;\n",
566 $edge->[0], $edge->[1], $varopts );
567 } elsif( $used{$edge->[0]} ) {
568 $subend{$edge->[0]} = 1;
569 } elsif( $used{$edge->[1]} ) {
570 $substart{$edge->[1]} = 1;
573 # Add substitute start and end edges if necessary
574 foreach my $node ( keys %substart ) {
575 my $witstr = $self->path_display_label ( $self->reading_witnesses( $self->reading( $node ) ) );
576 my $variables = { %edge_attrs, 'label' => $witstr };
577 my $varopts = _dot_attr_string( $variables );
578 $dot .= "\t\"#SUBSTART#\" -> \"$node\" $varopts;";
580 foreach my $node ( keys %subend ) {
581 my $witstr = $self->path_display_label ( $self->reading_witnesses( $self->reading( $node ) ) );
582 my $variables = { %edge_attrs, 'label' => $witstr };
583 my $varopts = _dot_attr_string( $variables );
584 $dot .= "\t\"$node\" -> \"#SUBEND#\" $varopts;";
591 sub _dot_attr_string {
594 foreach my $k ( sort keys %$hash ) {
596 push( @attrs, $k.'="'.$v.'"' );
598 return( '[ ' . join( ', ', @attrs ) . ' ]' );
602 my( $self, @edge ) = @_;
603 # If edge is an arrayref, cope.
604 if( @edge == 1 && ref( $edge[0] ) eq 'ARRAY' ) {
608 my @wits = keys %{$self->sequence->get_edge_attributes( @edge )};
612 sub path_display_label {
613 my( $self, @wits ) = @_;
614 my $maj = scalar( $self->tradition->witnesses ) * 0.6;
615 if( scalar @wits > $maj ) {
616 # TODO break out a.c. wits
619 return join( ', ', @wits );
626 Returns a GraphML representation of the collation. The GraphML will contain
627 two graphs. The first expresses the attributes of the readings and the witness
628 paths that link them; the second expresses the relationships that link the
629 readings. This is the native transfer format for a tradition.
638 my $datafile = 't/data/florilegium_tei_ps.xml';
639 my $tradition = Text::Tradition->new( 'input' => 'TEI',
644 ok( $tradition, "Got a tradition object" );
645 is( scalar $tradition->witnesses, 13, "Found all witnesses" );
646 ok( $tradition->collation, "Tradition has a collation" );
648 my $c = $tradition->collation;
649 is( scalar $c->readings, $READINGS, "Collation has all readings" );
650 is( scalar $c->paths, $PATHS, "Collation has all paths" );
651 is( scalar $c->relationships, 0, "Collation has all relationships" );
653 # Add a few relationships
654 $c->add_relationship( 'w123', 'w125', { 'type' => 'collated' } );
655 $c->add_relationship( 'w193', 'w196', { 'type' => 'collated' } );
656 $c->add_relationship( 'w257', 'w262', { 'type' => 'transposition' } );
658 # Now write it to GraphML and parse it again.
660 my $graphml = $c->as_graphml;
661 my $st = Text::Tradition->new( 'input' => 'Self', 'string' => $graphml );
662 is( scalar $st->collation->readings, $READINGS, "Reparsed collation has all readings" );
663 is( scalar $st->collation->paths, $PATHS, "Reparsed collation has all paths" );
664 is( scalar $st->collation->relationships, 3, "Reparsed collation has new relationships" );
674 my $graphml_ns = 'http://graphml.graphdrawing.org/xmlns';
675 my $xsi_ns = 'http://www.w3.org/2001/XMLSchema-instance';
676 my $graphml_schema = 'http://graphml.graphdrawing.org/xmlns ' .
677 'http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd';
679 # Create the document and root node
680 my $graphml = XML::LibXML->createDocument( "1.0", "UTF-8" );
681 my $root = $graphml->createElementNS( $graphml_ns, 'graphml' );
682 $graphml->setDocumentElement( $root );
683 $root->setNamespace( $xsi_ns, 'xsi', 0 );
684 $root->setAttributeNS( $xsi_ns, 'schemaLocation', $graphml_schema );
686 # Add the data keys for the graph
689 my @graph_attributes = qw/ version wit_list_separator baselabel linear ac_label /;
690 foreach my $datum ( @graph_attributes ) {
691 $graph_data_keys{$datum} = 'dg'.$gdi++;
692 my $key = $root->addNewChild( $graphml_ns, 'key' );
693 $key->setAttribute( 'attr.name', $datum );
694 $key->setAttribute( 'attr.type', $key eq 'linear' ? 'boolean' : 'string' );
695 $key->setAttribute( 'for', 'graph' );
696 $key->setAttribute( 'id', $graph_data_keys{$datum} );
699 # Add the data keys for nodes
706 is_start => 'boolean',
708 is_lacuna => 'boolean',
710 foreach my $datum ( keys %node_data ) {
711 $node_data_keys{$datum} = 'dn'.$ndi++;
712 my $key = $root->addNewChild( $graphml_ns, 'key' );
713 $key->setAttribute( 'attr.name', $datum );
714 $key->setAttribute( 'attr.type', $node_data{$datum} );
715 $key->setAttribute( 'for', 'node' );
716 $key->setAttribute( 'id', $node_data_keys{$datum} );
719 # Add the data keys for edges, i.e. witnesses
723 class => 'string', # Class, deprecated soon
724 witness => 'string', # ID/label for a path
725 relationship => 'string', # ID/label for a relationship
726 extra => 'boolean', # Path key
727 scope => 'string', # Relationship key
728 non_correctable => 'boolean', # Relationship key
729 non_independent => 'boolean', # Relationship key
731 foreach my $datum ( keys %edge_data ) {
732 $edge_data_keys{$datum} = 'de'.$edi++;
733 my $key = $root->addNewChild( $graphml_ns, 'key' );
734 $key->setAttribute( 'attr.name', $datum );
735 $key->setAttribute( 'attr.type', $edge_data{$datum} );
736 $key->setAttribute( 'for', 'edge' );
737 $key->setAttribute( 'id', $edge_data_keys{$datum} );
740 # Add the collation graph itself
741 my $sgraph = $root->addNewChild( $graphml_ns, 'graph' );
742 $sgraph->setAttribute( 'edgedefault', 'directed' );
743 $sgraph->setAttribute( 'id', $self->tradition->name );
744 $sgraph->setAttribute( 'parse.edgeids', 'canonical' );
745 $sgraph->setAttribute( 'parse.edges', scalar($self->paths) );
746 $sgraph->setAttribute( 'parse.nodeids', 'canonical' );
747 $sgraph->setAttribute( 'parse.nodes', scalar($self->readings) );
748 $sgraph->setAttribute( 'parse.order', 'nodesfirst' );
750 # Collation attribute data
751 foreach my $datum ( @graph_attributes ) {
752 my $value = $datum eq 'version' ? '3.0' : $self->$datum;
753 _add_graphml_data( $sgraph, $graph_data_keys{$datum}, $value );
758 # Add our readings to the graph
759 foreach my $n ( sort { $a->id cmp $b->id } $self->readings ) {
760 # Add to the main graph
761 my $node_el = $sgraph->addNewChild( $graphml_ns, 'node' );
762 my $node_xmlid = 'n' . $node_ctr++;
763 $node_hash{ $n->id } = $node_xmlid;
764 $node_el->setAttribute( 'id', $node_xmlid );
765 foreach my $d ( keys %node_data ) {
767 _add_graphml_data( $node_el, $node_data_keys{$d}, $nval )
772 # Add the path edges to the sequence graph
774 foreach my $e ( sort { $a->[0] cmp $b->[0] } $self->sequence->edges() ) {
775 # We add an edge in the graphml for every witness in $e.
776 foreach my $wit ( $self->path_witnesses( $e ) ) {
777 my( $id, $from, $to ) = ( 'e'.$edge_ctr++,
778 $node_hash{ $e->[0] },
779 $node_hash{ $e->[1] } );
780 my $edge_el = $sgraph->addNewChild( $graphml_ns, 'edge' );
781 $edge_el->setAttribute( 'source', $from );
782 $edge_el->setAttribute( 'target', $to );
783 $edge_el->setAttribute( 'id', $id );
785 # It's a witness path, so add the witness
787 my $key = $edge_data_keys{'witness'};
788 # Is this an ante-corr witness?
789 my $aclabel = $self->ac_label;
790 if( $wit =~ /^(.*)\Q$aclabel\E$/ ) {
791 # Keep the base witness
793 # ...and record that this is an 'extra' reading path
794 _add_graphml_data( $edge_el, $edge_data_keys{'extra'}, $aclabel );
796 _add_graphml_data( $edge_el, $edge_data_keys{'witness'}, $base );
797 _add_graphml_data( $edge_el, $edge_data_keys{'class'}, 'path' );
801 # Add the relationship graph to the XML
802 $self->relations->as_graphml( $graphml_ns, $root, \%node_hash,
803 $node_data_keys{'id'}, \%edge_data_keys );
805 # Save and return the thing
806 my $result = decode_utf8( $graphml->toString(1) );
810 sub _add_graphml_data {
811 my( $el, $key, $value ) = @_;
812 return unless defined $value;
813 my $data_el = $el->addNewChild( $el->namespaceURI, 'data' );
814 $data_el->setAttribute( 'key', $key );
815 $data_el->appendText( $value );
820 Returns a CSV alignment table representation of the collation graph, one
821 row per witness (or witness uncorrected.)
827 my $table = $self->make_alignment_table;
828 my $csv = Text::CSV_XS->new( { binary => 1, quote_null => 0 } );
830 # Make the header row
831 $csv->combine( map { $_->{'witness'} } @{$table->{'alignment'}} );
832 push( @result, decode_utf8( $csv->string ) );
833 # Make the rest of the rows
834 foreach my $idx ( 0 .. $table->{'length'} - 1 ) {
835 my @rowobjs = map { $_->{'tokens'}->[$idx] } @{$table->{'alignment'}};
836 my @row = map { $_ ? $_->{'t'} : $_ } @rowobjs;
837 $csv->combine( @row );
838 push( @result, decode_utf8( $csv->string ) );
840 return join( "\n", @result );
843 =head2 make_alignment_table( $use_refs, $include_witnesses )
845 Return a reference to an alignment table, in a slightly enhanced CollateX
846 format which looks like this:
848 $table = { alignment => [ { witness => "SIGIL",
849 tokens => [ { t => "TEXT" }, ... ] },
851 tokens => [ { t => "TEXT" }, ... ] },
855 If $use_refs is set to 1, the reading object is returned in the table
856 instead of READINGTEXT; if not, the text of the reading is returned.
858 If $include_witnesses is set to a hashref, only the witnesses whose sigil
859 keys have a true hash value will be included.
863 sub make_alignment_table {
864 my( $self, $noderefs, $include ) = @_;
865 unless( $self->linear ) {
866 warn "Need a linear graph in order to make an alignment table";
869 my $table = { 'alignment' => [], 'length' => $self->end->rank - 1 };
870 my @all_pos = ( 1 .. $self->end->rank - 1 );
871 foreach my $wit ( sort { $a->sigil cmp $b->sigil } $self->tradition->witnesses ) {
873 next unless $include->{$wit->sigil};
875 # print STDERR "Making witness row(s) for " . $wit->sigil . "\n";
876 my @wit_path = $self->reading_sequence( $self->start, $self->end, $wit->sigil );
877 my @row = _make_witness_row( \@wit_path, \@all_pos, $noderefs );
878 push( @{$table->{'alignment'}},
879 { 'witness' => $wit->sigil, 'tokens' => \@row } );
880 if( $wit->is_layered ) {
881 my @wit_ac_path = $self->reading_sequence( $self->start, $self->end,
882 $wit->sigil.$self->ac_label, $wit->sigil );
883 my @ac_row = _make_witness_row( \@wit_ac_path, \@all_pos, $noderefs );
884 push( @{$table->{'alignment'}},
885 { 'witness' => $wit->sigil.$self->ac_label, 'tokens' => \@ac_row } );
891 sub _make_witness_row {
892 my( $path, $positions, $noderefs ) = @_;
894 map { $char_hash{$_} = undef } @$positions;
896 foreach my $rdg ( @$path ) {
897 my $rtext = $rdg->text;
898 $rtext = '#LACUNA#' if $rdg->is_lacuna;
899 print STDERR "rank " . $rdg->rank . "\n" if $debug;
900 # print STDERR "No rank for " . $rdg->id . "\n" unless defined $rdg->rank;
901 $char_hash{$rdg->rank} = $noderefs ? { 't' => $rdg }
904 my @row = map { $char_hash{$_} } @$positions;
905 # Fill in lacuna markers for undef spots in the row
906 my $last_el = shift @row;
907 my @filled_row = ( $last_el );
908 foreach my $el ( @row ) {
909 # If we are using node reference, make the lacuna node appear many times
910 # in the table. If not, use the lacuna tag.
911 if( $last_el && _el_is_lacuna( $last_el ) && !defined $el ) {
912 $el = $noderefs ? $last_el : { 't' => '#LACUNA#' };
914 push( @filled_row, $el );
920 # Tiny utility function to say if a table element is a lacuna
923 return 1 if $el->{'t'} eq '#LACUNA#';
924 return 1 if ref( $el->{'t'} ) eq 'Text::Tradition::Collation::Reading'
925 && $el->{'t'}->is_lacuna;
929 # Helper to turn the witnesses along columns rather than rows. Assumes
934 return $result unless scalar @$table;
935 my $nrows = scalar @{$table->[0]};
936 foreach my $idx ( 0 .. $nrows - 1 ) {
937 foreach my $wit ( 0 .. $#{$table} ) {
938 $result->[$idx]->[$wit] = $table->[$wit]->[$idx];
944 =head1 NAVIGATION METHODS
946 =head2 reading_sequence( $first, $last, $sigil, $backup )
948 Returns the ordered list of readings, starting with $first and ending
949 with $last, for the witness given in $sigil. If a $backup sigil is
950 specified (e.g. when walking a layered witness), it will be used wherever
951 no $sigil path exists. If there is a base text reading, that will be
952 used wherever no path exists for $sigil or $backup.
956 # TODO Think about returning some lazy-eval iterator.
958 sub reading_sequence {
959 my( $self, $start, $end, $witness, $backup ) = @_;
961 $witness = $self->baselabel unless $witness;
962 my @readings = ( $start );
965 while( $n && $n->id ne $end->id ) {
966 if( exists( $seen{$n->id} ) ) {
967 warn "Detected loop at " . $n->id;
972 my $next = $self->next_reading( $n, $witness, $backup );
974 warn "Did not find any path for $witness from reading " . $n->id;
977 push( @readings, $next );
980 # Check that the last reading is our end reading.
981 my $last = $readings[$#readings];
982 warn "Last reading found from " . $start->text .
983 " for witness $witness is not the end!"
984 unless $last->id eq $end->id;
989 =head2 next_reading( $reading, $sigil );
991 Returns the reading that follows the given reading along the given witness
997 # Return the successor via the corresponding path.
999 my $answer = $self->_find_linked_reading( 'next', @_ );
1000 return undef unless $answer;
1001 return $self->reading( $answer );
1004 =head2 prior_reading( $reading, $sigil )
1006 Returns the reading that precedes the given reading along the given witness
1012 # Return the predecessor via the corresponding path.
1014 my $answer = $self->_find_linked_reading( 'prior', @_ );
1015 return $self->reading( $answer );
1018 sub _find_linked_reading {
1019 my( $self, $direction, $node, $path, $alt_path ) = @_;
1020 my @linked_paths = $direction eq 'next'
1021 ? $self->sequence->edges_from( $node )
1022 : $self->sequence->edges_to( $node );
1023 return undef unless scalar( @linked_paths );
1025 # We have to find the linked path that contains all of the
1026 # witnesses supplied in $path.
1027 my( @path_wits, @alt_path_wits );
1028 @path_wits = sort( $self->_witnesses_of_label( $path ) ) if $path;
1029 @alt_path_wits = sort( $self->_witnesses_of_label( $alt_path ) ) if $alt_path;
1032 foreach my $le ( @linked_paths ) {
1033 if( $self->sequence->has_edge_attribute( @$le, $self->baselabel ) ) {
1036 my @le_wits = $self->path_witnesses( $le );
1037 if( _is_within( \@path_wits, \@le_wits ) ) {
1038 # This is the right path.
1039 return $direction eq 'next' ? $le->[1] : $le->[0];
1040 } elsif( _is_within( \@alt_path_wits, \@le_wits ) ) {
1044 # Got this far? Return the alternate path if it exists.
1045 return $direction eq 'next' ? $alt_le->[1] : $alt_le->[0]
1048 # Got this far? Return the base path if it exists.
1049 return $direction eq 'next' ? $base_le->[1] : $base_le->[0]
1052 # Got this far? We have no appropriate path.
1053 warn "Could not find $direction node from " . $node->id
1054 . " along path $path";
1060 my( $set1, $set2 ) = @_;
1061 my $ret = @$set1; # will be 0, i.e. false, if set1 is empty
1062 foreach my $el ( @$set1 ) {
1063 $ret = 0 unless grep { /^\Q$el\E$/ } @$set2;
1068 # Return the string that joins together a list of witnesses for
1069 # display on a single path.
1070 sub _witnesses_of_label {
1071 my( $self, $label ) = @_;
1072 my $regex = $self->wit_list_separator;
1073 my @answer = split( /\Q$regex\E/, $label );
1078 =head1 INITIALIZATION METHODS
1080 These are mostly for use by parsers.
1082 =head2 make_witness_path( $witness )
1084 Link the array of readings contained in $witness->path (and in
1085 $witness->uncorrected_path if it exists) into collation paths.
1086 Clear out the arrays when finished.
1088 =head2 make_witness_paths
1090 Call make_witness_path for all witnesses in the tradition.
1094 # For use when a collation is constructed from a base text and an apparatus.
1095 # We have the sequences of readings and just need to add path edges.
1096 # When we are done, clear out the witness path attributes, as they are no
1098 # TODO Find a way to replace the witness path attributes with encapsulated functions?
1100 sub make_witness_paths {
1102 foreach my $wit ( $self->tradition->witnesses ) {
1103 # print STDERR "Making path for " . $wit->sigil . "\n";
1104 $self->make_witness_path( $wit );
1108 sub make_witness_path {
1109 my( $self, $wit ) = @_;
1110 my @chain = @{$wit->path};
1111 my $sig = $wit->sigil;
1112 foreach my $idx ( 0 .. $#chain-1 ) {
1113 $self->add_path( $chain[$idx], $chain[$idx+1], $sig );
1115 if( $wit->is_layered ) {
1116 @chain = @{$wit->uncorrected_path};
1117 foreach my $idx( 0 .. $#chain-1 ) {
1118 my $source = $chain[$idx];
1119 my $target = $chain[$idx+1];
1120 $self->add_path( $source, $target, $sig.$self->ac_label )
1121 unless $self->has_path( $source, $target, $sig );
1125 $wit->clear_uncorrected_path;
1128 =head2 calculate_ranks
1130 Calculate the reading ranks (that is, their aligned positions relative
1131 to each other) for the graph. This can only be called on linear collations.
1135 sub calculate_ranks {
1137 # Walk a version of the graph where every node linked by a relationship
1138 # edge is fundamentally the same node, and do a topological ranking on
1139 # the nodes in this graph.
1140 my $topo_graph = Graph->new();
1144 foreach my $r ( $self->readings ) {
1145 next if exists $rel_containers{$r->id};
1146 my @rels = $r->related_readings( 'colocated' );
1148 # Make a relationship container.
1150 my $rn = 'rel_container_' . $rel_ctr++;
1151 $topo_graph->add_vertex( $rn );
1153 $rel_containers{$_->id} = $rn;
1156 # Add a new node to mirror the old node.
1157 $rel_containers{$r->id} = $r->id;
1158 $topo_graph->add_vertex( $r->id );
1163 foreach my $r ( $self->readings ) {
1164 foreach my $n ( $self->sequence->successors( $r->id ) ) {
1165 my( $tfrom, $tto ) = ( $rel_containers{$r->id},
1166 $rel_containers{$n} );
1167 # $DB::single = 1 unless $tfrom && $tto;
1168 $topo_graph->add_edge( $tfrom, $tto );
1172 # Now do the rankings, starting with the start node.
1173 my $topo_start = $rel_containers{$self->start->id};
1174 my $node_ranks = { $topo_start => 0 };
1175 my @curr_origin = ( $topo_start );
1176 # A little iterative function.
1177 while( @curr_origin ) {
1178 @curr_origin = _assign_rank( $topo_graph, $node_ranks, @curr_origin );
1180 # Transfer our rankings from the topological graph to the real one.
1181 foreach my $r ( $self->readings ) {
1182 if( defined $node_ranks->{$rel_containers{$r->id}} ) {
1183 $r->rank( $node_ranks->{$rel_containers{$r->id}} );
1185 die "No rank calculated for node " . $r->id
1186 . " - do you have a cycle in the graph?";
1192 my( $graph, $node_ranks, @current_nodes ) = @_;
1193 # Look at each of the children of @current_nodes. If all the child's
1194 # parents have a rank, assign it the highest rank + 1 and add it to
1195 # @next_nodes. Otherwise skip it; we will return when the highest-ranked
1196 # parent gets a rank.
1198 foreach my $c ( @current_nodes ) {
1199 warn "Current reading $c has no rank!"
1200 unless exists $node_ranks->{$c};
1201 # print STDERR "Looking at child of node $c, rank "
1202 # . $node_ranks->{$c} . "\n";
1203 foreach my $child ( $graph->successors( $c ) ) {
1204 next if exists $node_ranks->{$child};
1205 my $highest_rank = -1;
1207 foreach my $parent ( $graph->predecessors( $child ) ) {
1208 if( exists $node_ranks->{$parent} ) {
1209 $highest_rank = $node_ranks->{$parent}
1210 if $highest_rank <= $node_ranks->{$parent};
1217 my $c_rank = $highest_rank + 1;
1218 # print STDERR "Assigning rank $c_rank to node $child \n";
1219 $node_ranks->{$child} = $c_rank;
1220 push( @next_nodes, $child );
1226 =head2 flatten_ranks
1228 A convenience method for parsing collation data. Searches the graph for readings
1229 with the same text at the same rank, and merges any that are found.
1235 my %unique_rank_rdg;
1236 foreach my $rdg ( $self->readings ) {
1237 next unless $rdg->has_rank;
1238 my $key = $rdg->rank . "||" . $rdg->text;
1239 if( exists $unique_rank_rdg{$key} ) {
1241 # print STDERR "Combining readings at same rank: $key\n";
1242 $self->merge_readings( $unique_rank_rdg{$key}, $rdg );
1244 $unique_rank_rdg{$key} = $rdg;
1250 =head1 UTILITY FUNCTIONS
1252 =head2 common_predecessor( $reading_a, $reading_b )
1254 Find the last reading that occurs in sequence before both the given readings.
1256 =head2 common_successor( $reading_a, $reading_b )
1258 Find the first reading that occurs in sequence after both the given readings.
1262 use Text::Tradition;
1264 my $cxfile = 't/data/Collatex-16.xml';
1265 my $t = Text::Tradition->new(
1267 'input' => 'CollateX',
1270 my $c = $t->collation;
1272 is( $c->common_predecessor( 'n9', 'n23' )->id,
1273 'n20', "Found correct common predecessor" );
1274 is( $c->common_successor( 'n9', 'n23' )->id,
1275 '#END#', "Found correct common successor" );
1277 is( $c->common_predecessor( 'n19', 'n17' )->id,
1278 'n16', "Found correct common predecessor for readings on same path" );
1279 is( $c->common_successor( 'n21', 'n26' )->id,
1280 '#END#', "Found correct common successor for readings on same path" );
1286 ## Return the closest reading that is a predecessor of both the given readings.
1287 sub common_predecessor {
1289 my( $r1, $r2 ) = $self->_objectify_args( @_ );
1290 return $self->common_in_path( $r1, $r2, 'predecessors' );
1293 sub common_successor {
1295 my( $r1, $r2 ) = $self->_objectify_args( @_ );
1296 return $self->common_in_path( $r1, $r2, 'successors' );
1299 sub common_in_path {
1300 my( $self, $r1, $r2, $dir ) = @_;
1301 my $iter = $r1->rank > $r2->rank ? $r1->rank : $r2->rank;
1302 $iter = $self->end->rank - $iter if $dir eq 'successors';
1304 my @last_checked = ( $r1, $r2 );
1306 while( !@candidates ) {
1308 foreach my $lc ( @last_checked ) {
1309 foreach my $p ( $lc->$dir ) {
1310 if( $all_seen{$p->id} ) {
1311 push( @candidates, $p );
1313 $all_seen{$p->id} = 1;
1314 push( @new_lc, $p );
1318 @last_checked = @new_lc;
1320 my @answer = sort { $a->rank <=> $b->rank } @candidates;
1321 return $dir eq 'predecessors' ? pop( @answer ) : shift ( @answer );
1325 __PACKAGE__->meta->make_immutable;
1331 =item * Get rid of $backup in reading_sequence