X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FText%2FTradition%2FParser%2FCollateX.pm;h=2ea2cadec907485677ec533d71b907c3808bf0e5;hb=d9e873d0f90f1b7aec072c16a0eed37878f7f47f;hp=2c13921225eea7c5f48fc309300ea3cac677dbb2;hpb=cda6a45b9b206364ffe567844cbfa1d80697fd81;p=scpubgit%2Fstemmatology.git diff --git a/lib/Text/Tradition/Parser/CollateX.pm b/lib/Text/Tradition/Parser/CollateX.pm index 2c13921..2ea2cad 100644 --- a/lib/Text/Tradition/Parser/CollateX.pm +++ b/lib/Text/Tradition/Parser/CollateX.pm @@ -43,89 +43,89 @@ sub parse { # Add the nodes to the graph. First delete the start node, because # GraphML graphs will have their own start nodes. $collation->del_reading( $collation->start() ); + $collation->del_reading( $collation->end() ); my $extra_data = {}; # Keep track of info to be processed after all # nodes have been created foreach my $n ( @{$graph_data->{'nodes'}} ) { - my %node_data = %$n; - my $nodeid = delete $node_data{$IDKEY}; - my $token = delete $node_data{$CONTENTKEY}; - unless( $nodeid && $token ) { - warn "Did not find an ID or token for graph node, can't add it"; - next; - } - my $gnode = $collation->add_reading( $nodeid ); - $gnode->text( $token ); - - # Whatever is left is extra info to be processed later. - if( keys %node_data ) { - $extra_data->{$nodeid} = \%node_data; - } + my %node_data = %$n; + my $nodeid = delete $node_data{$IDKEY}; + my $token = delete $node_data{$CONTENTKEY}; + unless( defined $nodeid && defined $token ) { + warn "Did not find an ID or token for graph node, can't add it"; + next; + } + my $gnode = $collation->add_reading( $nodeid ); + $gnode->text( $token ); + + # Whatever is left is extra info to be processed later. + if( keys %node_data ) { + $extra_data->{$nodeid} = \%node_data; + } } - + # Now add the edges. foreach my $e ( @{$graph_data->{'edges'}} ) { - my %edge_data = %$e; - my $from = delete $edge_data{'source'}; - my $to = delete $edge_data{'target'}; - - # In CollateX, we have a distinct witness data ID per witness, - # so that we can have multiple witnesses per edge. We want to - # translate this to one witness per edge in our own - # representation. - foreach my $ekey ( keys %edge_data ) { - my $wit = $edge_data{$ekey}; - # Create the witness object if it does not yet exist. - unless( $witnesses{$wit} ) { - $tradition->add_witness( 'sigil' => $wit ); - $witnesses{$wit} = 1; - } - $collation->add_path( $from->{$IDKEY}, $to->{$IDKEY}, $wit ); - } + my %edge_data = %$e; + my $from = delete $edge_data{'source'}; + my $to = delete $edge_data{'target'}; + + # In CollateX, we have a distinct witness data ID per witness, + # so that we can have multiple witnesses per edge. We want to + # translate this to one witness per edge in our own + # representation. + foreach my $ekey ( keys %edge_data ) { + my $wit = $edge_data{$ekey}; + # Create the witness object if it does not yet exist. + unless( $witnesses{$wit} ) { + $tradition->add_witness( 'sigil' => $wit ); + $witnesses{$wit} = 1; + } + $collation->add_path( $from->{$IDKEY}, $to->{$IDKEY}, $wit ); + } } # Process the extra node data if it exists. foreach my $nodeid ( keys %$extra_data ) { - my $ed = $extra_data->{$nodeid}; - if( exists $ed->{$TRANSKEY} ) { - - my $tn_reading = $collation->reading( $nodeid ); - my $main_reading = $collation->reading( $ed->{$TRANSKEY} ); - if( $collation->linear ) { - $tn_reading->set_identical( $main_reading ); - } else { - $collation->merge_readings( $main_reading, $tn_reading ); - } - } # else we don't have any other tags to process yet. + my $ed = $extra_data->{$nodeid}; + if( exists $ed->{$TRANSKEY} ) { + + my $tn_reading = $collation->reading( $nodeid ); + my $main_reading = $collation->reading( $ed->{$TRANSKEY} ); + if( $collation->linear ) { + $tn_reading->set_identical( $main_reading ); + } else { + $collation->merge_readings( $main_reading, $tn_reading ); + } + } # else we don't have any other tags to process yet. } # Find the beginning and end nodes of the graph. The beginning node # has no incoming edges; the end node has no outgoing edges. my( $begin_node, $end_node ); foreach my $gnode ( $collation->readings() ) { - # print STDERR "Checking node " . $gnode->name . "\n"; - my @outgoing = $gnode->outgoing(); - my @incoming = $gnode->incoming(); - - unless( scalar @incoming ) { - warn "Already have a beginning node" if $begin_node; - $begin_node = $gnode; - $collation->start( $gnode ); - } - unless( scalar @outgoing ) { - warn "Already have an ending node" if $end_node; - $end_node = $gnode; - } + # print STDERR "Checking node " . $gnode->name . "\n"; + my @outgoing = $gnode->outgoing(); + my @incoming = $gnode->incoming(); + + unless( scalar @incoming ) { + warn "Already have a beginning node" if $begin_node; + $begin_node = $gnode; + $collation->start( $gnode ); + } + unless( scalar @outgoing ) { + warn "Already have an ending node" if $end_node; + $end_node = $gnode; + $collation->end( $gnode ); + } } + + # TODO Need to populate $wit->path / uncorrected_path - # Record for each witness its sequence of readings, and determine - # the common nodes. - my @common_nodes = $collation->walk_witness_paths( $end_node ); - - # Now we have added the witnesses and their paths, so have also - # implicitly marked the common nodes. Now we can calculate their - # explicit positions. - $collation->calculate_positions( @common_nodes ); + # Now we have added the witnesses and their paths, so we can + # calculate their explicit positions. + # TODO CollateX does this, and we should just have it exported there. + $collation->calculate_ranks(); } =back