keep analysis scripts out of CPAN; make CHANGES file; increment versions for release
[scpubgit/stemmatology.git] / analysis / lib / Text / Tradition / Analysis.pm
CommitLineData
d71100ed 1package Text::Tradition::Analysis;
2
3use strict;
4use warnings;
428bcf0b 5use Algorithm::Diff; # for word similarity measure
03c0a7d7 6use Encode qw/ decode_utf8 encode_utf8 /;
d1348d38 7use Exporter 'import';
b4cb2d60 8use Graph;
a745c3d9 9use JSON qw/ to_json decode_json /;
03c0a7d7 10use LWP::UserAgent;
7e17346f 11use Set::Scalar;
7e17346f 12use Text::Tradition::Analysis::Result;
d71100ed 13use Text::Tradition::Stemma;
d120c995 14use TryCatch;
d71100ed 15
951ddfe8 16use vars qw/ @EXPORT_OK $VERSION /;
a2cf85dd 17@EXPORT_OK = qw/ run_analysis group_variants analyze_variant_location wit_stringify /;
38b22fbf 18$VERSION = "1.2";
951ddfe8 19
d1348d38 20
03c0a7d7 21my $SOLVER_URL = 'http://byzantini.st/cgi-bin/graphcalc.cgi';
7e17346f 22my $unsolved_problems = {};
5c44c598 23
7f52eac8 24=head1 NAME
25
26Text::Tradition::Analysis - functions for stemma analysis of a tradition
27
8943ff68 28=head1 DESCRIPTION
29
30Text::Tradition is a library for representation and analysis of collated
31texts, particularly medieval ones. Where the Collation is the central
32feature of a Tradition, it may also have one or more stemmata associated
33with it, and these stemmata may be analyzed. This package provides the
34following modules:
35
36=over 4
37
38=item * L<Text::Tradition::HasStemma> - a role that will be composed into
39Text::Tradition objects, providing the ability for Text::Tradition::Stemma
40objects to be associated with them.
41
42=item * L<Text::Tradition::Stemma> - an object class that represents stemma
43hypotheses, both rooted (with a single archetype) and unrooted (e.g.
44phylogenetic trees).
45
46=item * Text::Tradition::Analysis (this package). Provides functions for
47the analysis of a given stemma against the collation within a given
48Tradition.
49
50=back
51
7f52eac8 52=head1 SYNOPSIS
53
54 use Text::Tradition;
55 use Text::Tradition::Analysis qw/ run_analysis analyze_variant_location /;
56 my $t = Text::Tradition->new(
57 'name' => 'this is a text',
58 'input' => 'TEI',
59 'file' => '/path/to/tei_parallel_seg_file.xml' );
60 $t->add_stemma( 'dotfile' => $stemmafile );
61
62 my $variant_data = run_analysis( $tradition );
7f52eac8 63
7f52eac8 64=head1 SUBROUTINES
65
88a6bac5 66=head2 run_analysis( $tradition, %opts )
7f52eac8 67
88a6bac5 68Runs the analysis described in analyze_variant_location on every location in the
69collation of the given tradition, with the given options. These include:
7f52eac8 70
88a6bac5 71=over 4
72
73=item * stemma_id - Specify which of the tradition's stemmata to use. Default
74is 0 (i.e. the first).
75
76=item * ranks - Specify a list of location ranks to analyze; exclude the rest.
77
78=item * merge_types - Specify a list of relationship types, where related readings
79should be treated as identical for the purposes of analysis.
80
ffa22d6f 81=item * exclude_type1 - Exclude those ranks whose groupings have only type-1 variants.
82
88a6bac5 83=back
7f52eac8 84
85=begin testing
86
87use Text::Tradition;
88use Text::Tradition::Analysis qw/ run_analysis analyze_variant_location /;
89
90my $datafile = 't/data/florilegium_tei_ps.xml';
91my $tradition = Text::Tradition->new( 'input' => 'TEI',
92 'name' => 'test0',
93 'file' => $datafile );
94my $s = $tradition->add_stemma( 'dotfile' => 't/data/florilegium.dot' );
95is( ref( $s ), 'Text::Tradition::Stemma', "Added stemma to tradition" );
96
f00cefe8 97my %expected_genealogical = (
a44aaf2a 98 1 => 0,
f00cefe8 99 2 => 1,
a44aaf2a 100 3 => 0,
101 5 => 0,
102 7 => 0,
103 8 => 0,
104 10 => 0,
f00cefe8 105 13 => 1,
a44aaf2a 106 33 => 0,
107 34 => 0,
108 37 => 0,
109 60 => 0,
f00cefe8 110 81 => 1,
a44aaf2a 111 84 => 0,
112 87 => 0,
113 101 => 0,
114 102 => 0,
f00cefe8 115 122 => 1,
a44aaf2a 116 157 => 0,
f00cefe8 117 166 => 1,
118 169 => 1,
a44aaf2a 119 200 => 0,
f00cefe8 120 216 => 1,
121 217 => 1,
122 219 => 1,
123 241 => 1,
124 242 => 1,
125 243 => 1,
126);
127
7e17346f 128my $data = run_analysis( $tradition, calcdsn => 'dbi:SQLite:dbname=t/data/analysis.db' );
7234b01d 129my $c = $tradition->collation;
f00cefe8 130foreach my $row ( @{$data->{'variants'}} ) {
a44aaf2a 131 # Account for rows that used to be "not useful"
132 unless( exists $expected_genealogical{$row->{'id'}} ) {
133 $expected_genealogical{$row->{'id'}} = 1;
134 }
18f48b82 135 my $gen_bool = $row->{'genealogical'} ? 1 : 0;
136 is( $gen_bool, $expected_genealogical{$row->{'id'}},
f00cefe8 137 "Got correct genealogical flag for row " . $row->{'id'} );
7234b01d 138 # Check that we have the right row with the right groups
139 my $rank = $row->{'id'};
140 foreach my $rdghash ( @{$row->{'readings'}} ) {
141 # Skip 'readings' that aren't really
142 next unless $c->reading( $rdghash->{'readingid'} );
143 # Check the rank
144 is( $c->reading( $rdghash->{'readingid'} )->rank, $rank,
145 "Got correct reading rank" );
146 # Check the witnesses
147 my @realwits = sort $c->reading_witnesses( $rdghash->{'readingid'} );
148 my @sgrp = sort @{$rdghash->{'group'}};
149 is_deeply( \@sgrp, \@realwits, "Reading analyzed with correct groups" );
150 }
f00cefe8 151}
a44aaf2a 152is( $data->{'variant_count'}, 58, "Got right total variant number" );
b4cb2d60 153# TODO Make something meaningful of conflict count, maybe test other bits
7f52eac8 154
155=end testing
156
157=cut
158
d71100ed 159sub run_analysis {
88a6bac5 160 my( $tradition, %opts ) = @_;
f00cefe8 161 my $c = $tradition->collation;
7e17346f 162 my $aclabel = $c->ac_label;
88a6bac5 163
164 my $stemma_id = $opts{'stemma_id'} || 0;
1d73ecad 165 my @ranks = ref( $opts{'ranks'} ) eq 'ARRAY' ? @{$opts{'ranks'}} : ();
e23225e7 166 my $collapse = Set::Scalar->new();
167 if( $opts{'merge_types'} && ref( $opts{'merge_types'} ) eq 'ARRAY' ) {
168 $collapse->insert( @{$opts{'merge_types'}} );
169 } elsif( $opts{'merge_types'} ) {
170 $collapse->insert( $opts{'merge_types'} );
171 }
7e17346f 172
173 # Make sure we have a lookup DB for graph calculation results; this will die
174 # if calcdir or calcdsn isn't passed.
03c0a7d7 175 my $dir;
176 if( exists $opts{'calcdir'} ) {
177 $dir = delete $opts{'calcdir'}
178 } elsif ( exists $opts{'calcdsn'} ) {
8943ff68 179 eval { require Text::Tradition::Directory };
180 if( $@ ) {
181 throw( "Could not instantiate a directory for " . $opts{'calcdsn'}
182 . ": $@" );
183 }
03c0a7d7 184 $dir = Text::Tradition::Directory->new( dsn => $opts{'calcdsn'} );
185 }
88a6bac5 186
187 # Get the stemma
188 my $stemma = $tradition->stemma( $stemma_id );
b4cb2d60 189
4ce27d42 190 # Figure out which witnesses we are working with - that is, the ones that
191 # appear both in the stemma and in the tradition. All others are 'lacunose'
192 # for our purposes.
7e17346f 193 my $lacunose = Set::Scalar->new( $stemma->hypotheticals );
194 my $stemma_wits = Set::Scalar->new( $stemma->witnesses );
195 my $tradition_wits = Set::Scalar->new( map { $_->sigil } $tradition->witnesses );
196 $lacunose->insert( $stemma_wits->symmetric_difference( $tradition_wits )->members );
88a6bac5 197
198 # Find and mark 'common' ranks for exclusion, unless they were
199 # explicitly specified.
200 unless( @ranks ) {
201 my %common_rank;
a44aaf2a 202 foreach my $rdg ( $c->common_readings ) {
88a6bac5 203 $common_rank{$rdg->rank} = 1;
204 }
205 @ranks = grep { !$common_rank{$_} } ( 1 .. $c->end->rank-1 );
d71100ed 206 }
7f52eac8 207
88a6bac5 208 # Group the variants to send to the solver
209 my @groups;
f629cb3b 210 my @use_ranks;
a44aaf2a 211 my %lacunae;
94654e27 212 my $moved = {};
88a6bac5 213 foreach my $rank ( @ranks ) {
7e17346f 214 my $missing = $lacunose->clone();
e23225e7 215 my $rankgroup = group_variants( $tradition, $rank, $missing, $moved, $collapse );
94654e27 216 # Filter out any empty rankgroups
217 # (e.g. from the later rank for a transposition)
218 next unless keys %$rankgroup;
7e17346f 219 # Get the graph for this rankgroup
220 my $rankgraph = _graph_for_grouping( $stemma, $rankgroup, $missing, $aclabel );
ffa22d6f 221 if( $opts{'exclude_type1'} ) {
222 # Check to see whether this is a "useful" group.
7e17346f 223 next unless _useful_variant( $rankgroup, $rankgraph, $aclabel );
ffa22d6f 224 }
f629cb3b 225 push( @use_ranks, $rank );
7e17346f 226 push( @groups, { grouping => $rankgroup, graph => $rankgraph } );
a44aaf2a 227 $lacunae{$rank} = $missing;
d71100ed 228 }
4ce27d42 229 # Run the solver
b7bd7aa5 230 my $answer;
231 try {
232 $answer = solve_variants( $dir, @groups );
233 } catch ( Text::Tradition::Error $e ) {
234 if( $e->message =~ /IDP/ ) {
235 # Something is wrong with the solver; make the variants table anyway
236 $answer->{'variants'} = [];
237 map { push( @{$answer->{'variants'}}, _init_unsolved( $_, 'IDP error' ) ) }
238 @groups;
239 } else {
240 # Something else is wrong; error out.
241 $e->throw;
242 }
243 }
fae07016 244
88a6bac5 245 # Do further analysis on the answer
a44aaf2a 246 my $conflict_count = 0;
7e17346f 247 my $reversion_count = 0;
f629cb3b 248 foreach my $idx ( 0 .. $#use_ranks ) {
88a6bac5 249 my $location = $answer->{'variants'}->[$idx];
250 # Add the rank back in
94654e27 251 my $rank = $use_ranks[$idx];
252 $location->{'id'} = $rank;
7234b01d 253 # Note what our lacunae are
f629cb3b 254 my %lmiss;
7234b01d 255 map { $lmiss{$_} = 1 } @{$lacunae{$use_ranks[$idx]}};
7234b01d 256 $location->{'missing'} = [ keys %lmiss ];
257
88a6bac5 258 # Run the extra analysis we need.
428bcf0b 259 ## TODO We run through all the variants in this call, so
260 ## why not add the reading data there instead of here below?
7e17346f 261 my $graph = $groups[$idx]->{graph};
262 analyze_location( $tradition, $graph, $location, \%lmiss );
7234b01d 263
638e2a95 264 my @layerwits;
7234b01d 265 # Do the final post-analysis tidying up of the data.
a44aaf2a 266 foreach my $rdghash ( @{$location->{'readings'}} ) {
e23225e7 267 $conflict_count++ if $rdghash->{'is_conflict'};
268 $reversion_count++ if $rdghash->{'is_reverted'};
94654e27 269 # Add the reading text back in, setting display value as needed
a44aaf2a 270 my $rdg = $c->reading( $rdghash->{'readingid'} );
94654e27 271 if( $rdg ) {
272 $rdghash->{'text'} = $rdg->text .
273 ( $rdg->rank == $rank ? '' : ' [' . $rdg->rank . ']' );
a445ce40 274 if( $rdg->does( 'Text::Tradition::Morphology' ) ) {
275 $rdghash->{'is_ungrammatical'} = $rdg->grammar_invalid;
276 $rdghash->{'is_nonsense'} = $rdg->is_nonsense;
277 }
94654e27 278 }
f629cb3b 279 # Remove lacunose witnesses from this reading's list now that the
7234b01d 280 # analysis is done
f629cb3b 281 my @realgroup;
7234b01d 282 map { push( @realgroup, $_ ) unless $lmiss{$_} } @{$rdghash->{'group'}};
f629cb3b 283 $rdghash->{'group'} = \@realgroup;
638e2a95 284 # Note any layered witnesses that appear in this group
285 foreach( @realgroup ) {
286 if( $_ =~ /^(.*)\Q$aclabel\E$/ ) {
287 push( @layerwits, $1 );
288 }
289 }
a44aaf2a 290 }
638e2a95 291 $location->{'layerwits'} = \@layerwits if @layerwits;
88a6bac5 292 }
a44aaf2a 293 $answer->{'conflict_count'} = $conflict_count;
7e17346f 294 $answer->{'reversion_count'} = $reversion_count;
f00cefe8 295
88a6bac5 296 return $answer;
d71100ed 297}
298
e23225e7 299=head2 group_variants( $tradition, $rank, $lacunose, $transposed, $merge_relationship_types )
7f52eac8 300
301Groups the variants at the given $rank of the collation, treating any
e23225e7 302relationships in the set $merge_relationship_types as equivalent.
303$lacunose should be a reference to an array, to which the sigla of lacunose
304witnesses at this rank will be appended; $transposed should be a reference
305to a hash, wherein the identities of transposed readings and their
306relatives will be stored.
7f52eac8 307
ffa22d6f 308Returns a hash $group_readings where $rdg is attested by the witnesses listed
309in $group_readings->{$rdg}.
7f52eac8 310
311=cut
312
313# Return group_readings, groups, lacunose
d1348d38 314sub group_variants {
94654e27 315 my( $tradition, $rank, $lacunose, $transposed, $collapse ) = @_;
7f52eac8 316 my $c = $tradition->collation;
335a62ef 317 my $aclabel = $c->ac_label;
d120c995 318 my $table = $c->alignment_table;
7f52eac8 319 # Get the alignment table readings
320 my %readings_at_rank;
7e17346f 321 my $check_for_gaps = Set::Scalar->new();
94654e27 322 my %moved_wits;
d120c995 323 my $has_transposition;
608bbd95 324 my @transp_acgap;
d120c995 325 foreach my $tablewit ( @{$table->{'alignment'}} ) {
7f52eac8 326 my $rdg = $tablewit->{'tokens'}->[$rank-1];
fae07016 327 my $wit = $tablewit->{'witness'};
ffa22d6f 328 # Exclude the witness if it is "lacunose" which if we got here
329 # means "not in the stemma".
7e17346f 330 next if _is_lacunose( $wit, $lacunose, $aclabel );
94654e27 331 # Note if the witness is actually in a lacuna
7f52eac8 332 if( $rdg && $rdg->{'t'}->is_lacuna ) {
335a62ef 333 _add_to_witlist( $wit, $lacunose, $aclabel );
94654e27 334 # Otherwise the witness either has a positive reading...
7f52eac8 335 } elsif( $rdg ) {
94654e27 336 # If the reading has been counted elsewhere as a transposition, ignore it.
337 if( $transposed->{$rdg->{'t'}->id} ) {
d120c995 338 # TODO Does this cope with three-way transpositions?
94654e27 339 map { $moved_wits{$_} = 1 } @{$transposed->{$rdg->{'t'}->id}};
340 next;
341 }
342 # Otherwise, record it...
343 $readings_at_rank{$rdg->{'t'}->id} = $rdg->{'t'};
344 # ...and grab any transpositions, and their relations.
ee26c4d9 345 my @transp = grep { $_->rank != $rank } _all_related( $rdg->{'t'} );
94654e27 346 foreach my $trdg ( @transp ) {
d120c995 347 next if exists $readings_at_rank{$trdg->id};
348 $has_transposition = 1;
349 my @affected_wits = _table_witnesses(
608bbd95 350 $table, $trdg->rank, $trdg, $lacunose, $aclabel );
d120c995 351 next unless @affected_wits;
352 map { $moved_wits{$_} = 1 } @affected_wits;
608bbd95 353 my @thisloc_wits = _table_witnesses( $table, $rank, $rdg->{'t'},
f9595796 354 $lacunose, $aclabel );
355 # Check to see if our affected wits have layers that do something
356 # wacky.
357 my %transploc_gaps;
358 map { $transploc_gaps{$_} = 1 }
608bbd95 359 _table_witnesses( $table, $trdg->rank, undef, $lacunose, $aclabel );
f9595796 360 foreach my $aw ( @affected_wits ) {
608bbd95 361 if( $transploc_gaps{$aw.$aclabel} ) {
362 push( @thisloc_wits, $aw.$aclabel );
363 push( @transp_acgap, $aw.$aclabel );
364 }
f9595796 365 }
366 # Record which witnesses we should count as already analyzed when we
367 # get to the transposed reading's own rank.
368 $transposed->{$trdg->id} = \@thisloc_wits;
94654e27 369 $readings_at_rank{$trdg->id} = $trdg;
370 }
371 # ...or it is empty, ergo a gap.
7f52eac8 372 } else {
7e17346f 373 _add_to_witlist( $wit, $check_for_gaps, $aclabel );
7f52eac8 374 }
375 }
608bbd95 376 # Push all the transposition layer gaps onto our list
377 $check_for_gaps->insert( @transp_acgap );
378 # Now remove from our 'gaps' any witnesses known to have been dealt with elsewhere.
7e17346f 379 my $gap_wits = Set::Scalar->new();
380 map { _add_to_witlist( $_, $gap_wits, $aclabel )
381 unless $moved_wits{$_} } $check_for_gaps->members;
382
383 # Group the readings, collapsing groups by relationship if needed.
d120c995 384 my $grouped_readings = {};
4ce27d42 385 foreach my $rdg ( values %readings_at_rank ) {
7f52eac8 386 # Skip readings that have been collapsed into others.
d120c995 387 next if exists $grouped_readings->{$rdg->id}
388 && $grouped_readings->{$rdg->id} eq 'COLLAPSE';
4ce27d42 389 # Get the witness list, including from readings collapsed into this one.
608bbd95 390 my @wits = _table_witnesses( $table, $rdg->rank, $rdg, $lacunose, $aclabel );
e23225e7 391 if( $collapse && $collapse->size ) {
392 my $filter = sub { $collapse->has( $_[0]->type ) };
7f52eac8 393 foreach my $other ( $rdg->related_readings( $filter ) ) {
608bbd95 394 my @otherwits = _table_witnesses( $table, $other->rank, $other, $lacunose, $aclabel );
fae07016 395 push( @wits, @otherwits );
d120c995 396 $grouped_readings->{$other->id} = 'COLLAPSE';
d1348d38 397 }
398 }
7e17346f 399 $grouped_readings->{$rdg->id} = Set::Scalar->new( @wits );
7f52eac8 400 }
7e17346f 401 if( $gap_wits->members ) {
402 $grouped_readings->{'(omitted)'} = $gap_wits;
403 }
404
7f52eac8 405 # Get rid of our collapsed readings
7e17346f 406 map { delete $grouped_readings->{$_} if(
407 $grouped_readings->{$_} eq 'COLLAPSE'
408 || $grouped_readings->{$_}->is_empty ) }
409 keys %$grouped_readings;
d120c995 410
411 # If something was transposed, check the groups for doubled-up readings
412 if( $has_transposition ) {
62a39b8f 413 # print STDERR "Group for rank $rank:\n";
414 # map { print STDERR "\t$_: " . join( ' ' , @{$grouped_readings->{$_}} ) . "\n" }
415 # keys %$grouped_readings;
d120c995 416 _check_transposed_consistency( $c, $rank, $transposed, $grouped_readings );
417 }
7f52eac8 418
4ce27d42 419 # Return the result
d120c995 420 return $grouped_readings;
421}
422
ee26c4d9 423sub _all_related {
f9b9b580 424 # Except by repetition
ee26c4d9 425 my $rdg = shift;
426 my $c = $rdg->collation;
f9b9b580 427 my @check = ( $rdg );
428 my %seen;
429 while( @check ) {
430 my @next;
431 foreach my $ck ( @check ) {
432 $seen{"$ck"} = 1;
433 push( @next, grep { !$seen{"$_"} }
434 $ck->related_readings( sub { $_[0]->type ne 'repetition' } ) );
435 }
436 @check = @next;
437 }
438
439
440 my @all = map { $c->reading( $_ ) } keys %seen;
ee26c4d9 441 return @all;
442}
443
444
d120c995 445# Helper function to query the alignment table for all witnesses (a.c. included)
446# that have a given reading at its rank.
447sub _table_witnesses {
608bbd95 448 my( $table, $rank, $trdg, $lacunose, $aclabel ) = @_;
449 my $tableidx = $rank - 1;
7e17346f 450 my $has_reading = Set::Scalar->new();
d120c995 451 foreach my $row ( @{$table->{'alignment'}} ) {
452 my $wit = $row->{'witness'};
7e17346f 453 next if _is_lacunose( $wit, $lacunose, $aclabel );
d120c995 454 my $rdg = $row->{'tokens'}->[$tableidx];
f9595796 455 if( $trdg ) {
456 # We have some positive reading we want.
457 next unless exists $rdg->{'t'} && defined $rdg->{'t'};
458 if( $trdg->is_lacuna ) {
459 _add_to_witlist( $wit, $has_reading, $aclabel )
460 if $rdg->{'t'}->is_lacuna;
461 } else {
462 _add_to_witlist( $wit, $has_reading, $aclabel )
463 if $rdg->{'t'}->id eq $trdg->id;
464 }
465 } else {
466 # We want the omissions.
467 next if exists $rdg->{'t'} && defined $rdg->{'t'};
468 _add_to_witlist( $wit, $has_reading, $aclabel )
469 }
d120c995 470 }
7e17346f 471 return $has_reading->members;
472}
473
474# Helper function to see if a witness is lacunose even if we are asking about
475# the a.c. version
476sub _is_lacunose {
477 my ( $wit, $lac, $acstr ) = @_;
478 if( $wit =~ /^(.*)\Q$acstr\E$/ ) {
479 $wit = $1;
480 }
481 return $lac->has( $wit );
d1348d38 482}
483
335a62ef 484# Helper function to ensure that X and X a.c. never appear in the same list.
485sub _add_to_witlist {
486 my( $wit, $list, $acstr ) = @_;
335a62ef 487 if( $wit =~ /^(.*)\Q$acstr\E$/ ) {
7e17346f 488 # Don't add X a.c. if we already have X
489 return if $list->has( $1 );
335a62ef 490 } else {
7e17346f 491 # Delete X a.c. if we are about to add X
492 $list->delete( $wit.$acstr );
335a62ef 493 }
7e17346f 494 $list->insert( $wit );
335a62ef 495}
496
d120c995 497sub _check_transposed_consistency {
498 my( $c, $rank, $transposed, $groupings ) = @_;
499 my %seen_wits;
500 my %thisrank;
501 # Note which readings are actually at this rank, and which witnesses
502 # belong to which reading.
503 foreach my $rdg ( keys %$groupings ) {
504 my $rdgobj = $c->reading( $rdg );
505 # Count '(omitted)' as a reading at this rank
506 $thisrank{$rdg} = 1 if !$rdgobj || $rdgobj->rank == $rank;
507 map { push( @{$seen_wits{$_}}, $rdg ) } @{$groupings->{$rdg}};
508 }
509 # Our work is done if we have no witness belonging to more than one
510 # reading.
511 my @doubled = grep { scalar @{$seen_wits{$_}} > 1 } keys %seen_wits;
512 return unless @doubled;
513 # If we have a symmetric related transposition, drop the non-rank readings.
514 if( @doubled == scalar keys %seen_wits ) {
515 foreach my $rdg ( keys %$groupings ) {
516 if( !$thisrank{$rdg} ) {
517 my $groupstr = wit_stringify( $groupings->{$rdg} );
518 my ( $matched ) = grep { $groupstr eq wit_stringify( $groupings->{$_} ) }
519 keys %thisrank;
520 delete $groupings->{$rdg};
521 # If we found a group match, assume there is a symmetry happening.
522 # TODO think more about this
62a39b8f 523 # print STDERR "*** Deleting symmetric reading $rdg\n";
d120c995 524 unless( $matched ) {
525 delete $transposed->{$rdg};
526 warn "Found problem in evident symmetry with reading $rdg";
527 }
528 }
529 }
530 # Otherwise 'unhook' the transposed reading(s) that have duplicates.
531 } else {
532 foreach my $dup ( @doubled ) {
533 foreach my $rdg ( @{$seen_wits{$dup}} ) {
534 next if $thisrank{$rdg};
535 next unless exists $groupings->{$rdg};
62a39b8f 536 # print STDERR "*** Deleting asymmetric doubled-up reading $rdg\n";
d120c995 537 delete $groupings->{$rdg};
538 delete $transposed->{$rdg};
539 }
540 }
541 # and put any now-orphaned readings into an 'omitted' reading.
542 foreach my $wit ( keys %seen_wits ) {
543 unless( grep { exists $groupings->{$_} } @{$seen_wits{$wit}} ) {
7e17346f 544 $groupings->{'(omitted)'} = Set::Scalar->new()
545 unless exists $groupings->{'(omitted)'};
d120c995 546 _add_to_witlist( $wit, $groupings->{'(omitted)'}, $c->ac_label );
547 }
548 }
549 }
550}
551
7e17346f 552# For the given grouping, return its situation graph based on the stemma.
553sub _graph_for_grouping {
554 my( $stemma, $grouping, $lacunose, $aclabel ) = @_;
555 my $acwits = [];
556 my $extant = {};
557 foreach my $gs ( values %$grouping ) {
558 map {
559 if( $_ =~ /^(.*)\Q$aclabel\E$/ ) {
560 push( @$acwits, $1 ) unless $lacunose->has( $1 );
561 } else {
562 $extant->{$_} = 1 unless $lacunose->has( $_ );
563 }
564 } $gs->members;
565 }
566 my $graph;
567 try {
568 # contig contains all extant wits and all hypothetical wits
569 # needed to make up the groups.
ace5fce5 570 $graph = $stemma->situation_graph( $extant, $acwits, $aclabel );
7e17346f 571 } catch ( Text::Tradition::Error $e ) {
03c0a7d7 572 throw( "Could not extend graph with given extant and a.c. witnesses: "
573 . $e->message );
7e17346f 574 } catch {
03c0a7d7 575 throw( "Could not extend graph with a.c. witnesses @$acwits" );
7e17346f 576 }
577 return $graph;
578}
88a6bac5 579
7e17346f 580=head2 solve_variants( $calcdir, @groups )
581
582Looks up the set of groups in the answers provided by the external graph solver
583service and returns a cleaned-up answer, adding the rank IDs back where they belong.
88a6bac5 584
88a6bac5 585The answer has the form
586 { "variants" => [ array of variant location structures ],
587 "variant_count" => total,
588 "conflict_count" => number of conflicts detected,
589 "genealogical_count" => number of solutions found }
590
591=cut
592
593sub solve_variants {
03c0a7d7 594 my( @groups ) = @_;
595
a745c3d9 596 # Are we using a local result directory, or did we pass an empty value
597 # for the directory?
03c0a7d7 598 my $dir;
a745c3d9 599 unless( ref( $groups[0] ) eq 'HASH' ) {
03c0a7d7 600 $dir = shift @groups;
601 }
335a62ef 602
03c0a7d7 603 ## For each graph/group combo, make a Text::Tradition::Analysis::Result
604 ## object so that we can send it off for IDP lookup.
7e17346f 605 my $variants = [];
606 my $genealogical = 0; # counter
03c0a7d7 607 # TODO Optimize for unique graph problems
554e2e7d 608 my %problems;
7e17346f 609 foreach my $graphproblem ( @groups ) {
03c0a7d7 610 # Construct the calc result key and look up its answer
611 my $problem = Text::Tradition::Analysis::Result->new(
612 graph => $graphproblem->{'graph'},
613 setlist => [ values %{$graphproblem->{'grouping'}} ] );
554e2e7d 614 if( exists $problems{$problem->object_key} ) {
615 $problem = $problems{$problem->object_key};
616 } else {
617 $problems{$problem->object_key} = $problem;
618 }
619 $graphproblem->{'object'} = $problem;
03c0a7d7 620 }
621
554e2e7d 622 my %results;
03c0a7d7 623 if( $dir ) {
624 my $scope = $dir->new_scope;
554e2e7d 625 map { $results{$_} = $dir->lookup( $_ ) || $problems{$_} } keys %problems;
626 } else {
627 my $json = JSON->new->allow_blessed->convert_blessed->utf8->encode(
628 [ values %problems ] );
03c0a7d7 629 # Send it off and get the result
630 # print STDERR "Sending request: " . decode_utf8( $json ) . "\n";
631 my $ua = LWP::UserAgent->new();
632 my $resp = $ua->post( $SOLVER_URL, 'Content-Type' => 'application/json',
633 'Content' => $json );
a745c3d9 634 my $answer;
03c0a7d7 635 if( $resp->is_success ) {
a745c3d9 636 $answer = decode_json( $resp->content );
03c0a7d7 637 throw( "Unexpected answer from IDP: $answer" ) unless ref( $answer ) eq 'ARRAY';
638 } else {
639 throw( "IDP solver returned " . $resp->status_line . " / " . $resp->content
640 . "; cannot run graph analysis" );
641 }
642 # One more sanity check
643 throw( "Something went wrong with answer symmetricity" )
554e2e7d 644 unless keys( %problems ) == @$answer;
03c0a7d7 645 # Convert the results
554e2e7d 646 foreach my $a ( @$answer ) {
647 my $r = Text::Tradition::Analysis::Result->new( $a );
648 $results{$r->object_key} = $r;
649 }
03c0a7d7 650 }
651
652 # We now have a single JSON-encoded Result object per problem sent. Fold its
653 # answers into our variant info structure.
554e2e7d 654 foreach my $graphproblem ( @groups ) {
655 my $result = $results{$graphproblem->{'object'}->object_key}
656 || $graphproblem->{'object'};
03c0a7d7 657
7e17346f 658 # Initialize the result structure for this graph problem
b7bd7aa5 659 my $vstruct;
660 if( $result->status eq 'OK' ) {
661 $vstruct = { readings => [] };
662 push( @$variants, $vstruct );
663 } else {
664 push( @$variants, _init_unsolved( $graphproblem, $result->status ) );
7e17346f 665 next;
5c44c598 666 }
b7bd7aa5 667
7e17346f 668 # 1. Did the group evaluate as genealogical?
03c0a7d7 669 $vstruct->{genealogical} = $result->is_genealogical;
670 $genealogical++ if $result->is_genealogical;
7e17346f 671
672 # 2. What are the calculated minimum groupings for each variant loc?
673 foreach my $rid ( keys %{$graphproblem->{grouping}} ) {
674 my $inputset = $graphproblem->{grouping}->{$rid};
03c0a7d7 675 my $minset = $result->minimum_grouping_for( $inputset );
7e17346f 676 push( @{$vstruct->{readings}}, { readingid => $rid, group => $minset } );
6d25a3a0 677 }
7e17346f 678
679 # 3. What are the sources and classes calculated for each witness?
03c0a7d7 680 $vstruct->{witcopy_types} = { $result->classes };
7e17346f 681 $vstruct->{reading_roots} = {};
03c0a7d7 682 map { $vstruct->{reading_roots}->{$_} = 1 } $result->sources;
7e17346f 683
88a6bac5 684 }
685
686 return { 'variants' => $variants,
687 'variant_count' => scalar @$variants,
688 'genealogical_count' => $genealogical };
689}
690
b7bd7aa5 691sub _init_unsolved {
692 my( $graphproblem, $status ) = @_;
693 my $vstruct = { 'readings' => [] };
694 $vstruct->{'unsolved'} = $status;
695 foreach my $rid ( keys %{$graphproblem->{grouping}} ) {
696 push( @{$vstruct->{readings}}, { readingid => $rid,
697 group => [ $graphproblem->{grouping}->{$rid}->members ] } );
698 }
699 return $vstruct;
700}
701
fae07016 702=head2 analyze_location ( $tradition, $graph, $location_hash )
7f52eac8 703
fae07016 704Given the tradition, its stemma graph, and the solution from the graph solver,
705work out the rest of the information we want. For each reading we need missing,
5c44c598 706conflict, reading_parents, independent_occurrence, followed, not_followed,
707and follow_unknown. Alters the location_hash in place.
7f52eac8 708
709=cut
732152b1 710
fae07016 711sub analyze_location {
7e17346f 712 my ( $tradition, $graph, $variant_row, $lacunose ) = @_;
94654e27 713 my $c = $tradition->collation;
fae07016 714
03c0a7d7 715 if( exists $variant_row->{'unsolved'} ) {
716 return;
717 }
718 my $reading_roots = delete $variant_row->{'reading_roots'};
719 my $classinfo = delete $variant_row->{'witcopy_types'};
720
fae07016 721 # Make a hash of all known node memberships, and make the subgraphs.
722 my $contig = {};
fae07016 723 my $subgraph = {};
638e2a95 724 my $acstr = $c->ac_label;
725 my @acwits;
5c44c598 726
5c44c598 727 # Note which witnesses positively belong to which group. This information
728 # comes ultimately from the IDP solver.
729 # Also make a note of the reading's roots.
fae07016 730 foreach my $rdghash ( @{$variant_row->{'readings'}} ) {
731 my $rid = $rdghash->{'readingid'};
5c44c598 732 my @roots;
638e2a95 733 foreach my $wit ( @{$rdghash->{'group'}} ) {
734 $contig->{$wit} = $rid;
735 if( $wit =~ /^(.*)\Q$acstr\E$/ ) {
736 push( @acwits, $1 );
737 }
5c44c598 738 if( exists $reading_roots->{$wit} && $reading_roots->{$wit} ) {
739 push( @roots, $wit );
740 }
638e2a95 741 }
5c44c598 742 $rdghash->{'independent_occurrence'} = \@roots;
94654e27 743 }
7e17346f 744
fae07016 745 # Now that we have all the node group memberships, calculate followed/
bebec0e9 746 # non-followed/unknown values for each reading. Also figure out the
747 # reading's evident parent(s).
748 foreach my $rdghash ( @{$variant_row->{'readings'}} ) {
fae07016 749 my $rid = $rdghash->{'readingid'};
428bcf0b 750 my $rdg = $c->reading( $rid );
5c44c598 751 my @roots = @{$rdghash->{'independent_occurrence'}};
7e17346f 752 my @reversions;
753 if( $classinfo ) {
754 @reversions = grep { $classinfo->{$_} eq 'revert' }
755 $rdghash->{'group'}->members;
be590045 756 $rdghash->{'reversions'} = \@reversions;
7e17346f 757 }
5c44c598 758 my @group = @{$rdghash->{'group'}};
fae07016 759
760 # Start figuring things out.
7e17346f 761 $rdghash->{'followed'} = scalar( @group )
762 - ( scalar( @roots ) + scalar( @reversions ) );
bebec0e9 763 # Find the parent readings, if any, of this reading.
7e17346f 764 my $sourceparents = _find_reading_parents( $rid, $graph, $contig, @roots );
7e17346f 765 # Work out relationships between readings and their non-followed parent.
766 _resolve_parent_relationships( $c, $rid, $rdg, $sourceparents );
be590045 767 $rdghash->{'source_parents'} = $sourceparents;
e23225e7 768
769 if( @reversions ) {
770 my $revparents = _find_reading_parents( $rid, $graph, $contig, @reversions );
771 _resolve_parent_relationships( $c, $rid, $rdg, $revparents );
772 $rdghash->{'reversion_parents'} = $revparents;
773 }
bebec0e9 774
775 # Find the number of times this reading was altered, and the number of
776 # times we're not sure.
777 my( %nofollow, %unknownfollow );
5c44c598 778 foreach my $wit ( @{$rdghash->{'group'}} ) {
bebec0e9 779 foreach my $wchild ( $graph->successors( $wit ) ) {
5c44c598 780 if( $reading_roots->{$wchild} && $contig->{$wchild}
781 && $contig->{$wchild} ne $rid ) {
bebec0e9 782 # It definitely changed here.
783 $nofollow{$wchild} = 1;
784 } elsif( !($contig->{$wchild}) ) {
785 # The child is a hypothetical node not definitely in
786 # any group. Answer is unknown.
787 $unknownfollow{$wchild} = 1;
5c44c598 788 } # else it is either in our group, or it is a non-root node in a
789 # known group and therefore is presumed to have its reading from
790 # its group, not this link.
bebec0e9 791 }
792 }
793 $rdghash->{'not_followed'} = keys %nofollow;
794 $rdghash->{'follow_unknown'} = keys %unknownfollow;
fae07016 795
796 # Now say whether this reading represents a conflict.
797 unless( $variant_row->{'genealogical'} ) {
7e17346f 798 $rdghash->{'is_conflict'} = @roots != 1;
e23225e7 799 $rdghash->{'is_reverted'} = scalar @reversions;
7e17346f 800 }
801 }
802}
803
804sub _find_reading_parents {
805 my( $rid, $graph, $contig, @list ) = @_;
806 my $parenthash = {};
807 foreach my $wit ( @list ) {
808 # Look in the stemma graph to find this witness's extant or known-reading
809 # immediate ancestor(s), and look up the reading that each ancestor holds.
810 my @check = $graph->predecessors( $wit );
811 while( @check ) {
812 my @next;
813 foreach my $wparent( @check ) {
814 my $preading = $contig->{$wparent};
815 if( $preading && $preading ne $rid ) {
816 $parenthash->{$preading} = 1;
817 } else {
818 push( @next, $graph->predecessors( $wparent ) );
819 }
820 }
821 @check = @next;
822 }
823 }
824 return $parenthash;
825}
826
827sub _resolve_parent_relationships {
828 my( $c, $rid, $rdg, $rdgparents ) = @_;
829 foreach my $p ( keys %$rdgparents ) {
830 # Resolve the relationship of the parent to the reading, and
831 # save it in our hash.
832 my $pobj = $c->reading( $p );
833 my $prep = $pobj ? $pobj->id . ' (' . $pobj->text . ')' : $p;
834 my $phash = { 'label' => $prep };
835 if( $pobj ) {
836 my $rel = $c->get_relationship( $p, $rid );
3bce8f64 837 if( $rel && $rel->type eq 'collated' ) {
838 $rel = undef;
839 }
7e17346f 840 if( $rel ) {
841 _add_to_hash( $rel, $phash );
842 } elsif( $rdg ) {
843 # First check for a transposed relationship
844 if( $rdg->rank != $pobj->rank ) {
845 foreach my $ti ( $rdg->related_readings( 'transposition' ) ) {
846 next unless $ti->text eq $rdg->text;
847 $rel = $c->get_relationship( $ti, $pobj );
848 if( $rel ) {
849 _add_to_hash( $rel, $phash, 1 );
850 last;
851 }
852 }
853 unless( $rel ) {
854 foreach my $ti ( $pobj->related_readings( 'transposition' ) ) {
855 next unless $ti->text eq $pobj->text;
856 $rel = $c->get_relationship( $ti, $rdg );
857 if( $rel ) {
858 _add_to_hash( $rel, $phash, 1 );
859 last;
860 }
861 }
5c44c598 862 }
863 }
7e17346f 864 unless( $rel ) {
865 # and then check for sheer word similarity.
866 my $rtext = $rdg->text;
867 my $ptext = $pobj->text;
868 if( similar( $rtext, $ptext ) ) {
869 # say STDERR "Words $rtext and $ptext judged similar";
870 $phash->{relation} = { type => 'wordsimilar' };
871 }
872 }
5c44c598 873 } else {
7e17346f 874 $phash->{relation} = { type => 'deletion' };
5c44c598 875 }
7e17346f 876 # Get the attributes of the parent object while we are here
877 $phash->{'text'} = $pobj->text if $pobj;
a445ce40 878 if( $pobj && $pobj->does('Text::Tradition::Morphology') ) {
879 $phash->{'is_nonsense'} = $pobj->is_nonsense;
880 $phash->{'is_ungrammatical'} = $pobj->grammar_invalid;
881 }
7e17346f 882 } elsif( $p eq '(omitted)' ) {
ee26c4d9 883 # Check to see if the reading in question is a repetition.
884 my @reps = $rdg->related_readings( 'repetition' );
885 if( @reps ) {
886 $phash->{relation} = { type => 'repetition',
887 annotation => "of reading @reps" };
888 } else {
889 $phash->{relation} = { type => 'addition' };
890 }
7e17346f 891 }
892 # Save it
893 $rdgparents->{$p} = $phash;
894 }
d71100ed 895}
896
428bcf0b 897sub _add_to_hash {
898 my( $rel, $phash, $is_transposed ) = @_;
899 $phash->{relation} = { type => $rel->type };
900 $phash->{relation}->{transposed} = 1 if $is_transposed;
901 $phash->{relation}->{annotation} = $rel->annotation
902 if $rel->has_annotation;
903}
904
905=head2 similar( $word1, $word2 )
906
907Use Algorithm::Diff to get a sense of how close the words are to each other.
908This will hopefully handle substitutions a bit more nicely than Levenshtein.
909
910=cut
911
912#!/usr/bin/env perl
913
914sub similar {
915 my( $word1, $word2 ) = sort { length($a) <=> length($b) } @_;
916 my @let1 = split( '', lc( $word1 ) );
917 my @let2 = split( '', lc( $word2 ) );
918 my $diff = Algorithm::Diff->new( \@let1, \@let2 );
919 my $mag = 0;
920 while( $diff->Next ) {
921 if( $diff->Same ) {
922 # Take off points for longer strings
923 my $cs = $diff->Range(1) - 2;
924 $cs = 0 if $cs < 0;
925 $mag -= $cs;
926 } elsif( !$diff->Items(1) ) {
927 $mag += $diff->Range(2);
928 } elsif( !$diff->Items(2) ) {
929 $mag += $diff->Range(1);
930 } else {
931 # Split the difference for substitutions
932 my $c1 = $diff->Range(1) || 1;
933 my $c2 = $diff->Range(2) || 1;
934 my $cd = ( $c1 + $c2 ) / 2;
935 $mag += $cd;
936 }
937 }
938 return ( $mag <= length( $word1 ) / 2 );
939}
940
5be0cdeb 941sub _useful_variant {
7e17346f 942 my( $rankgroup, $rankgraph, $acstr ) = @_;
5be0cdeb 943
944 # Sort by group size and return
945 my $is_useful = 0;
7e17346f 946 foreach my $rdg ( keys %$rankgroup ) {
947 my @wits = $rankgroup->{$rdg}->members;
948 if( @wits > 1 ) {
5be0cdeb 949 $is_useful++;
950 } else {
7e17346f 951 $is_useful++ unless( $rankgraph->is_sink_vertex( $wits[0] )
952 || $wits[0] =~ /\Q$acstr\E$/ );
5be0cdeb 953 }
954 }
7e17346f 955 return $is_useful > 1;
5be0cdeb 956}
957
7f52eac8 958=head2 wit_stringify( $groups )
959
960Takes an array of witness groupings and produces a string like
961['A','B'] / ['C','D','E'] / ['F']
d71100ed 962
7f52eac8 963=cut
d71100ed 964
965sub wit_stringify {
966 my $groups = shift;
967 my @gst;
968 # If we were passed an array of witnesses instead of an array of
969 # groupings, then "group" the witnesses first.
970 unless( ref( $groups->[0] ) ) {
971 my $mkgrp = [ $groups ];
972 $groups = $mkgrp;
973 }
974 foreach my $g ( @$groups ) {
975 push( @gst, '[' . join( ',', map { "'$_'" } @$g ) . ']' );
976 }
977 return join( ' / ', @gst );
978}
7f52eac8 979
7f52eac8 9801;
981
a745c3d9 982sub throw {
983 Text::Tradition::Error->throw(
984 'ident' => 'Analysis error',
985 'message' => $_[0],
986 );
987}
988
7f52eac8 989=head1 LICENSE
990
991This package is free software and is provided "as is" without express
992or implied warranty. You can redistribute it and/or modify it under
993the same terms as Perl itself.
994
995=head1 AUTHOR
996
997Tara L Andrews E<lt>aurum@cpan.orgE<gt>