Commit | Line | Data |
e58153d6 |
1 | package Text::Tradition::Parser::BaseText; |
b49c4318 |
2 | |
3 | use strict; |
4 | use warnings; |
52ce987f |
5 | use Module::Load; |
4ca00eca |
6 | use Algorithm::Diff; |
b49c4318 |
7 | |
2ceca8c3 |
8 | =head1 NAME |
9 | |
10 | Text::Tradition::Parser::BaseText |
11 | |
12 | =head1 SYNOPSIS |
13 | |
14 | use Text::Tradition::Parser::BaseText qw( merge_base ); |
15 | merge_base( $graph, 'reference.txt', @apparatus_entries ) |
16 | |
17 | =head1 DESCRIPTION |
18 | |
19 | For an overview of the package, see the documentation for the |
20 | Text::Tradition::Graph module. |
21 | |
22 | This 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, |
24 | these must be joined into a single collation. The parser should |
25 | therefore make a list of variants and their locations, and BaseText |
26 | will join those listed variants onto the reference text. |
27 | |
28 | =head1 SUBROUTINES |
29 | |
30 | =over |
31 | |
52ce987f |
32 | =item B<parse> |
33 | |
34 | parse( $graph, %opts ); |
35 | |
36 | Takes 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 | |
43 | sub 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 | |
54 | merge_base( $graph, 'reference.txt', @apparatus_entries ) |
55 | |
56 | Takes three arguments: a newly-initialized Text::Tradition::Graph |
57 | object, a text file containing the reference text, and a list of |
58 | variants (apparatus entries). Adds the base text to the graph, and |
59 | joins the variants to that. |
60 | |
61 | The list of variants is an array of hash references; each hash takes |
62 | the 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 | |
72 | Any hash key that is not of the form /^rdg_\d+$/ and that does not |
73 | begin with an underscore is assumed to be a witness name. Any 'meta' |
74 | information to be passed must be passed in a key with a leading |
75 | underscore in its name. |
76 | |
77 | =cut |
78 | |
3265b0ce |
79 | my $SHORTEND = 20; # Debug var - set this to limit the number of lines parsed |
4ca00eca |
80 | |
81 | my %base_text_index; |
6a222840 |
82 | my $edits_required = {}; |
4ca00eca |
83 | |
84 | # edits_required -> wit -> [ { start_idx, end_idx, items } ] |
930ff666 |
85 | |
b49c4318 |
86 | sub 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 ); |
6a222840 |
179 | push( @unwitnessed_lemma_nodes, @lemma_set ) |
180 | if !@mss && $k eq 'rdg_0'; |
181 | |
e2902068 |
182 | # Keep track of what witnesses we have seen. |
52ce987f |
183 | @all_witnesses{ @mss } = ( 1 ) x scalar( @mss ); |
4ca00eca |
184 | # Keep track of which witnesses bear corrected readings here. |
185 | foreach my $m ( @mss ) { |
186 | my $base = _is_post_corr( $m ); |
187 | next unless $base; |
6a222840 |
188 | $pc_seen{$base} = 1; |
4ca00eca |
189 | } |
190 | next if $k eq 'rdg_0'; |
191 | |
192 | # TODO don't hardcode the reading split operation |
193 | my @variant = split( /\s+/, $app->{$k} ); |
194 | @variant = () if $app->{$k} eq '/'; # This is an omission. |
b49c4318 |
195 | |
e2902068 |
196 | # Make the variant into a set of readings. |
4ca00eca |
197 | my @variant_readings; |
b49c4318 |
198 | my $ctr = 0; |
b49c4318 |
199 | foreach my $vw ( @variant ) { |
200 | my $vwname = "$k/$line.$num.$ctr"; $ctr++; |
e2902068 |
201 | my $vwreading = $collation->add_reading( $vwname ); |
202 | $vwreading->text( $vw ); |
4ca00eca |
203 | push( @variant_readings, $vwreading ); |
b49c4318 |
204 | } |
e49731d7 |
205 | |
4ca00eca |
206 | $variant_objects->{$k} = { 'mss' => \@mss, |
207 | 'reading' => \@variant_readings, |
208 | }; |
209 | push( @reading_sets, \@variant_readings ); |
210 | } |
b49c4318 |
211 | |
4ca00eca |
212 | # Now collate and collapse the identical readings within the |
213 | # collated sets. Modifies the reading sets that were passed. |
214 | collate_variants( $collation, @reading_sets ); |
215 | |
15d2d3df |
216 | # TODO Here would be a very good place to set up relationships |
217 | # between the nodes and the lemma. |
3265b0ce |
218 | set_relationships( $collation, $app, \@lemma_set, $variant_objects ); |
15d2d3df |
219 | |
4ca00eca |
220 | # Now create the splice-edit objects that will be used |
221 | # to reconstruct each witness. |
222 | |
223 | foreach my $rkey ( keys %$variant_objects ) { |
224 | # Object is argument list for splice, so: |
225 | # offset, length, replacements |
c78feb69 |
226 | my $edit_object = [ $lemma_start->name, |
4ca00eca |
227 | scalar( @lemma_set ), |
228 | $variant_objects->{$rkey}->{reading} ]; |
229 | foreach my $ms ( @{$variant_objects->{$rkey}->{mss}} ) { |
230 | # Is this a p.c. entry? |
231 | my $base = _is_post_corr( $ms ); |
232 | if( $base ) { # this is a post-corr witness |
233 | my $pc_key = $base . "_post"; |
234 | _add_hash_entry( $edits_required, $pc_key, $edit_object ); |
235 | } else { # this is an ante-corr witness |
236 | my $pc_key = $ms . "_post"; |
6a222840 |
237 | _add_hash_entry( $edits_required, $ms, $edit_object ); |
238 | unless( $pc_seen{$ms} ) { |
239 | # If this witness carries no correction, add this |
240 | # same object to its post-corrected state. |
241 | _add_hash_entry( $edits_required, $pc_key, |
242 | $edit_object ); |
4ca00eca |
243 | } |
244 | } |
e2902068 |
245 | } |
4ca00eca |
246 | } |
247 | } # Finished going through the apparatus entries |
248 | |
249 | # Now make the witness objects, and create their text sequences |
6a222840 |
250 | foreach my $w ( grep { $_ !~ /_post$/ } keys %$edits_required ) { |
15d2d3df |
251 | print STDERR "Creating witness $w\n"; |
4ca00eca |
252 | my $witness_obj = $collation->tradition->add_witness( sigil => $w ); |
3265b0ce |
253 | my $debug = undef; # $w eq 'Vb10'; |
c78feb69 |
254 | my @ante_corr_seq = apply_edits( $collation, $edits_required->{$w}, $debug ); |
255 | my @post_corr_seq = apply_edits( $collation, $edits_required->{$w."_post"}, $debug ) |
4ca00eca |
256 | if exists( $edits_required->{$w."_post"} ); |
257 | |
15d2d3df |
258 | my @repeated = _check_for_repeated( @ante_corr_seq ); |
259 | warn "Repeated elements @repeated in $w a.c." |
260 | if @repeated; |
261 | @repeated = _check_for_repeated( @post_corr_seq ); |
262 | warn "Repeated elements @repeated in $w p.c." |
263 | if @repeated; |
264 | |
6a222840 |
265 | # Now save these paths in my witness object |
4ca00eca |
266 | if( @post_corr_seq ) { |
6a222840 |
267 | $witness_obj->path( \@post_corr_seq ); |
15d2d3df |
268 | $witness_obj->uncorrected_path( \@ante_corr_seq ); |
e2902068 |
269 | } else { |
6a222840 |
270 | $witness_obj->path( \@ante_corr_seq ); |
52ce987f |
271 | } |
b49c4318 |
272 | } |
e2902068 |
273 | |
6a222840 |
274 | # Now remove our 'base text' edges, which is to say, the only |
275 | # ones we have created so far. Also remove any nodes that didn't |
276 | # appear in any witnesses. |
277 | foreach ( $collation->paths() ) { |
278 | $collation->del_path( $_ ); |
279 | } |
280 | foreach( @unwitnessed_lemma_nodes ) { |
281 | $collation->del_reading( $_ ); |
282 | } |
4ca00eca |
283 | |
e2902068 |
284 | # Now walk paths and calculate positions. |
285 | my @common_readings = |
6a222840 |
286 | $collation->make_witness_paths(); |
e2902068 |
287 | $collation->calculate_positions( @common_readings ); |
b49c4318 |
288 | } |
289 | |
15d2d3df |
290 | sub _check_for_repeated { |
291 | my @seq = @_; |
292 | my %unique; |
293 | my @repeated; |
294 | foreach ( @seq ) { |
295 | if( exists $unique{$_->name} ) { |
296 | push( @repeated, $_->name ); |
297 | } else { |
298 | $unique{$_->name} = 1; |
299 | } |
300 | } |
301 | return @repeated; |
302 | } |
303 | |
2ceca8c3 |
304 | =item B<read_base> |
305 | |
e2902068 |
306 | my @line_beginnings = read_base( 'reference.txt', $collation ); |
2ceca8c3 |
307 | |
e2902068 |
308 | Takes a text file and a (presumed empty) collation object, adds the |
309 | words as simple linear readings to the collation, and returns a |
310 | list of readings that represent the beginning of lines. This collation |
311 | is now the starting point for application of apparatus entries in |
312 | merge_base, e.g. from a CSV file or a Classical Text Editor file. |
2ceca8c3 |
313 | |
314 | =cut |
b49c4318 |
315 | |
316 | sub read_base { |
e2902068 |
317 | my( $base_file, $collation ) = @_; |
b49c4318 |
318 | |
e2902068 |
319 | # This array gives the first reading for each line. We put the |
b49c4318 |
320 | # common starting point in line zero. |
e2902068 |
321 | my $last_reading = $collation->start(); |
6a222840 |
322 | $base_text_index{$last_reading->name} = 0; |
e2902068 |
323 | my $lineref_array = [ $last_reading ]; # There is no line zero. |
b49c4318 |
324 | |
325 | open( BASE, $base_file ) or die "Could not open file $base_file: $!"; |
6a222840 |
326 | my $i = 1; |
b49c4318 |
327 | while(<BASE>) { |
e2902068 |
328 | # Make the readings, and connect them up for the base, but |
329 | # also save the first reading of each line in an array for the |
330 | # purpose. |
331 | # TODO use configurable reading separator |
b49c4318 |
332 | chomp; |
333 | my @words = split; |
334 | my $started = 0; |
335 | my $wordref = 0; |
336 | my $lineref = scalar @$lineref_array; |
15d2d3df |
337 | last if $SHORTEND && $lineref > $SHORTEND; |
b49c4318 |
338 | foreach my $w ( @words ) { |
e2902068 |
339 | my $readingref = join( ',', $lineref, ++$wordref ); |
340 | my $reading = $collation->add_reading( $readingref ); |
341 | $reading->text( $w ); |
b49c4318 |
342 | unless( $started ) { |
e2902068 |
343 | push( @$lineref_array, $reading ); |
b49c4318 |
344 | $started = 1; |
345 | } |
4ca00eca |
346 | # Add edge paths in the graph, for easier tracking when |
347 | # we start applying corrections. These paths will be |
348 | # removed when we're done. |
349 | my $path = $collation->add_path( $last_reading, $reading, |
350 | $collation->baselabel ); |
351 | $last_reading = $reading; |
352 | |
353 | # Note an array index for the reading, for later correction splices. |
354 | $base_text_index{$readingref} = $i++; |
b49c4318 |
355 | } |
356 | } |
357 | close BASE; |
358 | # Ending point for all texts |
e2902068 |
359 | my $endpoint = $collation->add_reading( '#END#' ); |
930ff666 |
360 | $collation->add_path( $last_reading, $endpoint, $collation->baselabel ); |
b49c4318 |
361 | push( @$lineref_array, $endpoint ); |
6a222840 |
362 | $base_text_index{$endpoint->name} = $i; |
b49c4318 |
363 | |
364 | return( @$lineref_array ); |
365 | } |
366 | |
e49731d7 |
367 | =item B<collate_variants> |
2ceca8c3 |
368 | |
4ca00eca |
369 | collate_variants( $collation, @reading_ranges ) |
2ceca8c3 |
370 | |
e49731d7 |
371 | Given a set of readings in the form |
372 | ( lemma_start, lemma_end, rdg1_start, rdg1_end, ... ) |
e2902068 |
373 | walks through each to identify those readings that are identical. The |
374 | collation is a Text::Tradition::Collation object; the elements of |
375 | @readings are Text::Tradition::Collation::Reading objects that appear |
376 | on the collation graph. |
b49c4318 |
377 | |
2ceca8c3 |
378 | TODO: Handle collapsed and non-collapsed transpositions. |
379 | |
380 | =cut |
b49c4318 |
381 | |
e49731d7 |
382 | sub collate_variants { |
4ca00eca |
383 | my( $collation, @reading_sets ) = @_; |
4ca00eca |
384 | |
c78feb69 |
385 | # Two different ways to do this, depending on whether we want |
386 | # transposed reading nodes to be merged into one (producing a |
387 | # nonlinear, bidirectional graph) or not (producing a relatively |
388 | # linear, unidirectional graph.) |
389 | return $collation->linear ? collate_linearly( @_ ) |
390 | : collate_nonlinearly( @_ ); |
391 | } |
4ca00eca |
392 | |
c78feb69 |
393 | sub collate_linearly { |
394 | my( $collation, $lemma_set, @variant_sets ) = @_; |
4ca00eca |
395 | |
396 | my @unique; |
397 | push( @unique, @$lemma_set ); |
c78feb69 |
398 | while( @variant_sets ) { |
399 | my $variant_set = shift @variant_sets; |
400 | # Use diff to do this job |
401 | my $diff = Algorithm::Diff->new( \@unique, $variant_set, |
402 | {'keyGen' => \&_collation_hash} ); |
403 | my @new_unique; |
404 | my %merged; |
405 | while( $diff->Next ) { |
406 | if( $diff->Same ) { |
407 | # merge the nodes |
408 | my @l = $diff->Items( 1 ); |
409 | my @v = $diff->Items( 2 ); |
410 | foreach my $i ( 0 .. $#l ) { |
411 | if( !$merged{$l[$i]->name} ) { |
412 | $collation->merge_readings( $l[$i], $v[$i] ); |
413 | $merged{$l[$i]->name} = 1; |
414 | } else { |
415 | print STDERR "Would have double merged " . $l[$i]->name . "\n"; |
6a222840 |
416 | } |
e49731d7 |
417 | } |
c78feb69 |
418 | # splice the lemma nodes into the variant set |
419 | my( $offset ) = $diff->Get( 'min2' ); |
420 | splice( @$variant_set, $offset, scalar( @l ), @l ); |
421 | push( @new_unique, @l ); |
422 | } else { |
423 | # Keep the old unique readings |
424 | push( @new_unique, $diff->Items( 1 ) ) if $diff->Items( 1 ); |
425 | # Add the new readings to the 'unique' list |
426 | push( @new_unique, $diff->Items( 2 ) ) if $diff->Items( 2 ); |
b49c4318 |
427 | } |
c78feb69 |
428 | } |
429 | @unique = @new_unique; |
430 | } |
431 | } |
432 | |
433 | sub collate_nonlinearly { |
434 | my( $collation, $lemma_set, @variant_sets ) = @_; |
435 | |
436 | my @unique; |
437 | push( @unique, @$lemma_set ); |
438 | while( @variant_sets ) { |
439 | my $variant_set = shift @variant_sets; |
440 | # Simply match the first reading that carries the same word, so |
441 | # long as that reading has not yet been used to match another |
442 | # word in this variant. That way lies loopy madness. |
443 | my @distinct; |
444 | my %merged; |
445 | foreach my $idx ( 0 .. $#{$variant_set} ) { |
446 | my $vw = $variant_set->[$idx]; |
447 | my @same = grep { cmp_str( $_ ) eq $vw->label } @unique; |
448 | my $matched; |
449 | if( @same ) { |
450 | foreach my $i ( 0 .. $#same ) { |
451 | unless( $merged{$same[$i]->name} ) { |
452 | print STDERR sprintf( "Merging %s into %s\n", |
453 | $vw->name, |
454 | $same[$i]->name ); |
455 | $collation->merge_readings( $same[$i], $vw ); |
456 | $merged{$same[$i]->name} = 1; |
457 | $matched = $i; |
458 | $variant_set->[$idx] = $same[$i]; |
15d2d3df |
459 | } |
460 | } |
6a222840 |
461 | } |
c78feb69 |
462 | unless( @same && defined($matched) ) { |
463 | push( @distinct, $vw ); |
464 | } |
b49c4318 |
465 | } |
c78feb69 |
466 | push( @unique, @distinct ); |
b49c4318 |
467 | } |
4ca00eca |
468 | } |
2ceca8c3 |
469 | |
c78feb69 |
470 | |
4ca00eca |
471 | |
472 | sub _collation_hash { |
473 | my $node = shift; |
6a222840 |
474 | return cmp_str( $node ); |
4ca00eca |
475 | } |
2ceca8c3 |
476 | |
15d2d3df |
477 | sub set_relationships { |
3265b0ce |
478 | my( $collation, $app, $lemma, $variants ) = @_; |
15d2d3df |
479 | foreach my $rkey ( keys %$variants ) { |
480 | my $var = $variants->{$rkey}->{'reading'}; |
481 | my $typekey = sprintf( "_%s_type", $rkey ); |
482 | my $type = $app->{$typekey}; |
483 | |
051ddba3 |
484 | if( $type =~ /^(inv|tr|rep)$/i ) { |
485 | # Transposition or repetition: look for nodes with the |
486 | # same label but different IDs and mark them. |
487 | $type = 'repetition' if $type =~ /^rep/i; |
488 | $DB::single = 1 if $type eq 'repetition'; |
3265b0ce |
489 | my %labels; |
490 | foreach my $r ( @$lemma ) { |
051ddba3 |
491 | $labels{cmp_str( $r )} = $r; |
3265b0ce |
492 | } |
493 | foreach my $r( @$var ) { |
494 | if( exists $labels{$r->label} && |
495 | $r->name ne $labels{$r->label}->name ) { |
051ddba3 |
496 | if( $type eq 'repetition' ) { |
497 | # Repetition |
498 | $collation->add_relationship( $type, $r, $labels{$r->label} ); |
499 | } else { |
500 | # Transposition |
501 | $r->set_identical( $labels{$r->label} ); |
502 | } |
3265b0ce |
503 | } |
504 | } |
051ddba3 |
505 | } elsif( $type =~ /^(gr|sp(el)?)$/i ) { |
3265b0ce |
506 | # Grammar/spelling: this can be a one-to-one or one-to-many |
507 | # mapping. We should think about merging readings if it is |
508 | # one-to-many. |
509 | $type = 'grammatical' if $type =~ /gr/i; |
510 | $type = 'spelling' if $type =~ /sp/i; |
511 | $type = 'repetition' if $type =~ /rep/i; |
512 | if( @$lemma == @$var ) { |
513 | foreach my $i ( 0 .. $#{$lemma} ) { |
514 | $collation->add_relationship( $type, $var->[$i], |
515 | $lemma->[$i] ); |
516 | } |
517 | } elsif ( @$lemma > @$var && @$var == 1 ) { |
518 | # Merge the lemma readings into one |
051ddba3 |
519 | ## TODO This is a bad solution. We need a real one-to-many |
520 | ## mapping. |
3265b0ce |
521 | my $ln1 = shift @$lemma; |
522 | foreach my $ln ( @$lemma ) { |
523 | $collation->merge_readings( $ln1, $ln, ' ' ); |
524 | } |
525 | $lemma = [ $ln1 ]; |
526 | $collation->add_relationship( $type, $var->[0], $lemma->[0] ); |
527 | } elsif ( @$lemma < @$var && @$lemma == 1 ) { |
528 | my $vn1 = shift @$var; |
529 | foreach my $vn ( @$var ) { |
530 | $collation->merge_readings( $vn1, $vn, ' ' ); |
531 | } |
532 | $var = [ $vn1 ]; |
533 | $collation->add_relationship( $type, $var->[0], $lemma->[0] ); |
534 | } else { |
535 | warn "Cannot set $type relationship on a many-to-many variant"; |
536 | } |
537 | } elsif( $type !~ /^(lex|add|om)$/i ) { |
538 | warn "Unrecognized type $type"; |
539 | } |
15d2d3df |
540 | } |
541 | } |
542 | |
543 | |
544 | |
4ca00eca |
545 | sub apply_edits { |
c78feb69 |
546 | my( $collation, $edit_sequence, $debug ) = @_; |
547 | my @lemma_text = $collation->reading_sequence( $collation->start, |
548 | $collation->reading( '#END#' ) ); |
4ca00eca |
549 | my $drift = 0; |
550 | foreach my $correction ( @$edit_sequence ) { |
c78feb69 |
551 | my( $lemma_start, $length, $items ) = @$correction; |
552 | my $offset = $base_text_index{$lemma_start}; |
4ca00eca |
553 | my $realoffset = $offset + $drift; |
c78feb69 |
554 | if( $debug || |
555 | $lemma_text[$realoffset]->name ne $lemma_start ) { |
556 | my @this_phrase = @lemma_text[$realoffset..$realoffset+$length-1]; |
557 | my @base_phrase; |
558 | my $i = $realoffset; |
559 | my $l = $collation->reading( $lemma_start ); |
560 | while( $i < $realoffset+$length ) { |
561 | push( @base_phrase, $l ); |
562 | $l = $collation->next_reading( $l ); |
563 | $i++; |
564 | } |
565 | |
566 | print STDERR sprintf( "Trying to replace %s (%s) starting at %d " . |
567 | "with %s (%s) with drift %d\n", |
568 | join( ' ', map {$_->label} @base_phrase ), |
569 | join( ' ', map {$_->name} @base_phrase ), |
570 | $realoffset, |
571 | join( ' ', map {$_->label} @$items ), |
572 | join( ' ', map {$_->name} @$items ), |
573 | $drift, |
574 | ) if $debug; |
575 | |
576 | warn( sprintf( "Should be replacing %s (%s) with %s (%s) " . |
577 | "but %s (%s) is there instead", |
578 | join( ' ', map {$_->label} @base_phrase ), |
579 | join( ' ', map {$_->name} @base_phrase ), |
580 | join( ' ', map {$_->label} @$items ), |
581 | join( ' ', map {$_->name} @$items ), |
582 | join( ' ', map {$_->label} @this_phrase ), |
583 | join( ' ', map {$_->name} @this_phrase ), |
584 | ) ) |
585 | if $lemma_text[$realoffset]->name ne $lemma_start; |
586 | } |
4ca00eca |
587 | splice( @lemma_text, $realoffset, $length, @$items ); |
588 | $drift += @$items - $length; |
b49c4318 |
589 | } |
6a222840 |
590 | return @lemma_text; |
b49c4318 |
591 | } |
6a222840 |
592 | |
4ca00eca |
593 | |
e2902068 |
594 | # Helper function. Given a witness sigil, if it is a post-correctione |
595 | # sigil,return the base witness. If not, return a false value. |
596 | sub _is_post_corr { |
597 | my( $sigil ) = @_; |
930ff666 |
598 | if( $sigil =~ /^(.*?)(\s*\(?p\.\s*c\.\)?)$/ ) { |
e2902068 |
599 | return $1; |
600 | } |
601 | return undef; |
602 | } |
603 | |
6a222840 |
604 | sub _add_hash_entry { |
605 | my( $hash, $key, $entry ) = @_; |
606 | if( exists $hash->{$key} ) { |
607 | push( @{$hash->{$key}}, $entry ); |
608 | } else { |
609 | $hash->{$key} = [ $entry ]; |
610 | } |
611 | } |
612 | |
613 | |
2ceca8c3 |
614 | =item B<cmp_str> |
615 | |
616 | Pretend you never saw this method. Really it needs to not be hardcoded. |
617 | |
618 | =cut |
619 | |
b49c4318 |
620 | sub cmp_str { |
e2902068 |
621 | my( $reading ) = @_; |
622 | my $word = $reading->label(); |
b49c4318 |
623 | $word = lc( $word ); |
624 | $word =~ s/\W//g; |
625 | $word =~ s/v/u/g; |
626 | $word =~ s/j/i/g; |
627 | $word =~ s/cha/ca/g; |
628 | $word =~ s/quatuor/quattuor/g; |
629 | $word =~ s/ioannes/iohannes/g; |
630 | return $word; |
631 | } |
632 | |
2ceca8c3 |
633 | =back |
634 | |
635 | =head1 LICENSE |
636 | |
637 | This package is free software and is provided "as is" without express |
638 | or implied warranty. You can redistribute it and/or modify it under |
639 | the same terms as Perl itself. |
640 | |
641 | =head1 AUTHOR |
642 | |
643 | Tara L Andrews, aurum@cpan.org |
644 | |
645 | =cut |
646 | |
b49c4318 |
647 | 1; |