1 package Text::Tradition::Parser::CollateX;
5 use Text::Tradition::Parser::GraphML qw/ graphml_parse /;
9 Text::Tradition::Parser::CollateX
15 my $t_from_file = Text::Tradition->new(
17 'input' => 'CollateX',
18 'file' => '/path/to/collation.xml'
21 my $t_from_string = Text::Tradition->new(
23 'input' => 'CollateX',
24 'string' => $collation_xml,
29 Parser module for Text::Tradition, given a GraphML file from the
30 CollateX program that describes a collation graph. For further
31 information on the GraphML format for text collation, see
32 http://gregor.middell.net/collatex/
38 parse( $tradition, $init_options );
40 Takes an initialized Text::Tradition object and a set of options; creates
41 the appropriate nodes and edges on the graph. The options hash should
42 include either a 'file' argument or a 'string' argument, depending on the
43 source of the XML to be parsed.
48 binmode STDOUT, ":utf8";
49 binmode STDERR, ":utf8";
50 eval { no warnings; binmode $DB::OUT, ":utf8"; };
52 my $cxfile = 't/data/Collatex-16.xml';
53 my $t = Text::Tradition->new(
55 'input' => 'CollateX',
59 is( ref( $t ), 'Text::Tradition', "Parsed our own GraphML" );
61 is( scalar $t->collation->readings, 26, "Collation has all readings" );
62 is( scalar $t->collation->paths, 32, "Collation has all paths" );
63 is( scalar $t->witnesses, 3, "Collation has all witnesses" );
65 # Check an 'identical' node
66 my $transposed = $t->collation->reading( 'n15' );
67 my @related = $transposed->related_readings;
68 is( scalar @related, 1, "Reading links to transposed version" );
69 is( $related[0]->id, 'n17', "Correct transposition link" );
77 my $CONTENTKEY = 'token';
78 my $TRANSKEY = 'identical';
81 my( $tradition, $opts ) = @_;
82 my( $graph_data ) = graphml_parse( $opts );
83 my $collation = $tradition->collation;
85 # First add the readings to the graph.
86 my $extra_data = {}; # Keep track of info to be processed after all
87 # nodes have been created
88 foreach my $n ( @{$graph_data->{'nodes'}} ) {
89 unless( defined $n->{$IDKEY} && defined $n->{$CONTENTKEY} ) {
90 warn "Did not find an ID or token for graph node, can't add it";
95 'id' => delete $node_data{$IDKEY},
96 'text' => delete $node_data{$CONTENTKEY},
98 my $gnode = $collation->add_reading( $gnode_args );
100 # Whatever is left is extra info to be processed later,
101 # e.g. a transposition link.
102 if( keys %node_data ) {
103 $extra_data->{$gnode->id} = \%node_data;
107 # Now add the path edges.
108 foreach my $e ( @{$graph_data->{'edges'}} ) {
110 my $from = delete $edge_data{'source'};
111 my $to = delete $edge_data{'target'};
113 # In CollateX, we have a distinct witness data ID per witness,
114 # so that we can have multiple witnesses per edge. We want to
115 # translate this to one witness per edge in our own
117 foreach my $ekey ( keys %edge_data ) {
118 my $wit = $edge_data{$ekey};
119 # Create the witness object if it does not yet exist.
120 unless( $tradition->witness( $wit ) ) {
121 $tradition->add_witness( 'sigil' => $wit );
123 $collation->add_path( $from->{$IDKEY}, $to->{$IDKEY}, $wit );
127 # Process the extra node data if it exists.
128 foreach my $nodeid ( keys %$extra_data ) {
129 my $ed = $extra_data->{$nodeid};
130 if( exists $ed->{$TRANSKEY} ) {
131 my $tn_reading = $collation->reading( $nodeid );
132 my $main_reading = $collation->reading( $ed->{$TRANSKEY} );
133 if( $collation->linear ) {
134 $collation->add_relationship( $tn_reading, $main_reading,
135 { type => 'transposition' } );
137 $collation->merge_readings( $main_reading, $tn_reading );
139 } # else we don't have any other tags to process yet.
142 # Find the beginning and end nodes of the graph. The beginning node
143 # has no incoming edges; the end node has no outgoing edges.
144 my( $begin_node, $end_node );
145 my @starts = $collation->sequence->source_vertices();
146 my @ends = $collation->sequence->sink_vertices();
148 warn "Found more or less than one start vertex: @starts";
150 $collation->merge_readings( $collation->start, @starts );
153 warn "Found more or less than one end vertex: @ends";
155 $collation->merge_readings( $collation->end, @ends );
159 $collation->calculate_ranks() if $collation->linear;
161 # Save the text for each witness so that we can ensure consistency
163 $tradition->collation->text_from_paths();
170 =item * Make this into a stream parser with GraphML
172 =item * Use CollateX-calculated ranks instead of recalculating our own
178 This package is free software and is provided "as is" without express
179 or implied warranty. You can redistribute it and/or modify it under
180 the same terms as Perl itself.
184 Tara L Andrews, aurum@cpan.org