9 use Text::Tradition::Analysis qw/ run_analysis /;
11 binmode STDOUT, ':utf8';
12 binmode STDERR, ':utf8';
13 eval { no warnings; binmode $DB::OUT, ':utf8'; $DB::deep = 1000 };
15 my $tradition = Text::Tradition->new(
17 'file' => 't/data/besoin.xml' );
18 $tradition->add_stemma( 'dotfile' => 't/data/besoin.dot' );
20 # Run the analysis of the tradition
21 ## TODO Make proper test db
22 my $calcdsn = 'dbi:SQLite:dbname=t/data/analysis.db';
41 155 => 'genealogical',
42 170 => 'genealogical',
44 205 => 'genealogical',
45 219 => 'genealogical',
46 239 => 'genealogical',
47 244 => 'genealogical',
49 251 => 'genealogical',
50 252 => 'genealogical',
51 293 => 'genealogical',
52 295 => 'genealogical',
53 309 => 'genealogical',
58 318 => 'genealogical',
59 319 => 'genealogical',
63 335 => 'genealogical',
70 406 => 'genealogical',
71 413 => 'genealogical',
75 493 => 'genealogical',
85 632 => 'genealogical',
86 634 => 'genealogical',
87 636 => 'genealogical',
88 685 => 'genealogical',
89 737 => 'genealogical',
100 780 => 'genealogical',
104 837 => 'genealogical',
161 925 => 'genealogical',
163 952 => 'genealogical',
164 954 => 'genealogical',
165 969 => 'genealogical',
166 972 => 'genealogical',
167 973 => 'genealogical',
169 1003 => 'genealogical',
170 1004 => 'genealogical' # check for transp
175 my @all_variant_ranks = sort { $a <=> $b } keys( %expected );
176 # Look through the results
177 my $c = $tradition->collation;
178 my %analysis_opts = ( calcdsn => $calcdsn );
179 my $results = run_analysis( $tradition, %analysis_opts );
181 foreach my $row ( @{$results->{'variants'}} ) {
182 push( @analyzed, $row->{id} );
183 $num_readings{$row->{id}} = scalar @{$row->{'readings'}};
184 my $type = 'genealogical';
185 if( grep { $_->{'is_conflict'} } @{$row->{'readings'}} ) {
187 } elsif( grep { $_->{'is_reverted'} } @{$row->{'readings'}} ) {
190 my $expected = $expected{$row->{'id'}};
191 $expected = 'genealogical' if $expected eq 'type1';
192 is( $type, $expected, "Got expected genealogical result for rank " . $row->{'id'} );
193 # If the row is genealogical, there should be one reading with no parents,
194 # every reading should independently occur exactly once, and the total
195 # number of changes + maybe-changes should equal the total number of
196 # readings who have that one as a parent.
197 if( $row->{'genealogical'} ) {
198 # Make the mapping of parent -> child readings
201 foreach my $rdg ( @{$row->{'readings'}} ) {
202 my $parents = $rdg->{'source_parents'} || {};
203 foreach my $p ( keys %$parents ) {
204 push( @{$is_parent{$p}}, $rdg->{'readingid'} );
206 push( @has_no_parent, $rdg->{'readingid'} ) unless keys %$parents;
209 foreach my $rdg ( @{$row->{'readings'}} ) {
210 is( @{$rdg->{'independent_occurrence'}}, 1,
211 "Genealogical reading originates exactly once" );
213 is( @has_no_parent, 1, "Only one genealogical reading lacks a parent" );
216 # Check that run_analysis ran an analysis on all our known variant ranks
217 is_deeply( \@all_variant_ranks, \@analyzed, "Ran analysis for all expected rows" );
219 # Now run it again, excluding type 1 variants.
220 map { delete $expected{$_} if $expected{$_} eq 'type1' } keys %expected;
221 my @useful_variant_ranks = sort { $a <=> $b } keys( %expected );
222 $analysis_opts{'exclude_type1'} = 1;
224 $results = run_analysis( $tradition, %analysis_opts );
225 foreach my $row ( @{$results->{'variants'}} ) {
226 push( @analyzed, $row->{id} );
227 my $type = 'genealogical';
228 if( grep { $_->{'is_conflict'} } @{$row->{'readings'}} ) {
230 } elsif( grep { $_->{'is_reverted'} } @{$row->{'readings'}} ) {
233 is( $type, $expected{$row->{'id'}}, "Got expected genealogical result on exclude_type1 run for rank " . $row->{'id'} );
235 is_deeply( \@analyzed, \@useful_variant_ranks, "Ran analysis for all useful rows" );
237 # Now run it again, excluding orthography / spelling.
238 my @merged_exclude = qw/ 76 136 142 155 293 317 319 335 350 361 413 500 515 636 685
240 # 205 now conflicts; it and 493 should also have one fewer reading
241 $expected{205} = 'conflict';
242 $num_readings{205}--;
243 $num_readings{493}--;
244 map { delete $expected{$_} } @merged_exclude;
245 my @merged_remaining = sort { $a <=> $b } keys( %expected );
246 $analysis_opts{'merge_types'} = [ qw/ orthographic spelling / ];
248 $results = run_analysis( $tradition, %analysis_opts );
249 foreach my $row ( @{$results->{'variants'}} ) {
250 push( @analyzed, $row->{id} );
251 my $type = 'genealogical';
252 if( grep { $_->{'is_conflict'} } @{$row->{'readings'}} ) {
254 } elsif( grep { $_->{'is_reverted'} } @{$row->{'readings'}} ) {
257 is( $type, $expected{$row->{'id'}}, "Got expected genealogical result on merge_types run for rank " . $row->{'id'} );
258 is( scalar @{$row->{'readings'}}, $num_readings{$row->{id}}, "Got expected number of readings during merge" );
260 is_deeply( \@analyzed, \@merged_remaining, "Ran analysis for all useful unmerged rows" );