More improvements to apparatus interpretation for CTE texts
[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;
b8f262e8 280 if( $interpreted eq '#LACUNA#' ) {
10e4b1ac 281 push( @rdg_nodes, $c->add_reading( { id => 'r'.$tag.".".$ctr++,
b8f262e8 282 is_lacuna => 1 } ) );
e9442e1c 283 } elsif( $flag->{'TR'} ) {
284 # Our reading is transposed to after the given string. Look
285 # down the collation base text and try to find it.
286 # The @rdg_nodes should remain blank here, so that the correct
287 # omission goes into the graph.
f9ffe014 288 my @transp_nodes;
e9442e1c 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 ) {
f9ffe014 295 my $success = _attach_transposition( $c, \@lemma, $anchor,
296 \@transp_nodes, \@witlist, $flag->{'TR'} );
297 unless( $success ) {
298 # If we didn't manage to insert the displaced reading,
299 # then restore it here rather than silently deleting it.
300 push( @rdg_nodes, @transp_nodes );
301 }
e9442e1c 302 }
b8f262e8 303 } else {
e9442e1c 304 if ( $flag->{'START'}
92de40a6 305 && $c->prior_reading( $app_id, $c->baselabel ) ne $c->start ) {
306 # Add a lacuna for the witness start.
307 push( @rdg_nodes, $c->add_reading( { id => 'r'.$tag.".".$ctr++,
308 is_lacuna => 1 } ) );
92de40a6 309 }
b8f262e8 310 foreach my $w ( split( /\s+/, $interpreted ) ) {
10e4b1ac 311 my $r = $c->add_reading( { id => 'r'.$tag.".".$ctr++,
b8f262e8 312 text => $w } );
313 push( @rdg_nodes, $r );
314 }
e9442e1c 315 if( $flag->{'END'}
92de40a6 316 && $c->next_reading( $app_id, $c->baselabel ) ne $c->end ) {
317 # Add a lacuna for the witness end.
318 push( @rdg_nodes, $c->add_reading( { id => 'r'.$tag.".".$ctr++,
319 is_lacuna => 1 } ) );
92de40a6 320 }
4d85a60e 321 }
92de40a6 322
f6e19c7c 323 # For each listed wit, save the reading.
a5978ac8 324 # If an A.C. or P.C. reading is implied rather than explicitly noted,
325 # this is where it will be dealt with.
e9442e1c 326 foreach my $wit ( @witlist ) {
327 $wit .= '_ac' if $flag->{'AC'};
f6e19c7c 328 $wit_rdgs{$wit} = \@rdg_nodes;
a5978ac8 329 # If the PC flag is set, there is a corresponding AC that
330 # follows the lemma and has to be explicitly declared.
331 if( $flag->{'PC'} ) {
332 $wit_rdgs{$wit.'_ac'} = \@lemma;
333 }
f6e19c7c 334 }
12720144 335
3a5d151b 336 # Does the reading have an ID? If so it probably has a witDetail
a5978ac8 337 # attached, and we need to read it. If an A.C. or P.C. reading is
338 # declared explicity, this is where it will be dealt with.
3a5d151b 339 if( $rdg->hasAttribute( 'xml:id' ) ) {
12720144 340 warn "Witdetail on meta reading" if $flag; # this could get complicated.
f6e19c7c 341 my $rid = $rdg->getAttribute( 'xml:id' );
342 my $xpc = XML::LibXML::XPathContext->new( $xn );
343 my @details = $xpc->findnodes( './witDetail[@target="'.$rid.'"]' );
344 foreach my $d ( @details ) {
345 _parse_wit_detail( $d, \%wit_rdgs, \@lemma );
346 }
3a5d151b 347 }
f6e19c7c 348 }
349
4d85a60e 350 # Now collate the variant readings, since it is not done for us.
12720144 351 collate_variants( $c, \@lemma, values %wit_rdgs );
b8f262e8 352
92de40a6 353 # Now add the witness paths for each reading. If we don't have an anchor
354 # (e.g. with an initial witStart) there was no witness path to speak of.
355 if( $anchor ) {
356 my $aclabel = $c->ac_label;
357 foreach my $wit_id ( keys %wit_rdgs ) {
358 my $witstr = _get_sigil( $wit_id, $aclabel );
359 my $rdg_list = $wit_rdgs{$wit_id};
360 _add_wit_path( $c, $rdg_list, $app_id, $anchor, $witstr );
361 }
362 }
4d85a60e 363}
6f4946fb 364
4d85a60e 365sub _anchor_name {
366 my $xmlid = shift;
367 $xmlid =~ s/^\#//;
10e4b1ac 368 return sprintf( "__ANCHOR_%s__", $xmlid );
6f4946fb 369}
370
4d85a60e 371sub _return_lemma {
372 my( $c, $app, $anchor ) = @_;
10e4b1ac 373 my @nodes = grep { $_->id !~ /^__A(PP|NCHOR)/ }
12720144 374 $c->reading_sequence( $c->reading( $app ), $c->reading( $anchor ),
375 $c->baselabel );
4d85a60e 376 return @nodes;
377}
6f4946fb 378
e9442e1c 379# Make a best-effort attempt to attach a transposition farther down the line.
380# $lemmaseq contains the Reading objects of the lemma
381# $anchor contains the point at which we should start scanning for a match
382# $rdgseq contains the Reading objects of the transposed reading
383# (should be identical to the lemma)
384# $witlist contains the list of applicable witnesses
385# $reftxt contains the text to match, after which the $rdgseq should go.
386sub _attach_transposition {
387 my( $c, $lemmaseq, $anchor, $rdgseq, $witlist, $reftxt ) = @_;
388 my @refwords = split( /\s+/, $reftxt );
389 my $checked = $c->reading( $anchor );
390 my $found;
f9ffe014 391 my $success;
e9442e1c 392 while( $checked ne $c->end && !$found ) {
393 my $next = $c->next_reading( $checked, $c->baselabel );
394 if( $next->text eq $refwords[0] ) {
395 # See if the entire sequence of words matches.
396 $found = $next;
397 foreach my $w ( 1..$#refwords ) {
398 $found = $c->next_reading( $next, $c->baselabel );
399 unless( $found->text eq $refwords[$w] ) {
400 $found = undef;
401 last;
402 }
403 }
404 }
405 $checked = $next;
406 }
407 if( $found ) {
408 # The $found variable should now contain the reading after which we
409 # should stick the transposition.
410 my $fnext = $c->next_reading( $found, $c->baselabel );
411 my $aclabel = $c->ac_label;
412 foreach my $wit_id ( @$witlist ) {
413 my $witstr = _get_sigil( $wit_id, $aclabel );
414 _add_wit_path( $c, $rdgseq, $found->id, $fnext->id, $witstr );
415 }
416 # ...and add the transposition relationship between lemma and rdgseq.
417 if( @$lemmaseq == @$rdgseq ) {
418 foreach my $i ( 0..$#{$lemmaseq} ) {
419 $c->add_relationship( $lemmaseq->[$i], $rdgseq->[$i],
420 { type => 'transposition', annotation => 'Detected by CTE' } );
421 }
f9ffe014 422 $success = 1;
e9442e1c 423 } else {
424 say STDERR "ERROR: lemma and transposed sequence different lengths?!"
425 }
426 } else {
427 say STDERR "WARNING: Unable to find $reftxt in base text for transposition";
e9442e1c 428 }
f9ffe014 429 return $success;
e9442e1c 430}
431
a445ce40 432=head2 interpret( $reading, $lemma )
433
434Given a string in $reading and a corresponding lemma in $lemma, interpret what
435the actual reading should be. Used to deal with apparatus-ese shorthands for
436marking transpositions, prefixed or suffixed words, and the like.
437
438=cut
439
6f4946fb 440sub interpret {
4d85a60e 441 # A utility function to change apparatus-ese into a full variant.
e9442e1c 442 my( $reading, $lemma, $anchor, $opts ) = @_;
4d85a60e 443 return $reading if $reading eq $lemma;
444 my $oldreading = $reading;
445 # $lemma =~ s/\s+[[:punct:]]+$//;
e9442e1c 446 my $flag = {}; # To pass back extra info about the interpretation
4d85a60e 447 my @words = split( /\s+/, $lemma );
f9ffe014 448 # Discard any 'sic' notation - that rather goes without saying.
a5978ac8 449 $reading =~ s/([[:punct:]]+)?sic([[:punct:]]+)?//g;
f9ffe014 450
451 # Now look for common jargon.
452 if( $reading =~ /^(.*) praem.$/ || $reading =~ /^praem\. (.*)$/ ) {
4d85a60e 453 $reading = "$1 $lemma";
f9ffe014 454 } elsif( $reading =~ /^(.*) add.$/ || $reading =~ /^add\. (.*)$/ ) {
4d85a60e 455 $reading = "$lemma $1";
b8f262e8 456 } elsif( $reading =~ /locus [uv]acuus/
457 || $reading eq 'def.'
c9158e60 458 || $reading eq 'illeg.'
a5978ac8 459 || $reading eq 'desunt'
f6e19c7c 460 ) {
b8f262e8 461 $reading = '#LACUNA#';
462 } elsif( $reading eq 'om.' ) {
4d85a60e 463 $reading = '';
c9158e60 464 } elsif( $reading =~ /^in[uv]\.$/
a5978ac8 465 || $reading =~ /^tr(ans(p)?)?\.$/ ) {
4d85a60e 466 # Hope it is two words.
a188b944 467 say STDERR "WARNING: want to invert a lemma that is not two words"
4d85a60e 468 unless scalar( @words ) == 2;
469 $reading = join( ' ', reverse( @words ) );
12720144 470 } elsif( $reading =~ /^iter(\.|at)$/ ) {
4d85a60e 471 # Repeat the lemma
472 $reading = "$lemma $lemma";
a5978ac8 473 } elsif( $reading =~ /^(.*?)\s*\(?in marg\.\)?$/ ) {
474 $reading = $1;
475 if( $reading ) {
476 # The given text is a correction.
477 $flag->{'PC'} = 1;
478 } else {
479 # The lemma itself was the correction; the witness carried
480 # no reading pre-correction.
481 $flag->{'AC'} = 1;
482 }
12720144 483 } elsif( $reading =~ /^(.*) \.\.\. (.*)$/ ) {
4d85a60e 484 # The first and last N words captured should replace the first and
485 # last N words of the lemma.
486 my @begin = split( /\s+/, $1 );
487 my @end = split( /\s+/, $2 );
488 if( scalar( @begin ) + scalar ( @end ) > scalar( @words ) ) {
489 # Something is wrong and we can't do the splice.
a188b944 490 say STDERR "ERROR: $lemma is too short to accommodate $oldreading";
4d85a60e 491 } else {
492 splice( @words, 0, scalar @begin, @begin );
493 splice( @words, -(scalar @end), scalar @end, @end );
494 $reading = join( ' ', @words );
495 }
e9442e1c 496 } elsif( $opts->{interpret_transposition} &&
a5978ac8 497 ( $reading =~ /^post\s*(?<lem>.*?)\s+tr(ans(p)?)?\.$/ ||
498 $reading =~ /^tr(ans(p)?)?\. post\s*(?<lem>.*)$/) ) {
e9442e1c 499 # Try to deal with transposed readings
500 ## DEBUG
501 say STDERR "Will attempt transposition: $reading at $anchor";
502 $reading = $lemma;
a5978ac8 503 $flag->{'TR'} = $+{lem};
e9442e1c 504 # Look for processed witStart and witEnd tags
92de40a6 505 } elsif( $reading =~ /^\#START\#\s*(.*)$/ ) {
506 $reading = $1;
e9442e1c 507 $flag->{'START'} = 1;
92de40a6 508 } elsif( $reading =~ /^(.*?)\s*\#END\#$/ ) {
509 $reading = $1;
e9442e1c 510 $flag->{'END'} = 1;
12720144 511 }
512 return( $reading, $flag );
4d85a60e 513}
514
f6e19c7c 515sub _parse_wit_detail {
516 my( $detail, $readings, $lemma ) = @_;
517 my $wit = $detail->getAttribute( 'wit' );
518 my $content = $detail->textContent;
a5978ac8 519 if( $content =~ /^a\.?\s*c(orr)?\.$/ ) {
f6e19c7c 520 # Replace the key in the $readings hash
521 my $rdg = delete $readings->{$wit};
522 $readings->{$wit.'_ac'} = $rdg;
523 $has_ac{$sigil_for{$wit}} = 1;
a5978ac8 524 } elsif( $content =~ /^p\.?\s*c(orr)?\.$/ || $content =~ /^s\.?\s*l\.$/ ) {
f6e19c7c 525 # If no key for the wit a.c. exists, add one pointing to the lemma
526 unless( exists $readings->{$wit.'_ac'} ) {
527 $readings->{$wit.'_ac'} = $lemma;
528 }
529 $has_ac{$sigil_for{$wit}} = 1;
a5978ac8 530 } else { #...not sure what it is?
531 say STDERR "WARNING: Unrecognized sigil addendum $content";
532 }
f6e19c7c 533}
534
a445ce40 535sub _get_sigil {
536 my( $xml_id, $layerlabel ) = @_;
f6e19c7c 537 if( $xml_id =~ /^(.*)_ac$/ ) {
538 my $real_id = $1;
a445ce40 539 return $sigil_for{$real_id} . $layerlabel;
f6e19c7c 540 } else {
541 return $sigil_for{$xml_id};
542 }
543}
544
a445ce40 545sub _expand_all_paths {
f6e19c7c 546 my( $c ) = @_;
547
548 # Walk the collation and fish out the paths for each witness
549 foreach my $wit ( $c->tradition->witnesses ) {
550 my $sig = $wit->sigil;
12720144 551 my @path = grep { !$_->is_ph }
f6e19c7c 552 $c->reading_sequence( $c->start, $c->end, $sig );
553 $wit->path( \@path );
554 if( $has_ac{$sig} ) {
12720144 555 my @ac_path = grep { !$_->is_ph }
861c3e27 556 $c->reading_sequence( $c->start, $c->end, $sig.$c->ac_label );
f6e19c7c 557 $wit->uncorrected_path( \@ac_path );
558 }
559 }
560
561 # Delete the anchors
12720144 562 foreach my $anchor ( grep { $_->is_ph } $c->readings ) {
f6e19c7c 563 $c->del_reading( $anchor );
564 }
12720144 565 # Delete the base edges
566 map { $c->del_path( $_, $c->baselabel ) } $c->paths;
f6e19c7c 567
568 # Make the path edges
569 $c->make_witness_paths();
7c2ed85e 570
571 # Now remove any orphan nodes, and warn that we are doing so.
82a45078 572 while( $c->sequence->predecessorless_vertices > 1 ) {
573 foreach my $v ( $c->sequence->predecessorless_vertices ) {
574 my $r = $c->reading( $v );
575 next if $r->is_start;
576 say STDERR "Deleting orphan reading $r / " . $r->text;
577 $c->del_reading( $r );
578 }
7c2ed85e 579 }
4d85a60e 580}
581
582sub _add_wit_path {
f6e19c7c 583 my( $c, $rdg, $app, $anchor, $wit ) = @_;
4d85a60e 584 my @nodes = @$rdg;
f6e19c7c 585 push( @nodes, $c->reading( $anchor ) );
4d85a60e 586
f6e19c7c 587 my $cur = $c->reading( $app );
4d85a60e 588 foreach my $n ( @nodes ) {
f6e19c7c 589 $c->add_path( $cur, $n, $wit );
4d85a60e 590 $cur = $n;
6f4946fb 591 }
6f4946fb 592}
593
00311328 594sub throw {
595 Text::Tradition::Error->throw(
596 'ident' => 'Parser::CTE error',
597 'message' => $_[0],
598 );
599}
600
6f4946fb 601=head1 LICENSE
602
603This package is free software and is provided "as is" without express
604or implied warranty. You can redistribute it and/or modify it under
605the same terms as Perl itself.
606
607=head1 AUTHOR
608
609Tara L Andrews, aurum@cpan.org
610
611=cut
612
6131;
614