1 package Text::Tradition::Parser::CTE;
6 use XML::LibXML::XPathContext;
10 Text::Tradition::Parser::CTE
14 Parser module for Text::Tradition, given a TEI file exported from
15 Classical Text Editor.
23 my @apparatus = read( $xml_file );
25 Takes a Tradition object and a TEI file exported from Classical Text
26 Editor; initializes the Tradition from the file.
30 my %seg_readings; # Save the XML IDs for apparatus anchors.
31 my %sigil_for; # Save the XML IDs for witnesses.
32 my %note_start; # Save the readings where an apparatus entry is attached.
35 my( $tradition, $xml_str ) = @_;
36 my $c = $tradition->collation; # Some shorthand
38 # First, parse the XML.
39 my $parser = XML::LibXML->new();
40 my $doc = $parser->parse_string( $xml_str );
41 my $tei = $doc->documentElement();
42 my $xpc = XML::LibXML::XPathContext->new( $tei );
44 # CTE uses a DTD rather than any xmlns-based parsing. Thus we
45 # need no namespace foo.
47 # Get the witnesses and create the witness objects.
48 foreach my $wit_el ( $xpc->findnodes( '//sourceDesc/listWit/witness' ) ) {
49 # The witness xml:id is used internally, and is *not* the sigil name.
50 my $id= $wit_el->getAttribute( 'xml:id' );
51 $id =~ s/^M/sig/; # Stupid but there you go.
52 my @sig_parts = $xpc->findnodes( './abbr/descendant::text()', $wit_el );
53 my $sig = join( '', grep { /\w/ } @sig_parts );
54 $tradition->add_witness( sigil => $sig, source => $wit_el->toString() );
55 $sigil_for{$id} = $sig;
58 # Now go through the text and find the base tokens. Tokens are
59 # either plain text to be split on whitespace, or they are wrapped
60 # in <hi/> or <seg/> elements.
63 foreach my $pg_el ( $xpc->findnodes( '/TEI/text/p' ) ) {
64 foreach my $xn ( $pg_el->childNodes ) {
65 push( @base_text, _get_readings( $tradition, $xn ) );
69 # String together the base.
70 my $source = $c->start;
71 foreach my $b ( @base_text ) {
72 $c->add_path( $source, $b, $c->baselabel );
75 $c->add_path( $source, $c->add_reading('#END#'), $c->baselabel );
77 # Now go through the text and find all the apparatus notes, and parse them.
78 foreach my $note_el( $xpc->findnodes( '//note[attribute::type = "a1"]' ) ) {
79 my $app_start = $note_start{$note_el};
80 my $apparatus = _parse_note( $note_el, $c, $app_start );
85 ## Recursive little helper function to help us navigate through nested
86 ## XML, picking out the text.
89 my( $tradition, $xn ) = @_;
91 if( $xn->nodeType == XML_TEXT_NODE ) {
94 foreach my $w ( split( /\s+/, $str ) ) {
95 my $rdg = $tradition->collation->add_reading( 'n'.$ctr++ );
97 push( @readings, $rdg );
99 } elsif( $xn->nodeName eq 'hi' ) {
100 foreach( $xn->childNodes ) {
101 # Recurse as if the hi weren't there.
102 push( @readings, _get_readings( $tradition, $_ ) );
104 } elsif( $xn->nodeName eq 'seg' ) {
105 # Read the reading, but also add the word in question as an anchor.
106 my $seg_id = $xn->getAttribute( 'xml:id' );
108 foreach( $xn->childNodes ) {
109 push( @r, _get_readings( $tradition, $_ ) );
111 warn "More than one reading found in seg $seg_id" unless @r == 1;
112 $seg_readings{'#'.$seg_id} = $r[0];
113 push( @readings, @r );
114 } elsif( $xn->nodeName eq 'note' ) {
115 # Save where we found this note.
116 $note_start{$xn} = $readings[-1];
121 ## Helper function to parse apparatus entries. This could get nasty, I mean fun.
123 my( $xn, $c, $app_start ) = @_;
124 my $app_end = $seg_readings{$xn->getAttribute( 'targetEnd' )};
125 my $lemma = join( ' ', map { $_->text } $c->reading_sequence( $app_start, $app_end ) );
128 # TODO A list of active witnesses should be passed really.
129 map { $seen_wits{$_} = 0 } vals( %sigil_for );
131 # The note has a <p/> tag, then <mentioned/>, then 0-1 text nodes,
132 # then an assortment of <hi/> or <abbr/> elements. If the hi
133 # contains an abbr, then it goes before, otherwise it probably
135 my @p = $xn->getChildrenByTagName( 'p' );
136 warn "More than one pg in note" unless @p == 1;
138 # Strip the <hi/> elements.
140 foreach ( $p[0]->childNodes ) {
141 if( $_->nodeName eq 'hi' ) {
142 push( @childnodes, $_->childNodes );
144 push( @childnodes, $_ );
148 # Go through and try to parse the sucker.
151 my $reading_sigla = 0;
153 foreach my $pxn ( $p[0]->childNodes ) {
154 next if $pxn->nodeName eq 'mentioned'; # Redundant for us.
155 if( $pxn->nodeType == XML_TEXT_NODE ) {
156 my $pxn_str = $pxn->data;
157 $pxn_str =~ s/^\s+//;
158 $pxn_str =~ s/\s+$//;
159 my @parts = split( /,\s*/, $pxn_str );
161 # Comma separation means that we are starting a new reading.
162 my $last = shift @parts;
163 if( $last =~ /^\s*a\.\s*c\.\s*$/ ) {
164 my $sig = pop @curr_wits;
166 push( @curr_wits, $sig );
168 $pxn_str = join( ', ', @parts );
169 # Trigger a reading interpretation.
172 if( $reading_sigla ) {
173 my @wits = keys %curr_wits;
174 $apparatus->{ interpret( $curr_rdg, $lemma ) } = \@wits;
180 if( $pxn_str =~ /^\s*a\.\s*c\.\s*$/ ) {
181 my $sig = pop @curr_wits;
183 push( @curr_wits, $sig );
185 $curr_rdg .= $pxn_str;
187 } elsif( $pxn->nodeName eq 'abbr' ) {
188 # It is a witness, stick it in @curr_wits
189 my $wit = $sigil_for{$pxn->getAttribute( 'n' )}
190 push( @curr_wits, $wit ) unless $curr_wits[-1] eq $wit;
191 $seen_wits{$wit} += 1; # Keep track of a 'seen' count in case there is an a.c.
195 $apparatus->{ interpret( $curr_rdg, $lemma ) } = \@wits if $curr_rdg;
196 $apparatus->{ $lemma } = grep { $seen_wits{$_} == 0 } keys %seen_wits;
203 # A utility function to change apparatus-ese into a full variant.
204 my( $reading, $lemma ) = @_;
205 return $reading if $reading eq $lemma;
206 my $oldreading = $reading;
207 $lemma =~ s/\s+[[:punct:]]+$//;
208 $reading =~ s/\s*\(?sic([\s\w.]+)?\)?$//;
209 my @words = split( /\s+/, $lemma );
210 if( $reading =~ /^(.*) praem.$/ ) {
211 $reading = "$1 $lemma";
212 } elsif( $reading =~ /^(.*) add.$/ ) {
213 $reading = "$lemma $1";
214 } elsif( $reading eq 'om.' ) {
216 } elsif( $reading eq 'inv.' ) {
217 # Hope it is two words.
218 print STDERR "WARNING: want to invert a lemma that is not two words\n"
219 unless scalar( @words ) == 2;
220 $reading = join( ' ', reverse( @words ) );
221 } elsif( $reading eq 'iter.' ) {
223 $reading = "$lemma $lemma";
224 } elsif( $reading =~ /^(.*) \.\.\. (.*)$/ ) {
225 # The first and last N words captured should replace the first and
226 # last N words of the lemma.
227 my @begin = split( /\s+/, $1 );
228 my @end = split( /\s+/, $2 );
229 if( scalar( @begin ) + scalar ( @end ) > scalar( @words ) ) {
230 # Something is wrong and we can't do the splice.
231 print STDERR "ERROR: $lemma is too short to accommodate $oldreading\n";
233 splice( @words, 0, scalar @begin, @begin );
234 splice( @words, -(scalar @end), scalar @end, @end );
235 $reading = join( ' ', @words );
238 print STDERR "Interpreted $oldreading as $reading given $lemma\n";
246 This package is free software and is provided "as is" without express
247 or implied warranty. You can redistribute it and/or modify it under
248 the same terms as Perl itself.
252 Tara L Andrews, aurum@cpan.org