1 package Text::Tradition::Parser::CTE;
5 use Text::Tradition::Parser::Util qw/ collate_variants /;
7 use XML::LibXML::XPathContext;
11 Text::Tradition::Parser::CTE
15 Parser module for Text::Tradition, given a TEI file exported from
16 Classical Text Editor.
24 my @apparatus = read( $xml_file );
26 Takes a Tradition object and a TEI file exported from Classical Text
27 Editor using double-endpoint-attachment critical apparatus encoding;
28 initializes the Tradition from the file.
32 my %sigil_for; # Save the XML IDs for witnesses.
33 my %apps; # Save the apparatus XML for a given ID.
34 my %has_ac; # Keep track of witnesses that have corrections.
35 my %group_sigla = ( ## HACK HACK HACK
36 '#M38' => [ qw( #M23 #M24 #M25 #M27 #M30 #M26 #M31 #M32 #M33 ) ], # l -> L*
40 my( $tradition, $opts ) = @_;
41 my $c = $tradition->collation; # Some shorthand
43 # First, parse the XML.
44 my $parser = XML::LibXML->new();
46 if( exists $opts->{'string'} ) {
47 $doc = $parser->parse_string( $opts->{'string'} );
48 } elsif ( exists $opts->{'file'} ) {
49 $doc = $parser->parse_file( $opts->{'file'} );
51 warn "Could not find string or file option to parse";
54 my $tei = $doc->documentElement();
55 my $xpc = XML::LibXML::XPathContext->new( $tei );
57 # CTE uses a DTD rather than any xmlns-based parsing. Thus we
58 # need no namespace handling.
60 # Get the witnesses and create the witness objects.
61 foreach my $wit_el ( $xpc->findnodes( '//sourceDesc/listWit/witness' ) ) {
62 # The witness xml:id is used internally, and is *not* the sigil name.
63 my $id= $wit_el->getAttribute( 'xml:id' );
64 my @sig_parts = $xpc->findnodes( './abbr/descendant::text()', $wit_el );
65 my $sig = _stringify_sigil( @sig_parts );
66 unless( exists $group_sigla{'#'.$id} ) { ## More HACKY
67 $tradition->add_witness( sigil => $sig, source => $wit_el->toString() );
68 $sigil_for{'#'.$id} = $sig; # Make life easy by keying on the ID ref syntax
72 # Now go through the text and find the base tokens, apparatus tags, and
73 # anchors. Make a giant array of all of these things in sequence.
74 # TODO consider combining this with creation of graph below
76 foreach my $pg_el ( $xpc->findnodes( '/TEI/text/body/p' ) ) {
77 foreach my $xn ( $pg_el->childNodes ) {
78 push( @base_text, _get_base( $xn ) );
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.
87 # First, put the base tokens, apps, and anchors in the graph.
90 foreach my $item ( @base_text ) {
92 if( $item->{'type'} eq 'token' ) {
93 $r = $c->add_reading( { id => 'n'.$counter++,
94 text => $item->{'content'} } );
95 } elsif ( $item->{'type'} eq 'anchor' ) {
96 $r = $c->add_reading( { id => '#ANCHOR_' . $item->{'content'} . '#',
98 } elsif ( $item->{'type'} eq 'app' ) {
99 my $tag = '#APP_' . $counter++ . '#';
100 $r = $c->add_reading( { id => $tag, is_ph => 1 } );
101 $apps{$tag} = $item->{'content'};
103 $c->add_path( $last, $r, $c->baselabel );
106 $c->add_path( $last, $c->end, $c->baselabel );
108 # Now we can parse the apparatus entries, and add the variant readings
111 foreach my $app_id ( keys %apps ) {
112 _add_readings( $c, $app_id );
115 # Finally, add explicit witness paths, remove the base paths, and remove
116 # the app/anchor tags.
117 expand_all_paths( $c );
120 sub _stringify_sigil {
122 my @parts = grep { /\w/ } map { $_->data } @nodes;
123 return join( '', @parts );
126 ## Recursive little helper function to help us navigate through nested
127 ## XML, picking out the words, the apparatus, and the anchors.
132 if( $xn->nodeType == XML_TEXT_NODE ) {
133 # Base text, just split the words on whitespace and add them
135 # TODO consider that XML markup might appear mid-token.
138 foreach my $w ( split( /\s+/, $str ) ) {
139 # HACK to cope with mismatched doublequotes
141 push( @readings, { 'type' => 'token', 'content' => $w } );
143 } elsif( $xn->nodeName eq 'hi' ) {
144 # Recurse as if the hi weren't there.
145 foreach( $xn->childNodes ) {
146 push( @readings, _get_base( $_ ) );
148 } elsif( $xn->nodeName eq 'app' ) {
149 # Apparatus, just save the entire XML node.
150 push( @readings, { 'type' => 'app', 'content' => $xn } );
151 } elsif( $xn->nodeName eq 'anchor' ) {
152 # Anchor to mark the end of some apparatus; save its ID.
153 push( @readings, { 'type' => 'anchor',
154 'content' => $xn->getAttribute( 'xml:id' ) } );
155 } elsif ( $xn->nodeName ne 'note' ) { # Any tag we don't know to disregard
156 print STDERR "Unrecognized tag " . $xn->nodeName . "\n";
162 my( $c, $app_id ) = @_;
163 my $xn = $apps{$app_id};
164 my $anchor = _anchor_name( $xn->getAttribute( 'to' ) );
165 # Get the lemma, which is all the readings between app and anchor,
166 # excluding other apps or anchors.
167 my @lemma = _return_lemma( $c, $app_id, $anchor );
168 my $lemma_str = join( ' ', grep { $_ !~ /^\#/ } map { $_->text } @lemma );
170 # For each reading, send its text to 'interpret' along with the lemma,
171 # and then save the list of witnesses that these tokens belong to.
172 my %wit_rdgs; # Maps from witnesses to the variant text
175 $tag =~ s/^\#APP_(.*)\#$/$1/;
176 foreach my $rdg ( $xn->getChildrenByTagName( 'rdg' ) ) {
178 foreach ( $rdg->childNodes ) {
179 push( @text, _get_base( $_ ) );
181 my( $interpreted, $flag ) = ( '', undef );
183 ( $interpreted, $flag ) = interpret(
184 join( ' ', map { $_->{'content'} } @text ), $lemma_str );
187 foreach my $w ( split( /\s+/, $interpreted ) ) {
188 my $r = $c->add_reading( { id => $tag . "/" . $ctr++,
190 push( @rdg_nodes, $r );
193 # For each listed wit, save the reading.
194 foreach my $wit ( split( /\s+/, $rdg->getAttribute( 'wit' ) ) ) {
195 $wit .= $flag if $flag;
196 $wit_rdgs{$wit} = \@rdg_nodes;
199 # Does the reading have an ID? If so it probably has a witDetail
200 # attached, and we need to read it.
201 if( $rdg->hasAttribute( 'xml:id' ) ) {
202 warn "Witdetail on meta reading" if $flag; # this could get complicated.
203 my $rid = $rdg->getAttribute( 'xml:id' );
204 my $xpc = XML::LibXML::XPathContext->new( $xn );
205 my @details = $xpc->findnodes( './witDetail[@target="'.$rid.'"]' );
206 foreach my $d ( @details ) {
207 _parse_wit_detail( $d, \%wit_rdgs, \@lemma );
212 # Now collate the variant readings, since it is not done for us.
213 collate_variants( $c, \@lemma, values %wit_rdgs );
215 # HACKY HACKY Expand "group" sigla.
216 # Does not work for nested groups; also does not work with a modifier
217 # on the group sigil.
218 foreach my $wit_id ( keys %group_sigla ) {
219 if ( exists $wit_rdgs{$wit_id} ) {
220 my $rdg = $wit_rdgs{$wit_id};
221 foreach my $w ( @{$group_sigla{$wit_id}} ) {
222 if( exists $wit_rdgs{$w} ) {
224 warn "Had reading for individual member $w of group $wit_id at $xn";
226 $wit_rdgs{$w} = $rdg;
228 delete $wit_rdgs{$wit_id};
232 # Now add the witness paths for each reading.
233 foreach my $wit_id ( keys %wit_rdgs ) {
234 my $witstr = get_sigil( $wit_id, $c );
235 my $rdg_list = $wit_rdgs{$wit_id};
236 _add_wit_path( $c, $rdg_list, $app_id, $anchor, $witstr );
243 return sprintf( "#ANCHOR_%s#", $xmlid );
247 my( $c, $app, $anchor ) = @_;
248 my @nodes = grep { $_->id !~ /^\#A(PP|NCHOR)/ }
249 $c->reading_sequence( $c->reading( $app ), $c->reading( $anchor ),
255 # A utility function to change apparatus-ese into a full variant.
256 my( $reading, $lemma ) = @_;
257 return $reading if $reading eq $lemma;
258 my $oldreading = $reading;
259 # $lemma =~ s/\s+[[:punct:]]+$//;
260 # $reading =~ s/\s*\(?sic([\s\w.]+)?\)?$//;
261 my $flag; # In case of p.c. indications
262 my @words = split( /\s+/, $lemma );
263 if( $reading =~ /^(.*) praem.$/ ) {
264 $reading = "$1 $lemma";
265 } elsif( $reading =~ /^(.*) add.$/ ) {
266 $reading = "$lemma $1";
267 } elsif( $reading =~ /add. alia manu/ ) {
270 } elsif( $reading eq 'om.'
271 || $reading =~ /locus [uv]acuus/
272 || $reading =~ /inscriptionem compegi e/ # TODO huh?
273 || $reading eq 'def.' # TODO huh?
276 } elsif( $reading =~ /^in[uv]\.$/ ) {
277 # Hope it is two words.
278 print STDERR "WARNING: want to invert a lemma that is not two words\n"
279 unless scalar( @words ) == 2;
280 $reading = join( ' ', reverse( @words ) );
281 } elsif( $reading =~ /^iter(\.|at)$/ ) {
283 $reading = "$lemma $lemma";
284 } elsif( $reading eq 'in marg.' ) {
285 # There was nothing before a correction.
288 } elsif( $reading =~ /^(.*) \.\.\. (.*)$/ ) {
289 # The first and last N words captured should replace the first and
290 # last N words of the lemma.
291 my @begin = split( /\s+/, $1 );
292 my @end = split( /\s+/, $2 );
293 if( scalar( @begin ) + scalar ( @end ) > scalar( @words ) ) {
294 # Something is wrong and we can't do the splice.
295 print STDERR "ERROR: $lemma is too short to accommodate $oldreading\n";
297 splice( @words, 0, scalar @begin, @begin );
298 splice( @words, -(scalar @end), scalar @end, @end );
299 $reading = join( ' ', @words );
302 if( $oldreading ne $reading || $flag || $oldreading =~ /\./ ) {
304 $int .= " ($flag)" if $flag;
305 print STDERR "Interpreted $oldreading as $int given $lemma\n";
307 return( $reading, $flag );
310 sub _parse_wit_detail {
311 my( $detail, $readings, $lemma ) = @_;
312 my $wit = $detail->getAttribute( 'wit' );
313 my $content = $detail->textContent;
314 if( $content =~ /a\.\s*c\./ ) {
315 # Replace the key in the $readings hash
316 my $rdg = delete $readings->{$wit};
317 $readings->{$wit.'_ac'} = $rdg;
318 $has_ac{$sigil_for{$wit}} = 1;
319 } elsif( $content =~ /p\.\s*c\./ ) {
320 # If no key for the wit a.c. exists, add one pointing to the lemma
321 unless( exists $readings->{$wit.'_ac'} ) {
322 $readings->{$wit.'_ac'} = $lemma;
324 $has_ac{$sigil_for{$wit}} = 1;
325 } # else don't bother just yet
329 my( $xml_id, $c ) = @_;
330 if( $xml_id =~ /^(.*)_ac$/ ) {
332 return $sigil_for{$real_id} . $c->ac_label;
334 return $sigil_for{$xml_id};
338 sub expand_all_paths {
341 # Walk the collation and fish out the paths for each witness
342 foreach my $wit ( $c->tradition->witnesses ) {
343 my $sig = $wit->sigil;
344 my @path = grep { !$_->is_ph }
345 $c->reading_sequence( $c->start, $c->end, $sig );
346 $wit->path( \@path );
347 if( $has_ac{$sig} ) {
348 my @ac_path = grep { !$_->is_ph }
349 $c->reading_sequence( $c->start, $c->end, $sig.$c->ac_label, $sig );
350 $wit->uncorrected_path( \@ac_path );
355 foreach my $anchor ( grep { $_->is_ph } $c->readings ) {
356 $c->del_reading( $anchor );
358 # Delete the base edges
359 map { $c->del_path( $_, $c->baselabel ) } $c->paths;
361 # Make the path edges
362 $c->make_witness_paths();
366 my( $c, $rdg, $app, $anchor, $wit ) = @_;
368 push( @nodes, $c->reading( $anchor ) );
370 my $cur = $c->reading( $app );
371 foreach my $n ( @nodes ) {
372 $c->add_path( $cur, $n, $wit );
381 This package is free software and is provided "as is" without express
382 or implied warranty. You can redistribute it and/or modify it under
383 the same terms as Perl itself.
387 Tara L Andrews, aurum@cpan.org