checkpoint, not sure what is here
[scpubgit/stemmatology.git] / lib / Text / Tradition / Parser / CollateX.pm
CommitLineData
cda6a45b 1package Text::Tradition::Parser::CollateX;
2
3use strict;
4use warnings;
5use Text::Tradition::Parser::GraphML;
6
7=head1 NAME
8
9Text::Tradition::Parser::CollateX
10
11=head1 DESCRIPTION
12
13Parser module for Text::Tradition, given a GraphML file from the
14CollateX program that describes a collation graph. For further
15information on the GraphML format for text collation, see
16http://gregor.middell.net/collatex/
17
18=head1 METHODS
19
20=over
21
22=item B<parse>
23
24parse( $graph, $graphml_string );
25
26Takes an initialized Text::Tradition::Graph object and a string
27containing the GraphML; creates the appropriate nodes and edges on the
28graph.
29
30=cut
31
32my $IDKEY = 'number';
33my $CONTENTKEY = 'token';
34my $TRANSKEY = 'identical';
35
36sub parse {
37 my( $tradition, $graphml_str ) = @_;
38 my $graph_data = Text::Tradition::Parser::GraphML::parse( $graphml_str );
39 my $collation = $tradition->collation;
40 my %witnesses; # Keep track of the witnesses we encounter as we
41 # run through the graph data.
42
43 # Add the nodes to the graph. First delete the start node, because
44 # GraphML graphs will have their own start nodes.
45 $collation->del_reading( $collation->start() );
46
47 my $extra_data = {}; # Keep track of info to be processed after all
48 # nodes have been created
49 foreach my $n ( @{$graph_data->{'nodes'}} ) {
50 my %node_data = %$n;
51 my $nodeid = delete $node_data{$IDKEY};
52 my $token = delete $node_data{$CONTENTKEY};
f6066bac 53 unless( defined $nodeid && defined $token ) {
54 $DB::single = 1;
cda6a45b 55 warn "Did not find an ID or token for graph node, can't add it";
56 next;
57 }
58 my $gnode = $collation->add_reading( $nodeid );
59 $gnode->text( $token );
60
61 # Whatever is left is extra info to be processed later.
62 if( keys %node_data ) {
63 $extra_data->{$nodeid} = \%node_data;
64 }
65 }
66
67 # Now add the edges.
68 foreach my $e ( @{$graph_data->{'edges'}} ) {
69 my %edge_data = %$e;
70 my $from = delete $edge_data{'source'};
71 my $to = delete $edge_data{'target'};
72
73 # In CollateX, we have a distinct witness data ID per witness,
74 # so that we can have multiple witnesses per edge. We want to
75 # translate this to one witness per edge in our own
76 # representation.
77 foreach my $ekey ( keys %edge_data ) {
78 my $wit = $edge_data{$ekey};
79 # Create the witness object if it does not yet exist.
80 unless( $witnesses{$wit} ) {
81 $tradition->add_witness( 'sigil' => $wit );
82 $witnesses{$wit} = 1;
83 }
84 $collation->add_path( $from->{$IDKEY}, $to->{$IDKEY}, $wit );
85 }
86 }
87
88 # Process the extra node data if it exists.
89 foreach my $nodeid ( keys %$extra_data ) {
90 my $ed = $extra_data->{$nodeid};
91 if( exists $ed->{$TRANSKEY} ) {
92
93 my $tn_reading = $collation->reading( $nodeid );
94 my $main_reading = $collation->reading( $ed->{$TRANSKEY} );
95 if( $collation->linear ) {
96 $tn_reading->set_identical( $main_reading );
97 } else {
98 $collation->merge_readings( $main_reading, $tn_reading );
99 }
100 } # else we don't have any other tags to process yet.
101 }
102
103 # Find the beginning and end nodes of the graph. The beginning node
104 # has no incoming edges; the end node has no outgoing edges.
105 my( $begin_node, $end_node );
106 foreach my $gnode ( $collation->readings() ) {
107 # print STDERR "Checking node " . $gnode->name . "\n";
108 my @outgoing = $gnode->outgoing();
109 my @incoming = $gnode->incoming();
110
111 unless( scalar @incoming ) {
112 warn "Already have a beginning node" if $begin_node;
113 $begin_node = $gnode;
114 $collation->start( $gnode );
115 }
116 unless( scalar @outgoing ) {
117 warn "Already have an ending node" if $end_node;
118 $end_node = $gnode;
119 }
120 }
121
122 # Record for each witness its sequence of readings, and determine
123 # the common nodes.
124 my @common_nodes = $collation->walk_witness_paths( $end_node );
125
126 # Now we have added the witnesses and their paths, so have also
127 # implicitly marked the common nodes. Now we can calculate their
128 # explicit positions.
129 $collation->calculate_positions( @common_nodes );
130}
131
132=back
133
134=head1 LICENSE
135
136This package is free software and is provided "as is" without express
137or implied warranty. You can redistribute it and/or modify it under
138the same terms as Perl itself.
139
140=head1 AUTHOR
141
142Tara L Andrews, aurum@cpan.org
143
144=cut
145
1461;