1 package Text::Tradition::Parser::Self;
5 use Text::Tradition::Parser::GraphML;
9 Text::Tradition::Parser::GraphML
13 Parser module for Text::Tradition to read in its own GraphML output format.
14 TODO document what this format is.
22 parse( $graph, $graphml_string );
24 Takes an initialized Text::Tradition::Graph object and a string
25 containing the GraphML; creates the appropriate nodes and edges on the
30 my( $IDKEY, $TOKENKEY, $TRANSPOS_KEY, $RANK_KEY, $CLASS_KEY )
31 = qw/ name reading identical rank class /;
34 my( $tradition, $graphml_str ) = @_;
35 my $graph_data = Text::Tradition::Parser::GraphML::parse( $graphml_str );
37 my $collation = $tradition->collation;
40 # Add the nodes to the graph.
41 # TODO Are we adding extra start/end nodes?
43 my $extra_data = {}; # Keep track of data that needs to be processed
44 # after the nodes & edges are created.
45 print STDERR "Adding graph nodes\n";
46 foreach my $n ( @{$graph_data->{'nodes'}} ) {
47 # Each node is either a segment or a reading, depending on
48 # its class. Readings have text, segments don't.
50 my $nodeid = delete $node_data{$IDKEY};
51 my $reading = delete $node_data{$TOKENKEY};
52 my $class = $node_data{$CLASS_KEY} || '';
53 # TODO this is a hack, fix it?
54 $class = 'reading' unless $class eq 'segment';
55 my $method = $class eq 'segment' ? "add_$class" : "add_reading";
56 my $gnode = $collation->$method( $nodeid );
57 $gnode->label( $reading );
58 $gnode->set_common if $class eq 'common';
60 # Now save the rest of the data, i.e. not the ID or label,
62 if ( keys %node_data ) {
63 $extra_data->{$nodeid} = \%node_data;
68 print STDERR "Adding graph edges\n";
69 foreach my $e ( @{$graph_data->{'edges'}} ) {
71 my $from = delete $edge_data{'source'};
72 my $to = delete $edge_data{'target'};
73 my $class = delete $edge_data{'class'};
75 # Whatever is left tells us what kind of edge it is.
76 foreach my $wkey ( keys %edge_data ) {
77 if( $wkey =~ /^witness/ ) {
78 unless( $class eq 'path' ) {
79 warn "Cannot add witness label to a $class edge";
82 my $wit = $edge_data{$wkey};
83 unless( $witnesses{$wit} ) {
84 $tradition->add_witness( sigil => $wit );
87 my $label = $wkey eq 'witness_ante_corr'
88 ? $wit . $collation->ac_label : $wit;
89 $collation->add_path( $from->{$IDKEY}, $to->{$IDKEY}, $label );
90 } elsif( $wkey eq 'relationship' ) {
91 unless( $class eq 'relationship' ) {
92 warn "Cannot add relationship label to a $class edge";
95 my $rel = $edge_data{$wkey};
96 # TODO handle global relationships
97 $collation->add_relationship( $rel, $from->{$IDKEY}, $to->{$IDKEY} );
99 my $seg_edge = $collation->graph->add_edge( $from->{$IDKEY}, $to->{$IDKEY} );
100 $seg_edge->set_attribute( 'class', 'segment' );
105 ## Deal with node information (transposition, relationships, etc.) that
106 ## needs to be processed after all the nodes are created.
107 print STDERR "Adding second-pass data\n";
109 foreach my $nkey ( keys %$extra_data ) {
110 foreach my $edkey ( keys %{$extra_data->{$nkey}} ) {
111 my $this_reading = $collation->reading( $nkey );
112 if( $edkey eq $TRANSPOS_KEY ) {
113 my $other_reading = $collation->reading( $extra_data->{$nkey}->{$edkey} );
114 # We evidently have a linear graph.
116 $this_reading->set_identical( $other_reading );
117 } elsif ( $edkey eq $RANK_KEY ) {
118 $this_reading->rank( $extra_data->{$nkey}->{$edkey} );
120 warn "Unfamiliar reading node data $edkey for $nkey";
124 $collation->linear( $linear );
125 # TODO We probably need to set the $witness->path arrays for each wit.
132 This package is free software and is provided "as is" without express
133 or implied warranty. You can redistribute it and/or modify it under
134 the same terms as Perl itself.
138 Tara L Andrews, aurum@cpan.org