remove some debugging statements
[scpubgit/stemmatology.git] / lib / Text / Tradition / Parser / TEI.pm
index 6eb5541..78ae542 100644 (file)
@@ -62,8 +62,8 @@ my $t = Text::Tradition->new(
 
 is( ref( $t ), 'Text::Tradition', "Parsed parallel-segmentation TEI" );
 if( $t ) {
-    is( scalar $t->collation->readings, 319, "Collation has all readings" );
-    is( scalar $t->collation->paths, 2854, "Collation has all paths" );
+    is( scalar $t->collation->readings, 311, "Collation has all readings" );
+    is( scalar $t->collation->paths, 361, "Collation has all paths" );
 }
 
 =end testing
@@ -124,7 +124,7 @@ sub parse {
     foreach my $wit_el ( $xpc->findnodes( "//$LISTWIT/$WITNESS" ) ) {
         my $sig = $wit_el->getAttribute( 'xml:id' );
         my $source = $wit_el->toString();
-        $tradition->add_witness( sigil => $sig, source => $source );
+        $tradition->add_witness( sigil => $sig, sourcetype => 'collation' );
     }
     map { $text->{$_->sigil} = [] } $tradition->witnesses;
 
@@ -151,37 +151,30 @@ sub parse {
         my $sequence = $text->{$sig};
         my @real_sequence = ( $c->start );
         push( @$sequence, $c->end );
-        my $source = $c->start;
-        foreach( _clean_sequence( $sig, $sequence ) ) {
-            my $rdg = _return_rdg( $_ );
-            push( @real_sequence, $rdg );
-            $c->add_path( $source, $rdg, $sig );
-            $source = $rdg;
+        foreach( _clean_sequence( $sig, $sequence, 1 ) ) {
+            push( @real_sequence, _return_rdg( $_ ) );
         }
         # See if we need to make an a.c. version of the witness.
         if( exists $app_ac->{$sig} ) {
             my @uncorrected;
             push( @uncorrected, @real_sequence );
+            # Get rid of any remaining placeholders.
+            @real_sequence = _clean_sequence( $sig, \@uncorrected );
+            # Do the uncorrections
             foreach my $app ( keys %{$app_ac->{$sig}} ) {
                 my $start = _return_rdg( $app_anchors->{$app}->{$sig}->{'start'} ); 
                 my $end = _return_rdg( $app_anchors->{$app}->{$sig}->{'end'} );
                 my @new = map { _return_rdg( $_ ) } @{$app_ac->{$sig}->{$app}};
                 _replace_sequence( \@uncorrected, $start, $end, @new );
             }
-            my $source = $c->start;
-            foreach my $rdg ( @uncorrected ) {
-                my $has_base = grep { $_->label eq $sig } $source->edges_to( $rdg );
-                if( $rdg ne $c->start && !$has_base ) {
-                    print STDERR sprintf( "Adding path %s from %s -> %s\n",
-                        $sig.$c->ac_label, $source->name, $rdg->name );
-                    $c->add_path( $source, $rdg, $sig.$c->ac_label );
-                }
-                $source = $rdg;
-            }
-            print STDERR "Adding a.c. version for witness $sig\n";
+            # and record the results.
+            $tradition->witness( $sig )->uncorrected_path( \@uncorrected );
             $tradition->witness( $sig )->is_layered( 1 );
         }
+        $tradition->witness( $sig )->path( \@real_sequence );
     }
+    # Now make our witness paths.
+    $tradition->collation->make_witness_paths();
     
     # Calculate the ranks for the nodes.
        $tradition->collation->calculate_ranks();
@@ -189,19 +182,27 @@ sub parse {
     # Now that we have ranks, see if we have distinct nodes with identical
     # text and identical rank that can be merged.
     $tradition->collation->flatten_ranks();
+    
+    # And now that we've done that, calculate the common nodes.
+    $tradition->collation->calculate_common_readings();
+    
+    # Save the text for each witness so that we can ensure consistency
+    # later on
+       $tradition->collation->text_from_paths();       
 }
 
 sub _clean_sequence {
-    my( $wit, $sequence ) = @_;
+    my( $wit, $sequence, $keep_ac ) = @_;
     my @clean_sequence;
     foreach my $rdg ( @$sequence ) {
         if( $rdg =~ /^PH-(.*)$/ ) {
-            # It is a placeholder.  Keep it only if we need it.
+            # It is a placeholder.  Keep it only if we need it for a later
+            # a.c. run.
             my $app_id = $1;
-            if( exists $app_ac->{$wit} &&
+            if( $keep_ac && exists $app_ac->{$wit} &&
                 exists $app_ac->{$wit}->{$app_id} ) {
-                print STDERR "Retaining empty placeholder for $app_id\n";
-                push( @clean_sequence, $rdg );
+                               # print STDERR "Retaining empty placeholder for $app_id\n";
+                               push( @clean_sequence, $rdg );
             }
         } else {
             push( @clean_sequence, $rdg );
@@ -214,8 +215,10 @@ sub _replace_sequence {
     my( $arr, $start, $end, @new ) = @_;
     my( $start_idx, $end_idx );
     foreach my $i ( 0 .. $#{$arr} ) {
-        $start_idx = $i if( $arr->[$i]->name eq $start );
-        if( $arr->[$i]->name eq $end ) {
+       # If $arr->[$i] is a placeholder, cope.
+       my $iid = ref( $arr->[$i] ) ? $arr->[$i]->id : $arr->[$i];
+        $start_idx = $i if( $iid eq $start );
+        if( $iid eq $end ) {
             $end_idx = $i;
             last;
         }
@@ -234,9 +237,9 @@ sub _return_rdg {
     # passed a reading object, return the object.
     my $wantobj = ref( $rdg ) eq 'Text::Tradition::Collation::Reading';
     my $real = $rdg;
-    if( exists $substitutions->{ $wantobj ? $rdg->name : $rdg } ) {
-        $real = $substitutions->{ $wantobj ? $rdg->name : $rdg };
-        $real = $real->name unless $wantobj;
+    if( exists $substitutions->{ $wantobj ? $rdg->id : $rdg } ) {
+        $real = $substitutions->{ $wantobj ? $rdg->id : $rdg };
+        $real = $real->id unless $wantobj;
     }
     return $real;
 }
@@ -277,9 +280,6 @@ sub _return_rdg {
                 next if $w !~ /[[:alnum:]]/;
                 my $rdg = _make_reading( $tradition->collation, $w );
                 push( @new_readings, $rdg );
-                unless( $in_var ) {
-                    $rdg->make_common;
-                }
                 foreach ( @cur_wits ) {
                     warn "Empty wit!" unless $_;
                     warn "Empty reading!" unless $rdg;
@@ -296,9 +296,6 @@ sub _return_rdg {
             my $xml_id = $xn->getAttribute( 'xml:id' );
             my $rdg = _make_reading( $tradition->collation, $xn->textContent, $xml_id );
             push( @new_readings, $rdg );
-            unless( $in_var ) {
-                $rdg->make_common;
-            }
             foreach( @cur_wits ) {
                 warn "Empty wit!" unless $_;
                 warn "Empty reading!" unless $rdg;
@@ -318,11 +315,10 @@ sub _return_rdg {
             # Now collate these sets if we have more than one.
             my $subs = collate_variants( $tradition->collation, @sets ) if @sets > 1;
             map { $substitutions->{$_} = $subs->{$_} } keys %$subs;
-            # TODO Look through substitutions to see if we can make anything common now.
             # Return the entire set of unique readings.
             my %unique;
             foreach my $s ( @sets ) {
-                map { $unique{$_->name} = $_ } @$s;
+                map { $unique{$_->id} = $_ } @$s;
             }
             push( @new_readings, values( %unique ) );
             # Exit the current app.
@@ -351,8 +347,8 @@ sub _return_rdg {
                 # Add the reading set to the app anchors for each witness
                 # or put in placeholders for empty p.c. readings
                 foreach ( @rdg_wits ) {
-                    my $start = @words ? $words[0]->name : "PH-$current_app";
-                    my $end = @words ? $words[-1]->name : "PH-$current_app";
+                    my $start = @words ? $words[0]->id : "PH-$current_app";
+                    my $end = @words ? $words[-1]->id : "PH-$current_app";
                     $app_anchors->{$current_app}->{$_}->{'start'} = $start;
                     $app_anchors->{$current_app}->{$_}->{'end'} = $end;
                     push( @{$text->{$_}}, $start ) unless @words;
@@ -369,7 +365,9 @@ sub _return_rdg {
                 my $i = 0;
                 foreach my $sig ( keys %$text ) {
                     next if $active_wits{$sig};
-                    my $l = $tradition->collation->add_lacuna( $current_app . "_$i" );
+                    my $l = $tradition->collation->add_reading( {
+                       'id' => $current_app . "_$i",
+                       'is_lacuna' => 1 } );
                     $i++;
                     push( @{$text->{$sig}}, $l );
                 }
@@ -382,7 +380,9 @@ sub _return_rdg {
             unless( $seen_apps == $app_count ) {
                 foreach my $i ( 0 .. $#cur_wits ) {
                     my $w = $cur_wits[$i];
-                    my $l = $tradition->collation->add_lacuna( $current_app . "_$i" );
+                    my $l = $tradition->collation->add_reading( {
+                       'id' => $current_app . "_$i",
+                       'is_lacuna' => 1 } );
                     push( @{$text->{$w}}, $l );
                 }
             }
@@ -471,8 +471,10 @@ sub _get_sigla {
                 $xml_id = $try_id;
             }
         }
-        my $rdg = $graph->add_reading( $xml_id );
-        $rdg->text( $word );
+        my $rdg = $graph->add_reading(
+               { 'id' => $xml_id,
+                 'text' => $word }
+               );
         $used_nodeids{$xml_id} = $rdg;
         return $rdg;
     }