make lacuna nodes lacunae from the beginning
[scpubgit/stemmatology.git] / lib / Text / Tradition / Parser / Tabular.pm
index 64b1575..46fcbea 100644 (file)
@@ -68,8 +68,8 @@ is( ref( $t ), 'Text::Tradition', "Parsed florilegium CSV file" );
 
 ### TODO Check these figures
 if( $t ) {
-    is( scalar $t->collation->readings, 313, "Collation has all readings" );
-    is( scalar $t->collation->paths, 2877, "Collation has all paths" );
+    is( scalar $t->collation->readings, 312, "Collation has all readings" );
+    is( scalar $t->collation->paths, 363, "Collation has all paths" );
     is( scalar $t->witnesses, 13, "Collation has all witnesses" );
 }
 
@@ -97,7 +97,9 @@ sub parse {
             }
         }
     } elsif( exists $opts->{'file'} ) {
-        open( my $fh, $opts->{'file'} ) or die "Could not open input file " . $opts->{'file'};
+        open( my $fh, $opts->{'file'} ) 
+            or warn "Could not open input file " . $opts->{'file'};
+        binmode( $fh, ':utf8' );
         while( my $row = $csv->getline( $fh ) ) {
             push( @$alignment_table, $row );
         }
@@ -136,7 +138,6 @@ sub parse {
         }
     }
     
-    
     # Collapse our lacunae into a single node and
     # push the end node onto all paths.
     $c->end->rank( scalar @$alignment_table );
@@ -145,19 +146,13 @@ sub parse {
         my $last_rdg = shift @$p;
         my $new_p = [ $last_rdg ];
         foreach my $rdg ( @$p ) {
-            if( $rdg->text eq '#LACUNA#' ) {
+            if( $rdg->is_lacuna && $last_rdg->is_lacuna ) {
                 # If we are in a lacuna already, drop this node.
-                # Otherwise make a lacuna node and drop this node.
-                unless( $last_rdg->is_lacuna ) {
-                    my $l = $c->add_lacuna( $rdg->name );
-                    $l->rank( $rdg->rank );
-                    push( @$new_p, $l );
-                    $last_rdg = $l;
-                }
-                $c->del_reading( $rdg );
+                               $c->del_reading( $rdg );
             } else {
-                # No lacuna, save the reading.
+                # Save the reading, lacuna or no.
                 push( @$new_p, $rdg );
+                $last_rdg = $rdg;
             }
         }
         push( @$new_p, $c->end );
@@ -186,11 +181,15 @@ sub make_nodes {
     }
     my $ctr = 1;
     foreach my $w ( keys %unique ) {
-        my $r = $collation->add_reading( "$index,$ctr" );
-        $ctr++;
-        $r->rank( $index );
-        $r->text( $w );
+       my $rargs = {
+               'id' => "$index,$ctr",
+               'rank' => $index,
+               'text' => $w,
+               };
+       $rargs->{'is_lacuna'} = 1 if $w eq '#LACUNA#';
+        my $r = $collation->add_reading( $rargs );
         $unique{$w} = $r;
+        $ctr++;
     }
     return \%unique;
 }