Current state of transposition parsing
[scpubgit/stemmatology.git] / base / lib / Text / Tradition / Parser / CTE.pm
CommitLineData
6f4946fb 1package Text::Tradition::Parser::CTE;
2
3use strict;
4use warnings;
a188b944 5use feature 'say';
c9158e60 6use Encode qw/ decode /;
00311328 7use Text::Tradition::Error;
4d85a60e 8use Text::Tradition::Parser::Util qw/ collate_variants /;
6f4946fb 9use XML::LibXML;
10use XML::LibXML::XPathContext;
11
12=head1 NAME
13
14Text::Tradition::Parser::CTE
15
16=head1 DESCRIPTION
17
18Parser module for Text::Tradition, given a TEI file exported from
19Classical Text Editor.
20
21=head1 METHODS
22
a445ce40 23=head2 parse
6f4946fb 24
25my @apparatus = read( $xml_file );
26
27Takes a Tradition object and a TEI file exported from Classical Text
4d85a60e 28Editor using double-endpoint-attachment critical apparatus encoding;
29initializes the Tradition from the file.
6f4946fb 30
31=cut
32
4d85a60e 33my %sigil_for; # Save the XML IDs for witnesses.
34my %apps; # Save the apparatus XML for a given ID.
f6e19c7c 35my %has_ac; # Keep track of witnesses that have corrections.
6f4946fb 36
37sub parse {
dfc37e38 38 my( $tradition, $opts ) = @_;
4d85a60e 39 my $c = $tradition->collation; # Some shorthand
40
e9442e1c 41 ## DEBUG/TEST
42 $opts->{interpret_transposition} = 1;
43
4d85a60e 44 # First, parse the XML.
c9158e60 45 my( $tei, $xpc ) = _remove_formatting( $opts );
46 return unless $tei; # we have already warned.
4d85a60e 47
48 # CTE uses a DTD rather than any xmlns-based parsing. Thus we
49 # need no namespace handling.
4d85a60e 50 # Get the witnesses and create the witness objects.
51 foreach my $wit_el ( $xpc->findnodes( '//sourceDesc/listWit/witness' ) ) {
52 # The witness xml:id is used internally, and is *not* the sigil name.
53 my $id= $wit_el->getAttribute( 'xml:id' );
92de40a6 54 # If the witness element has an abbr element, that is the sigil. Otherwise
55 # the whole thing is the sigil.
56 my $sig = $xpc->findvalue( 'abbr', $wit_el );
57 my $identifier = 'CTE witness';
58 if( $sig ) {
59 # The sigil is what is in the <abbr/> tag; the identifier is anything
60 # that follows.
61 $identifier = _tidy_identifier(
62 $xpc->findvalue( 'child::text()', $wit_el ) );
63 } else {
64 my @sig_parts = $xpc->findnodes( 'descendant::text()', $wit_el );
65 $sig = _stringify_sigil( @sig_parts );
66 }
67 say STDERR "Adding witness $sig ($identifier)";
68 $tradition->add_witness( sigil => $sig, identifier => $identifier,
69 sourcetype => 'collation' );
b8f262e8 70 $sigil_for{'#'.$id} = $sig; # Make life easy by keying on the ID ref syntax
4d85a60e 71 }
c9158e60 72
4d85a60e 73 # Now go through the text and find the base tokens, apparatus tags, and
74 # anchors. Make a giant array of all of these things in sequence.
75 # TODO consider combining this with creation of graph below
76 my @base_text;
77 foreach my $pg_el ( $xpc->findnodes( '/TEI/text/body/p' ) ) {
78 foreach my $xn ( $pg_el->childNodes ) {
82a45078 79 push( @base_text, _get_base( $xn ) );
4d85a60e 80 }
6f4946fb 81 }
4d85a60e 82 # We now have to work through this array applying the alternate
83 # apparatus readings to the base text. Essentially we will put
84 # everything on the graph, from which we will delete the apps and
85 # anchors when we are done.
f6e19c7c 86
87 # First, put the base tokens, apps, and anchors in the graph.
4d85a60e 88 my $counter = 0;
89 my $last = $c->start;
90 foreach my $item ( @base_text ) {
91 my $r;
92 if( $item->{'type'} eq 'token' ) {
12720144 93 $r = $c->add_reading( { id => 'n'.$counter++,
94 text => $item->{'content'} } );
4d85a60e 95 } elsif ( $item->{'type'} eq 'anchor' ) {
10e4b1ac 96 $r = $c->add_reading( { id => '__ANCHOR_' . $item->{'content'} . '__',
12720144 97 is_ph => 1 } );
4d85a60e 98 } elsif ( $item->{'type'} eq 'app' ) {
10e4b1ac 99 my $tag = '__APP_' . $counter++ . '__';
12720144 100 $r = $c->add_reading( { id => $tag, is_ph => 1 } );
4d85a60e 101 $apps{$tag} = $item->{'content'};
102 }
f6e19c7c 103 $c->add_path( $last, $r, $c->baselabel );
4d85a60e 104 $last = $r;
6f4946fb 105 }
f6e19c7c 106 $c->add_path( $last, $c->end, $c->baselabel );
4d85a60e 107
108 # Now we can parse the apparatus entries, and add the variant readings
109 # to the graph.
4d85a60e 110 foreach my $app_id ( keys %apps ) {
e9442e1c 111 _add_readings( $c, $app_id, $opts );
6f4946fb 112 }
4d85a60e 113
f6e19c7c 114 # Finally, add explicit witness paths, remove the base paths, and remove
115 # the app/anchor tags.
a445ce40 116 _expand_all_paths( $c );
861c3e27 117
118 # Save the text for each witness so that we can ensure consistency
119 # later on
a188b944 120 unless( $opts->{'nocalc'} ) {
121 $tradition->collation->text_from_paths();
122 $tradition->collation->calculate_ranks();
123 $tradition->collation->flatten_ranks();
124 }
6f4946fb 125}
126
4d85a60e 127sub _stringify_sigil {
128 my( @nodes ) = @_;
129 my @parts = grep { /\w/ } map { $_->data } @nodes;
222d58f1 130 my $whole = join( '', @parts );
131 $whole =~ s/\W//g;
132 return $whole;
4d85a60e 133}
6f4946fb 134
92de40a6 135sub _tidy_identifier {
136 my( $str ) = @_;
137 $str =~ s/^\W+//;
138 return $str;
139}
140
c9158e60 141# Get rid of all the formatting elements that get in the way of tokenization.
142sub _remove_formatting {
143 my( $opts ) = @_;
144
145 # First, parse the original XML
146 my $parser = XML::LibXML->new();
147 my $doc;
148 if( exists $opts->{'string'} ) {
149 $doc = $parser->parse_string( $opts->{'string'} );
150 } elsif ( exists $opts->{'file'} ) {
151 $doc = $parser->parse_file( $opts->{'file'} );
152 } else {
153 warn "Could not find string or file option to parse";
154 return;
155 }
00311328 156
c9158e60 157 # Second, remove the formatting
158 my $xpc = XML::LibXML::XPathContext->new( $doc->documentElement );
159 my @useless = $xpc->findnodes( '//hi' );
160 foreach my $n ( @useless ) {
161 my $parent = $n->parentNode();
162 my @children = $n->childNodes();
163 my $first = shift @children;
7c2ed85e 164 if( $first ) {
165 $parent->replaceChild( $first, $n );
166 foreach my $c ( @children ) {
167 $parent->insertAfter( $c, $first );
168 $first = $c;
169 }
170 } else {
171 $parent->removeChild( $n );
c9158e60 172 }
173 }
174
175 # Third, write out and reparse to merge the text nodes.
00311328 176 my $enc = $doc->encoding || 'UTF-8';
177 my $result = decode( $enc, $doc->toString() );
c9158e60 178 my $tei = $parser->parse_string( $result )->documentElement;
00311328 179 unless( $tei->nodeName =~ /^tei(corpus)?$/i ) {
180 throw( "Parsed document has non-TEI root element " . $tei->nodeName );
181 }
c9158e60 182 $xpc = XML::LibXML::XPathContext->new( $tei );
183 return( $tei, $xpc );
184}
185
186## Helper function to help us navigate through nested XML, picking out
187## the words, the apparatus, and the anchors.
4d85a60e 188
189sub _get_base {
190 my( $xn ) = @_;
191 my @readings;
192 if( $xn->nodeType == XML_TEXT_NODE ) {
193 # Base text, just split the words on whitespace and add them
194 # to our sequence.
195 my $str = $xn->data;
196 $str =~ s/^\s+//;
c9158e60 197 my @tokens = split( /\s+/, $str );
92de40a6 198 push( @readings, map { { type => 'token', content => $_ } } @tokens );
4d85a60e 199 } elsif( $xn->nodeName eq 'app' ) {
200 # Apparatus, just save the entire XML node.
92de40a6 201 push( @readings, { type => 'app', content => $xn } );
4d85a60e 202 } elsif( $xn->nodeName eq 'anchor' ) {
203 # Anchor to mark the end of some apparatus; save its ID.
82a45078 204 if( $xn->hasAttribute('xml:id') ) {
92de40a6 205 push( @readings, { type => 'anchor',
206 content => $xn->getAttribute( 'xml:id' ) } );
82a45078 207 } # if the anchor has no XML ID, it is not relevant to us.
92de40a6 208 } elsif( $xn->nodeName =~ /^wit(Start|End)$/ ){
209 push( @readings, { type => 'token', content => '#' . uc( $1 ) . '#' } );
210 } elsif( $xn->nodeName !~ /^(note|seg|milestone|emph)$/ ) { # Any tag we don't know to disregard
a188b944 211 say STDERR "Unrecognized tag " . $xn->nodeName;
6f4946fb 212 }
4d85a60e 213 return @readings;
6f4946fb 214}
215
c9158e60 216sub _append_tokens {
217 my( $list, @tokens ) = @_;
218 if( @$list && $list->[-1]->{'content'} =~ /\#JOIN\#$/ ) {
219 # The list evidently ended mid-word; join the next token onto it.
220 my $t = shift @tokens;
221 if( ref $t && $t->{'type'} eq 'token' ) {
222 # Join the word
223 $t = $t->{'content'};
224 } elsif( ref $t ) {
225 # An app or anchor intervened; end the word.
226 unshift( @tokens, $t );
227 $t = '';
228 }
229 $list->[-1]->{'content'} =~ s/\#JOIN\#$/$t/;
230 }
231 foreach my $t ( @tokens ) {
232 unless( ref( $t ) ) {
233 $t = { 'type' => 'token', 'content' => $t };
234 }
235 push( @$list, $t );
236 }
237}
238
4d85a60e 239sub _add_readings {
e9442e1c 240 my( $c, $app_id, $opts ) = @_;
4d85a60e 241 my $xn = $apps{$app_id};
92de40a6 242 # If the app is of type a1, it is an apparatus criticus.
243 # If it is of type a2, it is an apparatus codicum and might not
244 # have an anchor.
245 my $anchor;
246 if( $xn->hasAttribute('to') ) {
247 $anchor = _anchor_name( $xn->getAttribute( 'to' ) );
248 }
249
4d85a60e 250 # Get the lemma, which is all the readings between app and anchor,
251 # excluding other apps or anchors.
92de40a6 252 my @lemma;
253 my $lemma_str = '';
254 if( $anchor ) {
255 @lemma = _return_lemma( $c, $app_id, $anchor );
256 $lemma_str = join( ' ', map { $_->text } grep { !$_->is_ph } @lemma );
257 }
258
4d85a60e 259 # For each reading, send its text to 'interpret' along with the lemma,
260 # and then save the list of witnesses that these tokens belong to.
3a5d151b 261 my %wit_rdgs; # Maps from witnesses to the variant text
4d85a60e 262 my $ctr = 0;
263 my $tag = $app_id;
10e4b1ac 264 $tag =~ s/^\__APP_(.*)\__$/$1/;
c9158e60 265
4d85a60e 266 foreach my $rdg ( $xn->getChildrenByTagName( 'rdg' ) ) {
e9442e1c 267 my @witlist = split( /\s+/, $rdg->getAttribute( 'wit' ) );
4d85a60e 268 my @text;
4d85a60e 269 foreach ( $rdg->childNodes ) {
270 push( @text, _get_base( $_ ) );
271 }
12720144 272 my( $interpreted, $flag ) = ( '', undef );
273 if( @text ) {
274 ( $interpreted, $flag ) = interpret(
e9442e1c 275 join( ' ', map { $_->{'content'} } @text ), $lemma_str, $anchor, $opts );
12720144 276 }
e9442e1c 277 next if( $interpreted eq $lemma_str ) && !keys %$flag; # Reading is lemma.
b8f262e8 278
4d85a60e 279 my @rdg_nodes;
e9442e1c 280 my @transp_nodes;
b8f262e8 281 if( $interpreted eq '#LACUNA#' ) {
10e4b1ac 282 push( @rdg_nodes, $c->add_reading( { id => 'r'.$tag.".".$ctr++,
b8f262e8 283 is_lacuna => 1 } ) );
e9442e1c 284 } elsif( $flag->{'TR'} ) {
285 # Our reading is transposed to after the given string. Look
286 # down the collation base text and try to find it.
287 # The @rdg_nodes should remain blank here, so that the correct
288 # omission goes into the graph.
289 foreach my $w ( split( /\s+/, $interpreted ) ) {
290 my $r = $c->add_reading( { id => 'r'.$tag.".".$ctr++,
291 text => $w } );
292 push( @transp_nodes, $r );
293 }
294 if( $anchor && @lemma ) {
295 _attach_transposition( $c, \@lemma, $anchor, \@transp_nodes,
296 \@witlist, $flag->{'TR'} );
297 }
b8f262e8 298 } else {
e9442e1c 299 if ( $flag->{'START'}
92de40a6 300 && $c->prior_reading( $app_id, $c->baselabel ) ne $c->start ) {
301 # Add a lacuna for the witness start.
302 push( @rdg_nodes, $c->add_reading( { id => 'r'.$tag.".".$ctr++,
303 is_lacuna => 1 } ) );
92de40a6 304 }
b8f262e8 305 foreach my $w ( split( /\s+/, $interpreted ) ) {
10e4b1ac 306 my $r = $c->add_reading( { id => 'r'.$tag.".".$ctr++,
b8f262e8 307 text => $w } );
308 push( @rdg_nodes, $r );
309 }
e9442e1c 310 if( $flag->{'END'}
92de40a6 311 && $c->next_reading( $app_id, $c->baselabel ) ne $c->end ) {
312 # Add a lacuna for the witness end.
313 push( @rdg_nodes, $c->add_reading( { id => 'r'.$tag.".".$ctr++,
314 is_lacuna => 1 } ) );
92de40a6 315 }
4d85a60e 316 }
92de40a6 317
f6e19c7c 318 # For each listed wit, save the reading.
e9442e1c 319 foreach my $wit ( @witlist ) {
320 $wit .= '_ac' if $flag->{'AC'};
f6e19c7c 321 $wit_rdgs{$wit} = \@rdg_nodes;
322 }
12720144 323
3a5d151b 324 # Does the reading have an ID? If so it probably has a witDetail
f6e19c7c 325 # attached, and we need to read it.
3a5d151b 326 if( $rdg->hasAttribute( 'xml:id' ) ) {
12720144 327 warn "Witdetail on meta reading" if $flag; # this could get complicated.
f6e19c7c 328 my $rid = $rdg->getAttribute( 'xml:id' );
329 my $xpc = XML::LibXML::XPathContext->new( $xn );
330 my @details = $xpc->findnodes( './witDetail[@target="'.$rid.'"]' );
331 foreach my $d ( @details ) {
332 _parse_wit_detail( $d, \%wit_rdgs, \@lemma );
333 }
3a5d151b 334 }
f6e19c7c 335 }
336
4d85a60e 337 # Now collate the variant readings, since it is not done for us.
12720144 338 collate_variants( $c, \@lemma, values %wit_rdgs );
b8f262e8 339
92de40a6 340 # Now add the witness paths for each reading. If we don't have an anchor
341 # (e.g. with an initial witStart) there was no witness path to speak of.
342 if( $anchor ) {
343 my $aclabel = $c->ac_label;
344 foreach my $wit_id ( keys %wit_rdgs ) {
345 my $witstr = _get_sigil( $wit_id, $aclabel );
346 my $rdg_list = $wit_rdgs{$wit_id};
347 _add_wit_path( $c, $rdg_list, $app_id, $anchor, $witstr );
348 }
349 }
4d85a60e 350}
6f4946fb 351
4d85a60e 352sub _anchor_name {
353 my $xmlid = shift;
354 $xmlid =~ s/^\#//;
10e4b1ac 355 return sprintf( "__ANCHOR_%s__", $xmlid );
6f4946fb 356}
357
4d85a60e 358sub _return_lemma {
359 my( $c, $app, $anchor ) = @_;
10e4b1ac 360 my @nodes = grep { $_->id !~ /^__A(PP|NCHOR)/ }
12720144 361 $c->reading_sequence( $c->reading( $app ), $c->reading( $anchor ),
362 $c->baselabel );
4d85a60e 363 return @nodes;
364}
6f4946fb 365
e9442e1c 366# Make a best-effort attempt to attach a transposition farther down the line.
367# $lemmaseq contains the Reading objects of the lemma
368# $anchor contains the point at which we should start scanning for a match
369# $rdgseq contains the Reading objects of the transposed reading
370# (should be identical to the lemma)
371# $witlist contains the list of applicable witnesses
372# $reftxt contains the text to match, after which the $rdgseq should go.
373sub _attach_transposition {
374 my( $c, $lemmaseq, $anchor, $rdgseq, $witlist, $reftxt ) = @_;
375 my @refwords = split( /\s+/, $reftxt );
376 my $checked = $c->reading( $anchor );
377 my $found;
378 while( $checked ne $c->end && !$found ) {
379 my $next = $c->next_reading( $checked, $c->baselabel );
380 if( $next->text eq $refwords[0] ) {
381 # See if the entire sequence of words matches.
382 $found = $next;
383 foreach my $w ( 1..$#refwords ) {
384 $found = $c->next_reading( $next, $c->baselabel );
385 unless( $found->text eq $refwords[$w] ) {
386 $found = undef;
387 last;
388 }
389 }
390 }
391 $checked = $next;
392 }
393 if( $found ) {
394 # The $found variable should now contain the reading after which we
395 # should stick the transposition.
396 my $fnext = $c->next_reading( $found, $c->baselabel );
397 my $aclabel = $c->ac_label;
398 foreach my $wit_id ( @$witlist ) {
399 my $witstr = _get_sigil( $wit_id, $aclabel );
400 _add_wit_path( $c, $rdgseq, $found->id, $fnext->id, $witstr );
401 }
402 # ...and add the transposition relationship between lemma and rdgseq.
403 if( @$lemmaseq == @$rdgseq ) {
404 foreach my $i ( 0..$#{$lemmaseq} ) {
405 $c->add_relationship( $lemmaseq->[$i], $rdgseq->[$i],
406 { type => 'transposition', annotation => 'Detected by CTE' } );
407 }
408 } else {
409 say STDERR "ERROR: lemma and transposed sequence different lengths?!"
410 }
411 } else {
412 say STDERR "WARNING: Unable to find $reftxt in base text for transposition";
413 map { $c->del_reading( $_ ) } @$rdgseq;
414 }
415}
416
a445ce40 417=head2 interpret( $reading, $lemma )
418
419Given a string in $reading and a corresponding lemma in $lemma, interpret what
420the actual reading should be. Used to deal with apparatus-ese shorthands for
421marking transpositions, prefixed or suffixed words, and the like.
422
423=cut
424
6f4946fb 425sub interpret {
4d85a60e 426 # A utility function to change apparatus-ese into a full variant.
e9442e1c 427 my( $reading, $lemma, $anchor, $opts ) = @_;
4d85a60e 428 return $reading if $reading eq $lemma;
429 my $oldreading = $reading;
430 # $lemma =~ s/\s+[[:punct:]]+$//;
e9442e1c 431 my $flag = {}; # To pass back extra info about the interpretation
4d85a60e 432 my @words = split( /\s+/, $lemma );
b2edc51f 433 $reading =~ s/[[:punct:]]?\bsic\b([[:punct:]]+)?//g;
4d85a60e 434 if( $reading =~ /^(.*) praem.$/ ) {
435 $reading = "$1 $lemma";
436 } elsif( $reading =~ /^(.*) add.$/ ) {
437 $reading = "$lemma $1";
b8f262e8 438 } elsif( $reading =~ /add. alia manu/
439 || $reading =~ /inscriptionem compegi e/ # TODO huh?
440 || $reading eq 'inc.' # TODO huh?
441 ) {
12720144 442 # Ignore it.
443 $reading = $lemma;
b8f262e8 444 } elsif( $reading =~ /locus [uv]acuus/
445 || $reading eq 'def.'
c9158e60 446 || $reading eq 'illeg.'
447 || $reading eq 'onleesbar'
f6e19c7c 448 ) {
b8f262e8 449 $reading = '#LACUNA#';
450 } elsif( $reading eq 'om.' ) {
4d85a60e 451 $reading = '';
c9158e60 452 } elsif( $reading =~ /^in[uv]\.$/
453 || $reading eq 'transp.' ) {
4d85a60e 454 # Hope it is two words.
a188b944 455 say STDERR "WARNING: want to invert a lemma that is not two words"
4d85a60e 456 unless scalar( @words ) == 2;
457 $reading = join( ' ', reverse( @words ) );
12720144 458 } elsif( $reading =~ /^iter(\.|at)$/ ) {
4d85a60e 459 # Repeat the lemma
460 $reading = "$lemma $lemma";
12720144 461 } elsif( $reading eq 'in marg.' ) {
462 # There was nothing before a correction.
463 $reading = '';
e9442e1c 464 $flag->{'AC'} = 1;
465 } elsif( $reading =~ /^(.*?)\s*\(?sic([\s\w!.]+)?\)?$/ ) {
466 # Discard any 'sic' notation; indeed, indeed.
467 $reading = $1;
468 if( $reading =~ /^(\W+)$/ ) {
469 # Nothing left but punctuation, so effectively it's the lemma.
470 $reading = $lemma;
471 }
12720144 472 } elsif( $reading =~ /^(.*) \.\.\. (.*)$/ ) {
4d85a60e 473 # The first and last N words captured should replace the first and
474 # last N words of the lemma.
475 my @begin = split( /\s+/, $1 );
476 my @end = split( /\s+/, $2 );
477 if( scalar( @begin ) + scalar ( @end ) > scalar( @words ) ) {
478 # Something is wrong and we can't do the splice.
a188b944 479 say STDERR "ERROR: $lemma is too short to accommodate $oldreading";
4d85a60e 480 } else {
481 splice( @words, 0, scalar @begin, @begin );
482 splice( @words, -(scalar @end), scalar @end, @end );
483 $reading = join( ' ', @words );
484 }
e9442e1c 485 } elsif( $opts->{interpret_transposition} &&
486 $reading =~ /^\s*post\s*(.*?)\s*(tr(ans(p)?)?)?\.?\s*$/ ) {
487 # Try to deal with transposed readings
488 ## DEBUG
489 say STDERR "Will attempt transposition: $reading at $anchor";
490 $reading = $lemma;
491 $flag->{'TR'} = $1;
492 # Look for processed witStart and witEnd tags
92de40a6 493 } elsif( $reading =~ /^\#START\#\s*(.*)$/ ) {
494 $reading = $1;
e9442e1c 495 $flag->{'START'} = 1;
92de40a6 496 } elsif( $reading =~ /^(.*?)\s*\#END\#$/ ) {
497 $reading = $1;
e9442e1c 498 $flag->{'END'} = 1;
12720144 499 }
500 return( $reading, $flag );
4d85a60e 501}
502
f6e19c7c 503sub _parse_wit_detail {
504 my( $detail, $readings, $lemma ) = @_;
505 my $wit = $detail->getAttribute( 'wit' );
506 my $content = $detail->textContent;
4ed2b212 507 if( $content =~ /a\.\s*c\b/ ) {
f6e19c7c 508 # Replace the key in the $readings hash
509 my $rdg = delete $readings->{$wit};
510 $readings->{$wit.'_ac'} = $rdg;
511 $has_ac{$sigil_for{$wit}} = 1;
4ed2b212 512 } elsif( $content =~ /p\.\s*c\b/ ) {
f6e19c7c 513 # If no key for the wit a.c. exists, add one pointing to the lemma
514 unless( exists $readings->{$wit.'_ac'} ) {
515 $readings->{$wit.'_ac'} = $lemma;
516 }
517 $has_ac{$sigil_for{$wit}} = 1;
518 } # else don't bother just yet
519}
520
a445ce40 521sub _get_sigil {
522 my( $xml_id, $layerlabel ) = @_;
f6e19c7c 523 if( $xml_id =~ /^(.*)_ac$/ ) {
524 my $real_id = $1;
a445ce40 525 return $sigil_for{$real_id} . $layerlabel;
f6e19c7c 526 } else {
527 return $sigil_for{$xml_id};
528 }
529}
530
a445ce40 531sub _expand_all_paths {
f6e19c7c 532 my( $c ) = @_;
533
534 # Walk the collation and fish out the paths for each witness
535 foreach my $wit ( $c->tradition->witnesses ) {
536 my $sig = $wit->sigil;
12720144 537 my @path = grep { !$_->is_ph }
f6e19c7c 538 $c->reading_sequence( $c->start, $c->end, $sig );
539 $wit->path( \@path );
540 if( $has_ac{$sig} ) {
12720144 541 my @ac_path = grep { !$_->is_ph }
861c3e27 542 $c->reading_sequence( $c->start, $c->end, $sig.$c->ac_label );
f6e19c7c 543 $wit->uncorrected_path( \@ac_path );
544 }
545 }
546
547 # Delete the anchors
12720144 548 foreach my $anchor ( grep { $_->is_ph } $c->readings ) {
f6e19c7c 549 $c->del_reading( $anchor );
550 }
12720144 551 # Delete the base edges
552 map { $c->del_path( $_, $c->baselabel ) } $c->paths;
f6e19c7c 553
554 # Make the path edges
555 $c->make_witness_paths();
7c2ed85e 556
557 # Now remove any orphan nodes, and warn that we are doing so.
82a45078 558 while( $c->sequence->predecessorless_vertices > 1 ) {
559 foreach my $v ( $c->sequence->predecessorless_vertices ) {
560 my $r = $c->reading( $v );
561 next if $r->is_start;
562 say STDERR "Deleting orphan reading $r / " . $r->text;
563 $c->del_reading( $r );
564 }
7c2ed85e 565 }
4d85a60e 566}
567
568sub _add_wit_path {
f6e19c7c 569 my( $c, $rdg, $app, $anchor, $wit ) = @_;
4d85a60e 570 my @nodes = @$rdg;
f6e19c7c 571 push( @nodes, $c->reading( $anchor ) );
4d85a60e 572
f6e19c7c 573 my $cur = $c->reading( $app );
4d85a60e 574 foreach my $n ( @nodes ) {
f6e19c7c 575 $c->add_path( $cur, $n, $wit );
4d85a60e 576 $cur = $n;
6f4946fb 577 }
6f4946fb 578}
579
00311328 580sub throw {
581 Text::Tradition::Error->throw(
582 'ident' => 'Parser::CTE error',
583 'message' => $_[0],
584 );
585}
586
6f4946fb 587=head1 LICENSE
588
589This package is free software and is provided "as is" without express
590or implied warranty. You can redistribute it and/or modify it under
591the same terms as Perl itself.
592
593=head1 AUTHOR
594
595Tara L Andrews, aurum@cpan.org
596
597=cut
598
5991;
600