new positioning system, works great for graph, needs work for CSV
[scpubgit/stemmatology.git] / lib / Text / Tradition / Parser / BaseText.pm
CommitLineData
e58153d6 1package Text::Tradition::Parser::BaseText;
b49c4318 2
3use strict;
4use warnings;
52ce987f 5use Module::Load;
4ca00eca 6use Algorithm::Diff;
b49c4318 7
2ceca8c3 8=head1 NAME
9
10Text::Tradition::Parser::BaseText
11
12=head1 SYNOPSIS
13
14use Text::Tradition::Parser::BaseText qw( merge_base );
15merge_base( $graph, 'reference.txt', @apparatus_entries )
16
17=head1 DESCRIPTION
18
19For an overview of the package, see the documentation for the
20Text::Tradition::Graph module.
21
22This module is meant for use with certain of the other Parser classes
23- whenever a list of variants is given with reference to a base text,
24these must be joined into a single collation. The parser should
25therefore make a list of variants and their locations, and BaseText
26will join those listed variants onto the reference text.
27
28=head1 SUBROUTINES
29
30=over
31
52ce987f 32=item B<parse>
33
34parse( $graph, %opts );
35
36Takes an initialized graph and a set of options, which must include:
37- 'base' - the base text referenced by the variants
38- 'format' - the format of the variant list
39- 'data' - the variants, in the given format.
40
41=cut
42
43sub parse {
e2902068 44 my( $tradition, %opts ) = @_;
52ce987f 45
46 my $format_mod = 'Text::Tradition::Parser::' . $opts{'format'};
47 load( $format_mod );
48 my @apparatus_entries = $format_mod->can('read')->( $opts{'data'} );
e2902068 49 merge_base( $tradition->collation, $opts{'base'}, @apparatus_entries );
52ce987f 50}
51
2ceca8c3 52=item B<merge_base>
53
54merge_base( $graph, 'reference.txt', @apparatus_entries )
55
56Takes three arguments: a newly-initialized Text::Tradition::Graph
57object, a text file containing the reference text, and a list of
58variants (apparatus entries). Adds the base text to the graph, and
59joins the variants to that.
60
61The list of variants is an array of hash references; each hash takes
62the form
63 { '_id' => line reference,
64 'rdg_0' => lemma reading,
65 'rdg_1' => first variant,
66 ... # and so on until all distinct readings are listed
67 'WitnessA' => 'rdg_0',
68 'WitnessB' => 'rdg_1',
69 ... # and so on until all witnesses are listed with their readings
70 }
71
72Any hash key that is not of the form /^rdg_\d+$/ and that does not
73begin with an underscore is assumed to be a witness name. Any 'meta'
74information to be passed must be passed in a key with a leading
75underscore in its name.
76
77=cut
78
b15511bf 79my $SHORTEND = ''; # Debug var - set this to limit the number of lines parsed
4ca00eca 80
81my %base_text_index;
6a222840 82my $edits_required = {};
4ca00eca 83
84# edits_required -> wit -> [ { start_idx, end_idx, items } ]
930ff666 85
b49c4318 86sub merge_base {
e2902068 87 my( $collation, $base_file, @app_entries ) = @_;
88 my @base_line_starts = read_base( $base_file, $collation );
b49c4318 89
52ce987f 90 my %all_witnesses;
6a222840 91 my @unwitnessed_lemma_nodes;
b49c4318 92 foreach my $app ( @app_entries ) {
93 my( $line, $num ) = split( /\./, $app->{_id} );
94 # DEBUG with a short graph
15d2d3df 95 last if $SHORTEND && $line > $SHORTEND;
2ceca8c3 96 # DEBUG for problematic entries
4ca00eca 97 my $scrutinize = '';
e2902068 98 my $first_line_reading = $base_line_starts[ $line ];
b49c4318 99 my $too_far = $base_line_starts[ $line+1 ];
100
101 my $lemma = $app->{rdg_0};
102 my $seq = 1;
103 # Is this the Nth occurrence of this reading in the line?
104 if( $lemma =~ s/(_)?(\d)$// ) {
105 $seq = $2;
106 }
107 my @lemma_words = split( /\s+/, $lemma );
108
109 # Now search for the lemma words within this line.
e2902068 110 my $lemma_start = $first_line_reading;
b49c4318 111 my $lemma_end;
112 my %seen;
113 while( $lemma_start ne $too_far ) {
114 # Loop detection
115 if( $seen{ $lemma_start->name() } ) {
116 warn "Detected loop at " . $lemma_start->name() .
117 ", ref $line,$num";
118 last;
119 }
120 $seen{ $lemma_start->name() } = 1;
121
122 # Try to match the lemma.
123 my $unmatch = 0;
124 print STDERR "Matching " . cmp_str( $lemma_start) . " against " .
125 $lemma_words[0] . "...\n"
126 if "$line.$num" eq $scrutinize;
127 if( cmp_str( $lemma_start ) eq $lemma_words[0] ) {
128 # Skip it if we need a match that is not the first.
129 if( --$seq < 1 ) {
130 # Now we have to compare the rest of the words here.
131 if( scalar( @lemma_words ) > 1 ) {
e2902068 132 my $next_reading =
133 $collation->next_reading( $lemma_start );
b49c4318 134 foreach my $w ( @lemma_words[1..$#lemma_words] ) {
135 printf STDERR "Now matching %s against %s\n",
e2902068 136 cmp_str($next_reading), $w
b49c4318 137 if "$line.$num" eq $scrutinize;
e2902068 138 if( $w ne cmp_str($next_reading) ) {
b49c4318 139 $unmatch = 1;
140 last;
141 } else {
e2902068 142 $lemma_end = $next_reading;
143 $next_reading =
144 $collation->next_reading( $lemma_end );
b49c4318 145 }
146 }
147 } else {
148 $lemma_end = $lemma_start;
149 }
150 } else {
151 $unmatch = 1;
152 }
153 }
154 last unless ( $unmatch || !defined( $lemma_end ) );
155 $lemma_end = undef;
e2902068 156 $lemma_start = $collation->next_reading( $lemma_start );
b49c4318 157 }
158
159 unless( $lemma_end ) {
160 warn "No match found for @lemma_words at $line.$num";
161 next;
b49c4318 162 }
163
4ca00eca 164 # Now we have found the lemma; we will record an 'edit', in
165 # terms of a splice operation, for each subsequent reading.
166 # We also note which witnesses take the given edit.
167
6a222840 168 my @lemma_set = $collation->reading_sequence( $lemma_start,
169 $lemma_end );
4ca00eca 170 my @reading_sets = [ @lemma_set ];
171
172 # For each reading that is not rdg_0, we create the variant
173 # reading nodes, and store the range as an edit operation on
174 # the base text.
175 my $variant_objects;
6a222840 176 my %pc_seen; # Keep track of mss with explicit post-corr data
b49c4318 177 foreach my $k ( grep { /^rdg/ } keys( %$app ) ) {
b49c4318 178 my @mss = grep { $app->{$_} eq $k } keys( %$app );
1ed3973e 179
180 # Keep track of lemma nodes that don't actually appear in
181 # any MSS; we will want to remove them from the collation.
6a222840 182 push( @unwitnessed_lemma_nodes, @lemma_set )
183 if !@mss && $k eq 'rdg_0';
184
e2902068 185 # Keep track of what witnesses we have seen.
52ce987f 186 @all_witnesses{ @mss } = ( 1 ) x scalar( @mss );
4ca00eca 187 # Keep track of which witnesses bear corrected readings here.
188 foreach my $m ( @mss ) {
189 my $base = _is_post_corr( $m );
190 next unless $base;
6a222840 191 $pc_seen{$base} = 1;
4ca00eca 192 }
193 next if $k eq 'rdg_0';
194
1ed3973e 195 # Parse the variant into reading tokens.
4ca00eca 196 # TODO don't hardcode the reading split operation
197 my @variant = split( /\s+/, $app->{$k} );
198 @variant = () if $app->{$k} eq '/'; # This is an omission.
b49c4318 199
4ca00eca 200 my @variant_readings;
b49c4318 201 my $ctr = 0;
b49c4318 202 foreach my $vw ( @variant ) {
203 my $vwname = "$k/$line.$num.$ctr"; $ctr++;
e2902068 204 my $vwreading = $collation->add_reading( $vwname );
205 $vwreading->text( $vw );
4ca00eca 206 push( @variant_readings, $vwreading );
b49c4318 207 }
e49731d7 208
4ca00eca 209 $variant_objects->{$k} = { 'mss' => \@mss,
210 'reading' => \@variant_readings,
211 };
212 push( @reading_sets, \@variant_readings );
213 }
b49c4318 214
4ca00eca 215 # Now collate and collapse the identical readings within the
216 # collated sets. Modifies the reading sets that were passed.
217 collate_variants( $collation, @reading_sets );
218
1ed3973e 219 # Record any stated relationships between the nodes and the lemma.
3265b0ce 220 set_relationships( $collation, $app, \@lemma_set, $variant_objects );
15d2d3df 221
4ca00eca 222 # Now create the splice-edit objects that will be used
223 # to reconstruct each witness.
224
225 foreach my $rkey ( keys %$variant_objects ) {
226 # Object is argument list for splice, so:
227 # offset, length, replacements
c78feb69 228 my $edit_object = [ $lemma_start->name,
4ca00eca 229 scalar( @lemma_set ),
230 $variant_objects->{$rkey}->{reading} ];
231 foreach my $ms ( @{$variant_objects->{$rkey}->{mss}} ) {
232 # Is this a p.c. entry?
233 my $base = _is_post_corr( $ms );
234 if( $base ) { # this is a post-corr witness
235 my $pc_key = $base . "_post";
236 _add_hash_entry( $edits_required, $pc_key, $edit_object );
237 } else { # this is an ante-corr witness
238 my $pc_key = $ms . "_post";
6a222840 239 _add_hash_entry( $edits_required, $ms, $edit_object );
240 unless( $pc_seen{$ms} ) {
241 # If this witness carries no correction, add this
242 # same object to its post-corrected state.
243 _add_hash_entry( $edits_required, $pc_key,
244 $edit_object );
4ca00eca 245 }
246 }
e2902068 247 }
4ca00eca 248 }
249 } # Finished going through the apparatus entries
250
251 # Now make the witness objects, and create their text sequences
6a222840 252 foreach my $w ( grep { $_ !~ /_post$/ } keys %$edits_required ) {
15d2d3df 253 print STDERR "Creating witness $w\n";
4ca00eca 254 my $witness_obj = $collation->tradition->add_witness( sigil => $w );
b15511bf 255 my $debug; # = $w eq 'Vb11';
256 my @ante_corr_seq = apply_edits( $collation, $edits_required->{$w}, $debug );
257 my @post_corr_seq = apply_edits( $collation, $edits_required->{$w."_post"}, $debug )
258 if exists( $edits_required->{$w."_post"} );
259
260 my @repeated = _check_for_repeated( @ante_corr_seq );
261 warn "Repeated elements @repeated in $w a.c."
262 if @repeated;
263 @repeated = _check_for_repeated( @post_corr_seq );
264 warn "Repeated elements @repeated in $w p.c."
15d2d3df 265 if @repeated;
b15511bf 266
6a222840 267 # Now save these paths in my witness object
b15511bf 268 if( @post_corr_seq ) {
269 $witness_obj->path( \@post_corr_seq );
270 $witness_obj->uncorrected_path( \@ante_corr_seq );
271 } else {
272 $witness_obj->path( \@ante_corr_seq );
52ce987f 273 }
b49c4318 274 }
e2902068 275
6a222840 276 # Now remove our 'base text' edges, which is to say, the only
1ed3973e 277 # ones we have created so far. Also remove any unwitnessed
278 # lemma nodes (TODO unless we are treating base as witness)
6a222840 279 foreach ( $collation->paths() ) {
280 $collation->del_path( $_ );
281 }
282 foreach( @unwitnessed_lemma_nodes ) {
283 $collation->del_reading( $_ );
284 }
4ca00eca 285
b15511bf 286 ### HACKY HACKY Do some one-off path corrections here.
287 if( $collation->linear ) {
288 # What?
289 } else {
290 my $c = $collation;
291 # Vb5:
292 my $path = $c->tradition->witness('Vb5')->path;
293 splice( @$path, 1436, 0, $c->reading('106,14') );
294 # Vb11:
295 $path = $c->tradition->witness('Vb11')->path;
296 $c->merge_readings( $c->reading('rdg_1/16.3.0'), $c->reading('rdg_1/16.2.1') );
297 splice( @$path, 209, 2, $c->reading( 'rdg_1/16.3.0' ), $c->reading( '16,1' ) );
298 # Vb12 a.c.:
299 $path = $c->tradition->witness('Vb12')->uncorrected_path;
300 splice( @$path, 1828, 1, $c->reading('rdg_2/137.5.0') );
301 # Vb13:
302 $path = $c->tradition->witness('Vb13')->path;
303 splice( @$path, 782, 0, $c->reading( '58,5' ) );
304 # Vb20 a.c.:
305 $path = $c->tradition->witness('Vb20')->uncorrected_path;
306 splice( @$path, 1251, 1, $c->reading( '94,6' ) );
307 # Vb26:
308 $path = $c->tradition->witness('Vb26')->path;
309 splice( @$path, 618, 0, $c->reading('46,2') )
310 }
311
e2902068 312 # Now walk paths and calculate positions.
313 my @common_readings =
6a222840 314 $collation->make_witness_paths();
e2902068 315 $collation->calculate_positions( @common_readings );
b49c4318 316}
317
15d2d3df 318sub _check_for_repeated {
319 my @seq = @_;
320 my %unique;
321 my @repeated;
322 foreach ( @seq ) {
323 if( exists $unique{$_->name} ) {
324 push( @repeated, $_->name );
325 } else {
326 $unique{$_->name} = 1;
327 }
328 }
329 return @repeated;
330}
331
2ceca8c3 332=item B<read_base>
333
e2902068 334my @line_beginnings = read_base( 'reference.txt', $collation );
2ceca8c3 335
e2902068 336Takes a text file and a (presumed empty) collation object, adds the
337words as simple linear readings to the collation, and returns a
338list of readings that represent the beginning of lines. This collation
339is now the starting point for application of apparatus entries in
340merge_base, e.g. from a CSV file or a Classical Text Editor file.
2ceca8c3 341
342=cut
b49c4318 343
344sub read_base {
e2902068 345 my( $base_file, $collation ) = @_;
b49c4318 346
e2902068 347 # This array gives the first reading for each line. We put the
b49c4318 348 # common starting point in line zero.
e2902068 349 my $last_reading = $collation->start();
6a222840 350 $base_text_index{$last_reading->name} = 0;
e2902068 351 my $lineref_array = [ $last_reading ]; # There is no line zero.
b49c4318 352
353 open( BASE, $base_file ) or die "Could not open file $base_file: $!";
6a222840 354 my $i = 1;
b49c4318 355 while(<BASE>) {
e2902068 356 # Make the readings, and connect them up for the base, but
357 # also save the first reading of each line in an array for the
358 # purpose.
359 # TODO use configurable reading separator
b49c4318 360 chomp;
361 my @words = split;
362 my $started = 0;
363 my $wordref = 0;
364 my $lineref = scalar @$lineref_array;
15d2d3df 365 last if $SHORTEND && $lineref > $SHORTEND;
b49c4318 366 foreach my $w ( @words ) {
e2902068 367 my $readingref = join( ',', $lineref, ++$wordref );
368 my $reading = $collation->add_reading( $readingref );
369 $reading->text( $w );
b49c4318 370 unless( $started ) {
e2902068 371 push( @$lineref_array, $reading );
b49c4318 372 $started = 1;
373 }
4ca00eca 374 # Add edge paths in the graph, for easier tracking when
375 # we start applying corrections. These paths will be
376 # removed when we're done.
377 my $path = $collation->add_path( $last_reading, $reading,
378 $collation->baselabel );
379 $last_reading = $reading;
380
381 # Note an array index for the reading, for later correction splices.
382 $base_text_index{$readingref} = $i++;
b49c4318 383 }
384 }
385 close BASE;
386 # Ending point for all texts
e2902068 387 my $endpoint = $collation->add_reading( '#END#' );
930ff666 388 $collation->add_path( $last_reading, $endpoint, $collation->baselabel );
b49c4318 389 push( @$lineref_array, $endpoint );
6a222840 390 $base_text_index{$endpoint->name} = $i;
b49c4318 391
392 return( @$lineref_array );
393}
394
e49731d7 395=item B<collate_variants>
2ceca8c3 396
4ca00eca 397collate_variants( $collation, @reading_ranges )
2ceca8c3 398
e49731d7 399Given a set of readings in the form
400( lemma_start, lemma_end, rdg1_start, rdg1_end, ... )
e2902068 401walks through each to identify those readings that are identical. The
402collation is a Text::Tradition::Collation object; the elements of
403@readings are Text::Tradition::Collation::Reading objects that appear
404on the collation graph.
b49c4318 405
2ceca8c3 406TODO: Handle collapsed and non-collapsed transpositions.
407
408=cut
b49c4318 409
e49731d7 410sub collate_variants {
4ca00eca 411 my( $collation, @reading_sets ) = @_;
4ca00eca 412
c78feb69 413 # Two different ways to do this, depending on whether we want
414 # transposed reading nodes to be merged into one (producing a
415 # nonlinear, bidirectional graph) or not (producing a relatively
416 # linear, unidirectional graph.)
417 return $collation->linear ? collate_linearly( @_ )
418 : collate_nonlinearly( @_ );
419}
4ca00eca 420
c78feb69 421sub collate_linearly {
422 my( $collation, $lemma_set, @variant_sets ) = @_;
4ca00eca 423
424 my @unique;
425 push( @unique, @$lemma_set );
c78feb69 426 while( @variant_sets ) {
427 my $variant_set = shift @variant_sets;
428 # Use diff to do this job
429 my $diff = Algorithm::Diff->new( \@unique, $variant_set,
430 {'keyGen' => \&_collation_hash} );
431 my @new_unique;
432 my %merged;
433 while( $diff->Next ) {
434 if( $diff->Same ) {
435 # merge the nodes
436 my @l = $diff->Items( 1 );
437 my @v = $diff->Items( 2 );
438 foreach my $i ( 0 .. $#l ) {
439 if( !$merged{$l[$i]->name} ) {
440 $collation->merge_readings( $l[$i], $v[$i] );
441 $merged{$l[$i]->name} = 1;
442 } else {
443 print STDERR "Would have double merged " . $l[$i]->name . "\n";
6a222840 444 }
e49731d7 445 }
c78feb69 446 # splice the lemma nodes into the variant set
447 my( $offset ) = $diff->Get( 'min2' );
448 splice( @$variant_set, $offset, scalar( @l ), @l );
449 push( @new_unique, @l );
450 } else {
451 # Keep the old unique readings
452 push( @new_unique, $diff->Items( 1 ) ) if $diff->Items( 1 );
453 # Add the new readings to the 'unique' list
454 push( @new_unique, $diff->Items( 2 ) ) if $diff->Items( 2 );
b49c4318 455 }
c78feb69 456 }
457 @unique = @new_unique;
458 }
459}
460
461sub collate_nonlinearly {
462 my( $collation, $lemma_set, @variant_sets ) = @_;
463
464 my @unique;
465 push( @unique, @$lemma_set );
466 while( @variant_sets ) {
467 my $variant_set = shift @variant_sets;
468 # Simply match the first reading that carries the same word, so
469 # long as that reading has not yet been used to match another
470 # word in this variant. That way lies loopy madness.
471 my @distinct;
472 my %merged;
473 foreach my $idx ( 0 .. $#{$variant_set} ) {
474 my $vw = $variant_set->[$idx];
475 my @same = grep { cmp_str( $_ ) eq $vw->label } @unique;
476 my $matched;
477 if( @same ) {
478 foreach my $i ( 0 .. $#same ) {
479 unless( $merged{$same[$i]->name} ) {
b15511bf 480 #print STDERR sprintf( "Merging %s into %s\n",
481 # $vw->name,
482 # $same[$i]->name );
c78feb69 483 $collation->merge_readings( $same[$i], $vw );
484 $merged{$same[$i]->name} = 1;
485 $matched = $i;
486 $variant_set->[$idx] = $same[$i];
15d2d3df 487 }
488 }
6a222840 489 }
c78feb69 490 unless( @same && defined($matched) ) {
491 push( @distinct, $vw );
492 }
b49c4318 493 }
c78feb69 494 push( @unique, @distinct );
b49c4318 495 }
4ca00eca 496}
2ceca8c3 497
c78feb69 498
4ca00eca 499
500sub _collation_hash {
501 my $node = shift;
6a222840 502 return cmp_str( $node );
4ca00eca 503}
2ceca8c3 504
15d2d3df 505sub set_relationships {
3265b0ce 506 my( $collation, $app, $lemma, $variants ) = @_;
15d2d3df 507 foreach my $rkey ( keys %$variants ) {
508 my $var = $variants->{$rkey}->{'reading'};
b15511bf 509 my $type = $app->{sprintf( "_%s_type", $rkey )};
510 my $noncorr = $app->{sprintf( "_%s_non_corr", $rkey )};
511 my $nonindep = $app->{sprintf( "_%s_non_indep", $rkey )};
512
513 my %rel_options = ();
514 $rel_options{'non_correctable'} = $noncorr if $noncorr && $noncorr =~ /^\d$/;
515 $rel_options{'non_indep'} = $nonindep if $nonindep && $nonindep =~ /^\d$/;
15d2d3df 516
051ddba3 517 if( $type =~ /^(inv|tr|rep)$/i ) {
518 # Transposition or repetition: look for nodes with the
519 # same label but different IDs and mark them.
520 $type = 'repetition' if $type =~ /^rep/i;
b15511bf 521 $rel_options{'sort'} = $type;
3265b0ce 522 my %labels;
523 foreach my $r ( @$lemma ) {
051ddba3 524 $labels{cmp_str( $r )} = $r;
3265b0ce 525 }
526 foreach my $r( @$var ) {
527 if( exists $labels{$r->label} &&
528 $r->name ne $labels{$r->label}->name ) {
051ddba3 529 if( $type eq 'repetition' ) {
530 # Repetition
b15511bf 531 $collation->add_relationship( $r, $labels{$r->label}, \%rel_options );
051ddba3 532 } else {
533 # Transposition
534 $r->set_identical( $labels{$r->label} );
535 }
3265b0ce 536 }
537 }
b15511bf 538 } elsif( $type =~ /^(gr|lex|sp(el)?)$/i ) {
539
540 # Grammar/spelling/lexical: this can be a one-to-one or
541 # one-to-many mapping. We should think about merging
542 # readings if it is one-to-many.
543
3265b0ce 544 $type = 'grammatical' if $type =~ /gr/i;
545 $type = 'spelling' if $type =~ /sp/i;
546 $type = 'repetition' if $type =~ /rep/i;
b15511bf 547 $type = 'lexical' if $type =~ /lex/i;
548 $rel_options{'sort'} = $type;
3265b0ce 549 if( @$lemma == @$var ) {
550 foreach my $i ( 0 .. $#{$lemma} ) {
b15511bf 551 $collation->add_relationship( $var->[$i], $lemma->[$i],
552 \%rel_options );
553 }
3265b0ce 554 } else {
b15511bf 555 # An uneven many-to-many mapping. Make a segment out of
556 # whatever we have.
557 my $lemseg = @$lemma > 1 ? $collation->add_segment( @$lemma ) : $lemma->[0];
558 my $varseg = @$var > 1 ? $collation->add_segment( @$var ) : $var->[0];
559 $collation->add_relationship( $varseg, $lemseg, \%rel_options );
3265b0ce 560 }
b15511bf 561 } elsif( $type !~ /^(add|om)$/i ) {
3265b0ce 562 warn "Unrecognized type $type";
563 }
15d2d3df 564 }
565}
566
567
568
4ca00eca 569sub apply_edits {
b15511bf 570 my( $collation, $edit_sequence, $debug ) = @_;
c78feb69 571 my @lemma_text = $collation->reading_sequence( $collation->start,
572 $collation->reading( '#END#' ) );
4ca00eca 573 my $drift = 0;
b15511bf 574 foreach my $correction ( @$edit_sequence ) {
575 my( $lemma_start, $length, $items ) = @$correction;
576 my $offset = $base_text_index{$lemma_start};
577 my $realoffset = $offset + $drift;
578 if( $debug ||
579 $lemma_text[$realoffset]->name ne $lemma_start ) {
580 my @this_phrase = @lemma_text[$realoffset..$realoffset+$length-1];
581 my @base_phrase;
582 my $i = $realoffset;
583 my $l = $collation->reading( $lemma_start );
584 while( $i < $realoffset+$length ) {
585 push( @base_phrase, $l );
586 $l = $collation->next_reading( $l );
587 $i++;
588 }
589
590 print STDERR sprintf( "Trying to replace %s (%s) starting at %d " .
591 "with %s (%s) with drift %d\n",
592 join( ' ', map {$_->label} @base_phrase ),
593 join( ' ', map {$_->name} @base_phrase ),
594 $realoffset,
595 join( ' ', map {$_->label} @$items ),
596 join( ' ', map {$_->name} @$items ),
597 $drift,
598 ) if $debug;
599
600 if( $lemma_text[$realoffset]->name ne $lemma_start ) {
601 warn( sprintf( "Should be replacing %s (%s) with %s (%s) " .
602 "but %s (%s) is there instead",
603 join( ' ', map {$_->label} @base_phrase ),
604 join( ' ', map {$_->name} @base_phrase ),
605 join( ' ', map {$_->label} @$items ),
606 join( ' ', map {$_->name} @$items ),
607 join( ' ', map {$_->label} @this_phrase ),
608 join( ' ', map {$_->name} @this_phrase ),
609 ) );
610 # next;
611 }
c78feb69 612 }
b15511bf 613 splice( @lemma_text, $realoffset, $length, @$items );
614 $drift += @$items - $length;
b49c4318 615 }
b15511bf 616 return @lemma_text;
b49c4318 617}
b15511bf 618
4ca00eca 619
e2902068 620# Helper function. Given a witness sigil, if it is a post-correctione
621# sigil,return the base witness. If not, return a false value.
622sub _is_post_corr {
623 my( $sigil ) = @_;
930ff666 624 if( $sigil =~ /^(.*?)(\s*\(?p\.\s*c\.\)?)$/ ) {
e2902068 625 return $1;
626 }
627 return undef;
628}
629
6a222840 630sub _add_hash_entry {
631 my( $hash, $key, $entry ) = @_;
632 if( exists $hash->{$key} ) {
633 push( @{$hash->{$key}}, $entry );
634 } else {
635 $hash->{$key} = [ $entry ];
636 }
637}
638
639
2ceca8c3 640=item B<cmp_str>
641
642Pretend you never saw this method. Really it needs to not be hardcoded.
643
644=cut
645
b49c4318 646sub cmp_str {
e2902068 647 my( $reading ) = @_;
648 my $word = $reading->label();
b49c4318 649 $word = lc( $word );
650 $word =~ s/\W//g;
651 $word =~ s/v/u/g;
652 $word =~ s/j/i/g;
653 $word =~ s/cha/ca/g;
654 $word =~ s/quatuor/quattuor/g;
655 $word =~ s/ioannes/iohannes/g;
656 return $word;
657}
658
2ceca8c3 659=back
660
661=head1 LICENSE
662
663This package is free software and is provided "as is" without express
664or implied warranty. You can redistribute it and/or modify it under
665the same terms as Perl itself.
666
667=head1 AUTHOR
668
669Tara L Andrews, aurum@cpan.org
670
671=cut
672
b49c4318 6731;