1 package Text::Tradition::Parser::Util;
7 use vars qw/ @EXPORT_OK /;
8 @EXPORT_OK = qw/ add_hash_entry check_for_repeated cmp_str collate_variants is_monotonic /;
12 Text::Tradition::Parser::Util
16 A collection of utilities used by multiple Text::Tradition parsers.
17 Probably not of external interest.
21 =head2 B<collate_variants>( $collation, @reading_ranges )
23 Given a set of readings in the form
24 ( lemma_start, lemma_end, rdg1_start, rdg1_end, ... )
25 walks through each to identify those readings that are identical. The
26 collation is a Text::Tradition::Collation object; the elements of
27 @readings are Text::Tradition::Collation::Reading objects that appear
28 on the collation graph.
30 TODO: Handle collapsed and non-collapsed transpositions.
34 sub collate_variants {
35 my( $collation, @reading_sets ) = @_;
37 # Two different ways to do this, depending on whether we want
38 # transposed reading nodes to be merged into one (producing a
39 # nonlinear, bidirectional graph) or not (producing a relatively
40 # linear, unidirectional graph.)
41 return $collation->linear ? collate_linearly( @_ )
42 : collate_nonlinearly( @_ );
45 sub collate_linearly {
46 my( $collation, $lemma_set, @variant_sets ) = @_;
49 my $substitutions = {};
50 push( @unique, @$lemma_set );
51 while( @variant_sets ) {
52 my $variant_set = shift @variant_sets;
53 # Use diff to do this job
54 my $diff = Algorithm::Diff->new( \@unique, $variant_set,
55 {'keyGen' => \&_collation_hash} );
58 while( $diff->Next ) {
61 my @l = $diff->Items( 1 );
62 my @v = $diff->Items( 2 );
63 foreach my $i ( 0 .. $#l ) {
64 if( !$merged{$l[$i]->name} ) {
65 print STDERR sprintf( "Merging %s into %s\n",
68 $collation->merge_readings( $l[$i], $v[$i] );
69 $merged{$l[$i]->name} = 1;
70 $substitutions->{$v[$i]->name} = $l[$i];
72 print STDERR "Would have double merged " . $l[$i]->name . "\n";
75 # splice the lemma nodes into the variant set
76 my( $offset ) = $diff->Get( 'min2' );
77 splice( @$variant_set, $offset, scalar( @l ), @l );
78 push( @new_unique, @l );
80 # Keep the old unique readings
81 push( @new_unique, $diff->Items( 1 ) ) if $diff->Items( 1 );
82 # Add the new readings to the 'unique' list
83 push( @new_unique, $diff->Items( 2 ) ) if $diff->Items( 2 );
86 @unique = @new_unique;
88 return $substitutions;
91 sub collate_nonlinearly {
92 my( $collation, $lemma_set, @variant_sets ) = @_;
95 my $substitutions = {};
96 push( @unique, @$lemma_set );
97 while( @variant_sets ) {
98 my $variant_set = shift @variant_sets;
99 # Simply match the first reading that carries the same word, so
100 # long as that reading has not yet been used to match another
101 # word in this variant. That way lies loopy madness.
104 foreach my $idx ( 0 .. $#{$variant_set} ) {
105 my $vw = $variant_set->[$idx];
106 my @same = grep { cmp_str( $_ ) eq $vw->label } @unique;
109 foreach my $i ( 0 .. $#same ) {
110 unless( $merged{$same[$i]->name} ) {
111 #print STDERR sprintf( "Merging %s into %s\n",
114 $collation->merge_readings( $same[$i], $vw );
115 $merged{$same[$i]->name} = 1;
117 $variant_set->[$idx] = $same[$i];
118 $substitutions->{$vw->name} = $same[$i];
122 unless( @same && defined($matched) ) {
123 push( @distinct, $vw );
126 push( @unique, @distinct );
128 return $substitutions;
131 sub _collation_hash {
133 return cmp_str( $node );
138 my $word = $reading->label();
144 $word =~ s/quatuor/quattuor/g;
145 $word =~ s/ioannes/iohannes/g;
149 =head2 B<check_for_repeated>( @readings )
151 Given an array of items, returns any items that appear in the array more
156 sub check_for_repeated {
161 if( exists $unique{$_->name} ) {
162 push( @repeated, $_->name );
164 $unique{$_->name} = 1;
171 my( $hash, $key, $entry ) = @_;
172 if( exists $hash->{$key} ) {
173 push( @{$hash->{$key}}, $entry );
175 $hash->{$key} = [ $entry ];
180 my( @readings ) = @_;
181 my( $common, $min, $max ) = ( -1, -1, -1 );
182 foreach my $rdg ( @readings ) {
183 # print STDERR "Checking reading " . $rdg->name . "/" . $rdg->text . " - "
184 # . $rdg->position->reference ."\n";
185 return 0 if $rdg->position->common < $common;
186 if( $rdg->position->common == $common ) {
187 return 0 if $rdg->position->min <= $min;
188 return 0 if $rdg->position->max <= $max;
190 $common = $rdg->position->common;
191 $min = $rdg->position->min;
192 $max = $rdg->position->max;
203 =item * Get rid of abomination that is cmp_str.
209 This package is free software and is provided "as is" without express
210 or implied warranty. You can redistribute it and/or modify it under
211 the same terms as Perl itself.
215 Tara L Andrews E<lt>aurum@cpan.orgE<gt>