X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FText%2FTradition%2FParser%2FGraphML.pm;h=3ecd93680877c35060ff12af7fd49cca17a740aa;hb=e867486f69f12dc06304594022c298935d1c7fb9;hp=c29f78a0a1211116754f49d128c6e7b39ab4d3d4;hpb=044d1e4538c88df85fd520a1213d88d1f24a7984;p=scpubgit%2Fstemmatology.git diff --git a/lib/Text/Tradition/Parser/GraphML.pm b/lib/Text/Tradition/Parser/GraphML.pm index c29f78a..3ecd936 100644 --- a/lib/Text/Tradition/Parser/GraphML.pm +++ b/lib/Text/Tradition/Parser/GraphML.pm @@ -2,9 +2,14 @@ package Text::Tradition::Parser::GraphML; use strict; use warnings; +use Exporter 'import'; +use vars qw/ @EXPORT_OK $xpc /; + use XML::LibXML; use XML::LibXML::XPathContext; +@EXPORT_OK = qw/ graphml_parse populate_witness_path /; + =head1 NAME Text::Tradition::Parser::GraphML @@ -19,9 +24,7 @@ GraphML conventions of the source program. =head1 METHODS -=over - -=item B +=head2 B( $init_opts ) parse( $init_opts ); @@ -31,17 +34,15 @@ and their associated data. =cut -use vars qw/ $xpc $graphattr $nodedata $witnesses /; - # Return graph -> nodeid -> { key1/val1, key2/val2, key3/val3 ... } # -> edgeid -> { source, target, wit1/val1, wit2/val2 ...} -sub parse { +sub graphml_parse { my( $opts ) = @_; my $graph_hash = { 'nodes' => [], 'edges' => [] }; - + my $parser = XML::LibXML->new(); my $doc; if( exists $opts->{'string'} ) { @@ -53,6 +54,7 @@ sub parse { return; } + my( $graphattr, $nodedata, $witnesses ) = ( {}, {}, {} ); my $graphml = $doc->documentElement(); $xpc = XML::LibXML::XPathContext->new( $graphml ); $xpc->registerNs( 'g', 'http://graphml.graphdrawing.org/xmlns' ); @@ -127,6 +129,29 @@ sub parse { return $graph_hash; } +=head2 B( $tradition ) + +Given a tradition, populate the 'path' and 'uncorrected_path' attributes +of all of its witnesses. Useful for all formats based on the graph itself. + +=cut + +sub populate_witness_path { + my ( $tradition, $ante_corr ) = @_; + my $c = $tradition->collation; + print STDERR "Walking paths for witnesses\n"; + foreach my $wit ( $tradition->witnesses ) { + my @path = $c->reading_sequence( $c->start, $c->end, $wit->sigil ); + $wit->path( \@path ); + if( $ante_corr->{$wit->sigil} ) { + # Get the uncorrected path too + my @uc = $c->reading_sequence( $c->start, $c->end, + $wit->sigil . $c->ac_label, $wit->sigil ); + $wit->uncorrected_path( \@uc ); + } + } +} + sub _lookup_node_data { my( $xmlnode, $key ) = @_; my $lookup_xpath = './g:data[@key="%s"]/child::text()';