Commit | Line | Data |
6f4946fb |
1 | package Text::Tradition::Parser::CTE; |
2 | |
3 | use strict; |
4 | use warnings; |
4d85a60e |
5 | use Text::Tradition::Parser::Util qw/ collate_variants /; |
6f4946fb |
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 |
4d85a60e |
27 | Editor using double-endpoint-attachment critical apparatus encoding; |
28 | initializes the Tradition from the file. |
6f4946fb |
29 | |
30 | =cut |
31 | |
4d85a60e |
32 | my %sigil_for; # Save the XML IDs for witnesses. |
33 | my %apps; # Save the apparatus XML for a given ID. |
f6e19c7c |
34 | my %has_ac; # Keep track of witnesses that have corrections. |
6f4946fb |
35 | |
36 | sub parse { |
dfc37e38 |
37 | my( $tradition, $opts ) = @_; |
4d85a60e |
38 | my $c = $tradition->collation; # Some shorthand |
39 | |
40 | # First, parse the XML. |
41 | my $parser = XML::LibXML->new(); |
dfc37e38 |
42 | my $doc; |
43 | if( exists $opts->{'string'} ) { |
44 | $doc = $parser->parse_string( $opts->{'string'} ); |
45 | } elsif ( exists $opts->{'file'} ) { |
46 | $doc = $parser->parse_file( $opts->{'file'} ); |
47 | } else { |
48 | warn "Could not find string or file option to parse"; |
49 | return; |
50 | } |
4d85a60e |
51 | my $tei = $doc->documentElement(); |
52 | my $xpc = XML::LibXML::XPathContext->new( $tei ); |
53 | |
54 | # CTE uses a DTD rather than any xmlns-based parsing. Thus we |
55 | # need no namespace handling. |
56 | |
57 | # Get the witnesses and create the witness objects. |
58 | foreach my $wit_el ( $xpc->findnodes( '//sourceDesc/listWit/witness' ) ) { |
59 | # The witness xml:id is used internally, and is *not* the sigil name. |
60 | my $id= $wit_el->getAttribute( 'xml:id' ); |
61 | my @sig_parts = $xpc->findnodes( './abbr/descendant::text()', $wit_el ); |
62 | my $sig = _stringify_sigil( @sig_parts ); |
b8f262e8 |
63 | $tradition->add_witness( sigil => $sig, source => $wit_el->toString() ); |
64 | $sigil_for{'#'.$id} = $sig; # Make life easy by keying on the ID ref syntax |
4d85a60e |
65 | } |
6f4946fb |
66 | |
4d85a60e |
67 | # Now go through the text and find the base tokens, apparatus tags, and |
68 | # anchors. Make a giant array of all of these things in sequence. |
69 | # TODO consider combining this with creation of graph below |
70 | my @base_text; |
71 | foreach my $pg_el ( $xpc->findnodes( '/TEI/text/body/p' ) ) { |
72 | foreach my $xn ( $pg_el->childNodes ) { |
73 | push( @base_text, _get_base( $xn ) ); |
74 | } |
6f4946fb |
75 | } |
4d85a60e |
76 | |
77 | # We now have to work through this array applying the alternate |
78 | # apparatus readings to the base text. Essentially we will put |
79 | # everything on the graph, from which we will delete the apps and |
80 | # anchors when we are done. |
f6e19c7c |
81 | |
82 | # First, put the base tokens, apps, and anchors in the graph. |
4d85a60e |
83 | my $counter = 0; |
84 | my $last = $c->start; |
85 | foreach my $item ( @base_text ) { |
86 | my $r; |
87 | if( $item->{'type'} eq 'token' ) { |
12720144 |
88 | $r = $c->add_reading( { id => 'n'.$counter++, |
89 | text => $item->{'content'} } ); |
4d85a60e |
90 | } elsif ( $item->{'type'} eq 'anchor' ) { |
12720144 |
91 | $r = $c->add_reading( { id => '#ANCHOR_' . $item->{'content'} . '#', |
92 | is_ph => 1 } ); |
4d85a60e |
93 | } elsif ( $item->{'type'} eq 'app' ) { |
94 | my $tag = '#APP_' . $counter++ . '#'; |
12720144 |
95 | $r = $c->add_reading( { id => $tag, is_ph => 1 } ); |
4d85a60e |
96 | $apps{$tag} = $item->{'content'}; |
97 | } |
f6e19c7c |
98 | $c->add_path( $last, $r, $c->baselabel ); |
4d85a60e |
99 | $last = $r; |
6f4946fb |
100 | } |
f6e19c7c |
101 | $c->add_path( $last, $c->end, $c->baselabel ); |
4d85a60e |
102 | |
103 | # Now we can parse the apparatus entries, and add the variant readings |
104 | # to the graph. |
105 | |
106 | foreach my $app_id ( keys %apps ) { |
107 | _add_readings( $c, $app_id ); |
6f4946fb |
108 | } |
4d85a60e |
109 | |
f6e19c7c |
110 | # Finally, add explicit witness paths, remove the base paths, and remove |
111 | # the app/anchor tags. |
112 | expand_all_paths( $c ); |
861c3e27 |
113 | |
114 | # Save the text for each witness so that we can ensure consistency |
115 | # later on |
116 | $tradition->collation->text_from_paths(); |
6f4946fb |
117 | } |
118 | |
4d85a60e |
119 | sub _stringify_sigil { |
120 | my( @nodes ) = @_; |
121 | my @parts = grep { /\w/ } map { $_->data } @nodes; |
122 | return join( '', @parts ); |
123 | } |
6f4946fb |
124 | |
125 | ## Recursive little helper function to help us navigate through nested |
4d85a60e |
126 | ## XML, picking out the words, the apparatus, and the anchors. |
127 | |
128 | sub _get_base { |
129 | my( $xn ) = @_; |
130 | my @readings; |
131 | if( $xn->nodeType == XML_TEXT_NODE ) { |
132 | # Base text, just split the words on whitespace and add them |
133 | # to our sequence. |
f6e19c7c |
134 | # TODO consider that XML markup might appear mid-token. |
4d85a60e |
135 | my $str = $xn->data; |
136 | $str =~ s/^\s+//; |
137 | foreach my $w ( split( /\s+/, $str ) ) { |
138 | push( @readings, { 'type' => 'token', 'content' => $w } ); |
139 | } |
140 | } elsif( $xn->nodeName eq 'hi' ) { |
141 | # Recurse as if the hi weren't there. |
142 | foreach( $xn->childNodes ) { |
143 | push( @readings, _get_base( $_ ) ); |
144 | } |
145 | } elsif( $xn->nodeName eq 'app' ) { |
146 | # Apparatus, just save the entire XML node. |
147 | push( @readings, { 'type' => 'app', 'content' => $xn } ); |
148 | } elsif( $xn->nodeName eq 'anchor' ) { |
149 | # Anchor to mark the end of some apparatus; save its ID. |
150 | push( @readings, { 'type' => 'anchor', |
151 | 'content' => $xn->getAttribute( 'xml:id' ) } ); |
152 | } elsif ( $xn->nodeName ne 'note' ) { # Any tag we don't know to disregard |
153 | print STDERR "Unrecognized tag " . $xn->nodeName . "\n"; |
6f4946fb |
154 | } |
4d85a60e |
155 | return @readings; |
6f4946fb |
156 | } |
157 | |
4d85a60e |
158 | sub _add_readings { |
159 | my( $c, $app_id ) = @_; |
160 | my $xn = $apps{$app_id}; |
161 | my $anchor = _anchor_name( $xn->getAttribute( 'to' ) ); |
162 | # Get the lemma, which is all the readings between app and anchor, |
163 | # excluding other apps or anchors. |
164 | my @lemma = _return_lemma( $c, $app_id, $anchor ); |
165 | my $lemma_str = join( ' ', grep { $_ !~ /^\#/ } map { $_->text } @lemma ); |
166 | |
167 | # For each reading, send its text to 'interpret' along with the lemma, |
168 | # and then save the list of witnesses that these tokens belong to. |
3a5d151b |
169 | my %wit_rdgs; # Maps from witnesses to the variant text |
4d85a60e |
170 | my $ctr = 0; |
171 | my $tag = $app_id; |
172 | $tag =~ s/^\#APP_(.*)\#$/$1/; |
173 | foreach my $rdg ( $xn->getChildrenByTagName( 'rdg' ) ) { |
174 | my @text; |
4d85a60e |
175 | foreach ( $rdg->childNodes ) { |
176 | push( @text, _get_base( $_ ) ); |
177 | } |
12720144 |
178 | my( $interpreted, $flag ) = ( '', undef ); |
179 | if( @text ) { |
180 | ( $interpreted, $flag ) = interpret( |
181 | join( ' ', map { $_->{'content'} } @text ), $lemma_str ); |
182 | } |
b8f262e8 |
183 | next if( $interpreted eq $lemma_str ) && !$flag; # Reading is lemma. |
184 | |
4d85a60e |
185 | my @rdg_nodes; |
b8f262e8 |
186 | if( $interpreted eq '#LACUNA#' ) { |
187 | push( @rdg_nodes, $c->add_reading( { id => $tag . "/" . $ctr++, |
188 | is_lacuna => 1 } ) ); |
189 | } else { |
190 | foreach my $w ( split( /\s+/, $interpreted ) ) { |
191 | my $r = $c->add_reading( { id => $tag . "/" . $ctr++, |
192 | text => $w } ); |
193 | push( @rdg_nodes, $r ); |
194 | } |
4d85a60e |
195 | } |
f6e19c7c |
196 | # For each listed wit, save the reading. |
197 | foreach my $wit ( split( /\s+/, $rdg->getAttribute( 'wit' ) ) ) { |
12720144 |
198 | $wit .= $flag if $flag; |
f6e19c7c |
199 | $wit_rdgs{$wit} = \@rdg_nodes; |
200 | } |
12720144 |
201 | |
3a5d151b |
202 | # Does the reading have an ID? If so it probably has a witDetail |
f6e19c7c |
203 | # attached, and we need to read it. |
3a5d151b |
204 | if( $rdg->hasAttribute( 'xml:id' ) ) { |
12720144 |
205 | warn "Witdetail on meta reading" if $flag; # this could get complicated. |
f6e19c7c |
206 | my $rid = $rdg->getAttribute( 'xml:id' ); |
207 | my $xpc = XML::LibXML::XPathContext->new( $xn ); |
208 | my @details = $xpc->findnodes( './witDetail[@target="'.$rid.'"]' ); |
209 | foreach my $d ( @details ) { |
210 | _parse_wit_detail( $d, \%wit_rdgs, \@lemma ); |
211 | } |
3a5d151b |
212 | } |
f6e19c7c |
213 | } |
214 | |
4d85a60e |
215 | # Now collate the variant readings, since it is not done for us. |
12720144 |
216 | collate_variants( $c, \@lemma, values %wit_rdgs ); |
b8f262e8 |
217 | |
4d85a60e |
218 | # Now add the witness paths for each reading. |
f6e19c7c |
219 | foreach my $wit_id ( keys %wit_rdgs ) { |
220 | my $witstr = get_sigil( $wit_id, $c ); |
221 | my $rdg_list = $wit_rdgs{$wit_id}; |
222 | _add_wit_path( $c, $rdg_list, $app_id, $anchor, $witstr ); |
6f4946fb |
223 | } |
4d85a60e |
224 | } |
6f4946fb |
225 | |
4d85a60e |
226 | sub _anchor_name { |
227 | my $xmlid = shift; |
228 | $xmlid =~ s/^\#//; |
229 | return sprintf( "#ANCHOR_%s#", $xmlid ); |
6f4946fb |
230 | } |
231 | |
4d85a60e |
232 | sub _return_lemma { |
233 | my( $c, $app, $anchor ) = @_; |
12720144 |
234 | my @nodes = grep { $_->id !~ /^\#A(PP|NCHOR)/ } |
235 | $c->reading_sequence( $c->reading( $app ), $c->reading( $anchor ), |
236 | $c->baselabel ); |
4d85a60e |
237 | return @nodes; |
238 | } |
6f4946fb |
239 | |
240 | sub interpret { |
4d85a60e |
241 | # A utility function to change apparatus-ese into a full variant. |
242 | my( $reading, $lemma ) = @_; |
243 | return $reading if $reading eq $lemma; |
244 | my $oldreading = $reading; |
245 | # $lemma =~ s/\s+[[:punct:]]+$//; |
246 | # $reading =~ s/\s*\(?sic([\s\w.]+)?\)?$//; |
12720144 |
247 | my $flag; # In case of p.c. indications |
4d85a60e |
248 | my @words = split( /\s+/, $lemma ); |
249 | if( $reading =~ /^(.*) praem.$/ ) { |
250 | $reading = "$1 $lemma"; |
251 | } elsif( $reading =~ /^(.*) add.$/ ) { |
252 | $reading = "$lemma $1"; |
b8f262e8 |
253 | } elsif( $reading =~ /add. alia manu/ |
254 | || $reading =~ /inscriptionem compegi e/ # TODO huh? |
255 | || $reading eq 'inc.' # TODO huh? |
256 | ) { |
12720144 |
257 | # Ignore it. |
258 | $reading = $lemma; |
b8f262e8 |
259 | } elsif( $reading =~ /locus [uv]acuus/ |
260 | || $reading eq 'def.' |
f6e19c7c |
261 | ) { |
b8f262e8 |
262 | $reading = '#LACUNA#'; |
263 | } elsif( $reading eq 'om.' ) { |
4d85a60e |
264 | $reading = ''; |
12720144 |
265 | } elsif( $reading =~ /^in[uv]\.$/ ) { |
4d85a60e |
266 | # Hope it is two words. |
267 | print STDERR "WARNING: want to invert a lemma that is not two words\n" |
268 | unless scalar( @words ) == 2; |
269 | $reading = join( ' ', reverse( @words ) ); |
12720144 |
270 | } elsif( $reading =~ /^iter(\.|at)$/ ) { |
4d85a60e |
271 | # Repeat the lemma |
272 | $reading = "$lemma $lemma"; |
12720144 |
273 | } elsif( $reading eq 'in marg.' ) { |
274 | # There was nothing before a correction. |
275 | $reading = ''; |
276 | $flag = '_ac'; |
277 | } elsif( $reading =~ /^(.*) \.\.\. (.*)$/ ) { |
4d85a60e |
278 | # The first and last N words captured should replace the first and |
279 | # last N words of the lemma. |
280 | my @begin = split( /\s+/, $1 ); |
281 | my @end = split( /\s+/, $2 ); |
282 | if( scalar( @begin ) + scalar ( @end ) > scalar( @words ) ) { |
283 | # Something is wrong and we can't do the splice. |
284 | print STDERR "ERROR: $lemma is too short to accommodate $oldreading\n"; |
285 | } else { |
286 | splice( @words, 0, scalar @begin, @begin ); |
287 | splice( @words, -(scalar @end), scalar @end, @end ); |
288 | $reading = join( ' ', @words ); |
289 | } |
6f4946fb |
290 | } |
12720144 |
291 | if( $oldreading ne $reading || $flag || $oldreading =~ /\./ ) { |
292 | my $int = $reading; |
293 | $int .= " ($flag)" if $flag; |
294 | print STDERR "Interpreted $oldreading as $int given $lemma\n"; |
295 | } |
296 | return( $reading, $flag ); |
4d85a60e |
297 | } |
298 | |
f6e19c7c |
299 | sub _parse_wit_detail { |
300 | my( $detail, $readings, $lemma ) = @_; |
301 | my $wit = $detail->getAttribute( 'wit' ); |
302 | my $content = $detail->textContent; |
303 | if( $content =~ /a\.\s*c\./ ) { |
304 | # Replace the key in the $readings hash |
305 | my $rdg = delete $readings->{$wit}; |
306 | $readings->{$wit.'_ac'} = $rdg; |
307 | $has_ac{$sigil_for{$wit}} = 1; |
308 | } elsif( $content =~ /p\.\s*c\./ ) { |
309 | # If no key for the wit a.c. exists, add one pointing to the lemma |
310 | unless( exists $readings->{$wit.'_ac'} ) { |
311 | $readings->{$wit.'_ac'} = $lemma; |
312 | } |
313 | $has_ac{$sigil_for{$wit}} = 1; |
314 | } # else don't bother just yet |
315 | } |
316 | |
317 | sub get_sigil { |
318 | my( $xml_id, $c ) = @_; |
319 | if( $xml_id =~ /^(.*)_ac$/ ) { |
320 | my $real_id = $1; |
321 | return $sigil_for{$real_id} . $c->ac_label; |
322 | } else { |
323 | return $sigil_for{$xml_id}; |
324 | } |
325 | } |
326 | |
327 | sub expand_all_paths { |
328 | my( $c ) = @_; |
329 | |
330 | # Walk the collation and fish out the paths for each witness |
331 | foreach my $wit ( $c->tradition->witnesses ) { |
332 | my $sig = $wit->sigil; |
12720144 |
333 | my @path = grep { !$_->is_ph } |
f6e19c7c |
334 | $c->reading_sequence( $c->start, $c->end, $sig ); |
335 | $wit->path( \@path ); |
336 | if( $has_ac{$sig} ) { |
12720144 |
337 | my @ac_path = grep { !$_->is_ph } |
861c3e27 |
338 | $c->reading_sequence( $c->start, $c->end, $sig.$c->ac_label ); |
f6e19c7c |
339 | $wit->uncorrected_path( \@ac_path ); |
340 | } |
341 | } |
342 | |
343 | # Delete the anchors |
12720144 |
344 | foreach my $anchor ( grep { $_->is_ph } $c->readings ) { |
f6e19c7c |
345 | $c->del_reading( $anchor ); |
346 | } |
12720144 |
347 | # Delete the base edges |
348 | map { $c->del_path( $_, $c->baselabel ) } $c->paths; |
f6e19c7c |
349 | |
350 | # Make the path edges |
351 | $c->make_witness_paths(); |
4d85a60e |
352 | } |
353 | |
354 | sub _add_wit_path { |
f6e19c7c |
355 | my( $c, $rdg, $app, $anchor, $wit ) = @_; |
4d85a60e |
356 | my @nodes = @$rdg; |
f6e19c7c |
357 | push( @nodes, $c->reading( $anchor ) ); |
4d85a60e |
358 | |
f6e19c7c |
359 | my $cur = $c->reading( $app ); |
4d85a60e |
360 | foreach my $n ( @nodes ) { |
f6e19c7c |
361 | $c->add_path( $cur, $n, $wit ); |
4d85a60e |
362 | $cur = $n; |
6f4946fb |
363 | } |
6f4946fb |
364 | } |
365 | |
366 | =back |
367 | |
368 | =head1 LICENSE |
369 | |
370 | This package is free software and is provided "as is" without express |
371 | or implied warranty. You can redistribute it and/or modify it under |
372 | the same terms as Perl itself. |
373 | |
374 | =head1 AUTHOR |
375 | |
376 | Tara L Andrews, aurum@cpan.org |
377 | |
378 | =cut |
379 | |
380 | 1; |
381 | |