1 package Text::Tradition::Parser::Self;
5 use Text::Tradition::Parser::GraphML qw/ graphml_parse /;
9 Text::Tradition::Parser::GraphML
15 my $t_from_file = Text::Tradition->new(
18 'file' => '/path/to/tradition.xml'
21 my $t_from_string = Text::Tradition->new(
24 'string' => $tradition_xml,
29 Parser module for Text::Tradition to read in its own GraphML output format.
30 GraphML is a relatively simple graph description language; a 'graph' element
31 can have 'node' and 'edge' elements, and each of these can have simple 'data'
32 elements for attributes to be saved.
34 The graph itself has attributes as in the Collation object:
44 =item * wit_list_separator
48 The node objects have the following attributes:
64 The edge objects have the following attributes:
70 =item * witness (for 'path' class edges)
72 =item * extra (for 'path' class edges)
74 =item * relationship (for 'relationship' class edges)
76 =item * equal_rank (for 'relationship' class edges)
78 =item * non_correctable (for 'relationship' class edges)
80 =item * non_independent (for 'relationship' class edges)
88 parse( $graph, $opts );
90 Takes an initialized Text::Tradition object and a set of options; creates
91 the appropriate nodes and edges on the graph. The options hash should
92 include either a 'file' argument or a 'string' argument, depending on the
93 source of the XML to be parsed.
98 binmode STDOUT, ":utf8";
99 binmode STDERR, ":utf8";
100 eval { no warnings; binmode $DB::OUT, ":utf8"; };
102 my $tradition = 't/data/florilegium_graphml.xml';
103 my $t = Text::Tradition->new(
106 'file' => $tradition,
109 is( ref( $t ), 'Text::Tradition', "Parsed our own GraphML" );
111 is( scalar $t->collation->readings, 319, "Collation has all readings" );
112 is( scalar $t->collation->paths, 376, "Collation has all paths" );
113 is( scalar $t->witnesses, 13, "Collation has all witnesses" );
120 my( $IDKEY, $TOKENKEY, $TRANSPOS_KEY, $RANK_KEY, $CLASS_KEY,
121 $START_KEY, $END_KEY, $LACUNA_KEY,
122 $SOURCE_KEY, $TARGET_KEY, $WITNESS_KEY, $EXTRA_KEY, $RELATIONSHIP_KEY,
123 $COLO_KEY, $CORRECT_KEY, $INDEP_KEY )
124 = qw/ name reading identical rank class
125 is_start is_end is_lacuna
126 source target witness extra relationship
127 equal_rank non_correctable non_independent /;
130 my( $tradition, $opts ) = @_;
131 my $graph_data = graphml_parse( $opts );
133 my $collation = $tradition->collation;
136 # Set up the graph-global attributes. They will appear in the
137 # hash under their accessor names.
139 print STDERR "Setting graph globals\n";
140 $tradition->name( $graph_data->{'name'} );
141 foreach my $gkey ( keys %{$graph_data->{'global'}} ) {
142 my $val = $graph_data->{'global'}->{$gkey};
143 if( $gkey eq 'version' ) {
146 $collation->$gkey( $val );
150 # Many of our tags changed.
153 $COLO_KEY = 'colocated';
156 # Add the nodes to the graph.
158 my $extra_data = {}; # Keep track of data that needs to be processed
159 # after the nodes & edges are created.
160 print STDERR "Adding graph nodes\n";
161 foreach my $n ( @{$graph_data->{'nodes'}} ) {
162 # If it is the start or end node, we already have one, so skip it.
163 next if defined $n->{$START_KEY} || defined $n->{$END_KEY};
165 # First extract the data that we can use without reference to
167 my %node_data = %$n; # Need $n itself untouched for edge processing
170 my $reading_options = {
171 'id' => delete $node_data{$IDKEY},
172 'is_lacuna' => delete $node_data{$LACUNA_KEY},
174 my $rank = delete $node_data{$RANK_KEY};
175 $reading_options->{'rank'} = $rank if $rank;
176 my $text = delete $node_data{$TOKENKEY};
177 $reading_options->{'text'} = $text if $text;
179 # This is a horrible hack for backwards compatibility.
180 unless( $use_version ) {
181 $reading_options->{'is_lacuna'} = 1
182 if $reading_options->{'text'} =~ /^\#LACUNA/;
185 delete $node_data{$CLASS_KEY}; # Not actually used
186 my $gnode = $collation->add_reading( $reading_options );
188 # Now save the data that we need for post-processing,
189 # if it exists. TODO this is unneeded after conversion
190 if ( keys %node_data ) {
191 $extra_data->{$gnode->id} = \%node_data
196 print STDERR "Adding graph edges\n";
197 foreach my $e ( @{$graph_data->{'edges'}} ) {
198 my $from = $e->{$SOURCE_KEY};
199 my $to = $e->{$TARGET_KEY};
200 my $class = $e->{$CLASS_KEY};
202 # We may have more information depending on the class.
203 if( $class eq 'path' ) {
204 # We need the witness, and whether it is an 'extra' reading path.
205 my $wit = $e->{$WITNESS_KEY};
206 warn "No witness label on path edge!" unless $wit;
207 my $extra = $e->{$EXTRA_KEY};
208 my $label = $wit . ( $extra ? $collation->ac_label : '' );
209 $collation->add_path( $from->{$IDKEY}, $to->{$IDKEY}, $label );
210 # Add the witness if we don't have it already.
211 unless( $witnesses{$wit} ) {
212 $tradition->add_witness( sigil => $wit );
213 $witnesses{$wit} = 1;
215 $tradition->witness( $wit )->is_layered( 1 ) if $extra;
216 } elsif( $class eq 'relationship' ) {
217 # We need the metadata about the relationship.
218 my $opts = { 'type' => $e->{$RELATIONSHIP_KEY} };
219 $opts->{$COLO_KEY} = $e->{$COLO_KEY}
220 if exists $e->{$COLO_KEY};
221 $opts->{$CORRECT_KEY} = $e->{$CORRECT_KEY}
222 if exists $e->{$CORRECT_KEY};
223 $opts->{$INDEP_KEY} = $e->{$INDEP_KEY}
224 if exists $e->{$INDEP_KEY};
225 warn "No relationship type for relationship edge!" unless $opts->{'type'};
226 my( $ok, @result ) = $collation->add_relationship( $from->{$IDKEY}, $to->{$IDKEY}, $opts );
228 warn "Did not add relationship: @result";
233 ## Deal with node information (transposition, relationships, etc.) that
234 ## needs to be processed after all the nodes are created.
235 ## TODO unneeded after conversion
236 unless( $use_version ) {
237 print STDERR "Adding second-pass node data\n";
238 foreach my $nkey ( keys %$extra_data ) {
239 foreach my $edkey ( keys %{$extra_data->{$nkey}} ) {
240 my $this_reading = $collation->reading( $nkey );
241 if( $edkey eq $TRANSPOS_KEY ) {
242 my $other_reading = $collation->reading( $extra_data->{$nkey}->{$edkey} );
243 $this_reading->set_identical( $other_reading );
245 warn "Unfamiliar reading node data $edkey for $nkey";
258 =item * Make this into a stream parser with GraphML
260 =item * Simply field -> attribute correspondence for nodes and edges
262 =item * Share key name constants with Collation.pm
268 This package is free software and is provided "as is" without express
269 or implied warranty. You can redistribute it and/or modify it under
270 the same terms as Perl itself.
274 Tara L Andrews E<lt>aurum@cpan.orgE<gt>