fix up CTE parser, including an ugly hack I need, with new graph
[scpubgit/stemmatology.git] / lib / Text / Tradition / Parser / CTE.pm
1 package Text::Tradition::Parser::CTE;
2
3 use strict;
4 use warnings;
5 use Text::Tradition::Parser::Util qw/ collate_variants /;
6 use XML::LibXML;
7 use XML::LibXML::XPathContext;
8
9 =head1 NAME
10
11 Text::Tradition::Parser::CTE
12
13 =head1 DESCRIPTION
14
15 Parser module for Text::Tradition, given a TEI file exported from
16 Classical Text Editor.
17
18 =head1 METHODS
19
20 =over
21
22 =item B<parse>
23
24 my @apparatus = read( $xml_file );
25
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.
29
30 =cut
31
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*
37         );
38
39 sub parse {
40         my( $tradition, $opts ) = @_;
41         my $c = $tradition->collation;  # Some shorthand
42         
43         # First, parse the XML.
44         my $parser = XML::LibXML->new();
45     my $doc;
46     if( exists $opts->{'string'} ) {
47         $doc = $parser->parse_string( $opts->{'string'} );
48     } elsif ( exists $opts->{'file'} ) {
49         $doc = $parser->parse_file( $opts->{'file'} );
50     } else {
51         warn "Could not find string or file option to parse";
52         return;
53     }
54         my $tei = $doc->documentElement();
55         my $xpc = XML::LibXML::XPathContext->new( $tei );
56
57         # CTE uses a DTD rather than any xmlns-based parsing.  Thus we
58         # need no namespace handling.
59
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
69                 }
70         }
71
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
75         my @base_text;
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 ) );
79                 }
80         }
81         
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.
86         
87         # First, put the base tokens, apps, and anchors in the graph.
88         my $counter = 0;
89         my $last = $c->start;
90         foreach my $item ( @base_text ) {
91             my $r;
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'} . '#', 
97                                                         is_ph => 1 } );
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'};
102         }
103         $c->add_path( $last, $r, $c->baselabel );
104         $last = $r;
105     }
106     $c->add_path( $last, $c->end, $c->baselabel );
107     
108     # Now we can parse the apparatus entries, and add the variant readings 
109     # to the graph.
110     
111     foreach my $app_id ( keys %apps ) {
112         _add_readings( $c, $app_id );
113     }
114     
115     # Finally, add explicit witness paths, remove the base paths, and remove
116     # the app/anchor tags.
117     expand_all_paths( $c );
118 }
119
120 sub _stringify_sigil {
121     my( @nodes ) = @_;
122     my @parts = grep { /\w/ } map { $_->data } @nodes;
123     return join( '', @parts );
124 }
125
126 ## Recursive little helper function to help us navigate through nested
127 ## XML, picking out the words, the apparatus, and the anchors.
128
129 sub _get_base {
130         my( $xn ) = @_;
131         my @readings;
132         if( $xn->nodeType == XML_TEXT_NODE ) {
133             # Base text, just split the words on whitespace and add them 
134             # to our sequence.
135             # TODO consider that XML markup might appear mid-token.
136                 my $str = $xn->data;
137                 $str =~ s/^\s+//;
138                 foreach my $w ( split( /\s+/, $str ) ) {
139                     # HACK to cope with mismatched doublequotes
140                     $w =~ s/\"//g;
141                         push( @readings, { 'type' => 'token', 'content' => $w } );
142                 }
143         } elsif( $xn->nodeName eq 'hi' ) {
144                 # Recurse as if the hi weren't there.
145                 foreach( $xn->childNodes ) {
146                         push( @readings, _get_base( $_ ) );
147                 }
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";
157         }
158         return @readings;
159 }
160
161 sub _add_readings {
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 );
169     
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
173     my $ctr = 0;
174     my $tag = $app_id;
175     $tag =~ s/^\#APP_(.*)\#$/$1/;
176     foreach my $rdg ( $xn->getChildrenByTagName( 'rdg' ) ) {
177         my @text;
178         foreach ( $rdg->childNodes ) {
179             push( @text, _get_base( $_ ) );
180         }
181         my( $interpreted, $flag ) = ( '', undef );
182         if( @text ) {
183                 ( $interpreted, $flag ) = interpret( 
184                         join( ' ', map { $_->{'content'} } @text ), $lemma_str );
185         }
186         my @rdg_nodes;
187         foreach my $w ( split( /\s+/, $interpreted ) ) {
188             my $r = $c->add_reading( { id => $tag . "/" . $ctr++,
189                                                            text => $w } );
190             push( @rdg_nodes, $r );
191         }
192         
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;
197         }
198                         
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 );
208             }
209         }
210     }       
211         
212     # Now collate the variant readings, since it is not done for us.
213     collate_variants( $c, \@lemma, values %wit_rdgs );
214     
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} ) {
223                                 $DB::single = 1;
224                                         warn "Had reading for individual member $w of group $wit_id at $xn";
225                         }
226                         $wit_rdgs{$w} = $rdg;
227                 }
228                 delete $wit_rdgs{$wit_id};
229         }
230     }
231     
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 );
237     }
238 }
239
240 sub _anchor_name {
241     my $xmlid = shift;
242     $xmlid =~ s/^\#//;
243     return sprintf( "#ANCHOR_%s#", $xmlid );
244 }
245
246 sub _return_lemma {
247     my( $c, $app, $anchor ) = @_;
248     my @nodes = grep { $_->id !~ /^\#A(PP|NCHOR)/ } 
249         $c->reading_sequence( $c->reading( $app ), $c->reading( $anchor ),
250                 $c->baselabel );
251     return @nodes;
252 }
253
254 sub interpret {
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/ ) {
268                 # Ignore it.
269                 $reading = $lemma;
270         } elsif( $reading eq 'om.' 
271             || $reading =~ /locus [uv]acuus/
272             || $reading =~ /inscriptionem compegi e/ # TODO huh?
273             || $reading eq 'def.' # TODO huh?
274             ) {
275                 $reading = '';
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)$/ ) {
282                 # Repeat the lemma
283                 $reading = "$lemma $lemma";
284         } elsif( $reading eq 'in marg.' ) {
285                 # There was nothing before a correction.
286                 $reading = '';
287                 $flag = '_ac';
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";
296                 } else {
297                         splice( @words, 0, scalar @begin, @begin );
298                         splice( @words, -(scalar @end), scalar @end, @end );
299                         $reading = join( ' ', @words );
300                 }
301         }
302         if( $oldreading ne $reading || $flag || $oldreading =~ /\./ ) {
303                 my $int = $reading;
304                 $int .= " ($flag)" if $flag;
305                 print STDERR "Interpreted $oldreading as $int given $lemma\n";
306         }
307         return( $reading, $flag );
308 }
309
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;
323         }
324         $has_ac{$sigil_for{$wit}} = 1;
325     } # else don't bother just yet
326 }
327
328 sub get_sigil {
329     my( $xml_id, $c ) = @_;
330     if( $xml_id =~ /^(.*)_ac$/ ) {
331         my $real_id = $1;
332         return $sigil_for{$real_id} . $c->ac_label;
333     } else {
334         return $sigil_for{$xml_id};
335     }
336 }
337
338 sub expand_all_paths { 
339     my( $c ) = @_;
340     
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 );
351         }
352     }   
353     
354     # Delete the anchors
355     foreach my $anchor ( grep { $_->is_ph } $c->readings ) {
356         $c->del_reading( $anchor );
357     }
358     # Delete the base edges
359     map { $c->del_path( $_, $c->baselabel ) } $c->paths;
360     
361     # Make the path edges
362     $c->make_witness_paths();
363 }
364
365 sub _add_wit_path {
366     my( $c, $rdg, $app, $anchor, $wit ) = @_;
367     my @nodes = @$rdg;
368     push( @nodes, $c->reading( $anchor ) );
369     
370     my $cur = $c->reading( $app );
371     foreach my $n ( @nodes ) {
372         $c->add_path( $cur, $n, $wit );
373         $cur = $n;
374     }
375 }
376
377 =back
378
379 =head1 LICENSE
380
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.
384
385 =head1 AUTHOR
386
387 Tara L Andrews, aurum@cpan.org
388
389 =cut
390
391 1;
392