make tabular parse test work
[scpubgit/stemmatology.git] / lib / Text / Tradition / Parser / Tabular.pm
index 9daed14..4c1e511 100644 (file)
@@ -32,9 +32,7 @@ Parser module for Text::Tradition to read an alignment table format, such as CSV
 
 =head1 METHODS
 
-=over
-
-=item B<parse>( $tradition, $option_hash )
+=head2 B<parse>( $tradition, $option_hash )
 
 Takes an initialized tradition and a set of options; creates the
 appropriate nodes and edges on the graph, as well as the appropriate
@@ -70,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" );
 }
 
@@ -99,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 );
         }
@@ -138,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 );
@@ -147,12 +146,22 @@ sub parse {
         my $last_rdg = shift @$p;
         my $new_p = [ $last_rdg ];
         foreach my $rdg ( @$p ) {
+               $DB::single = 1 if $rdg->id eq '228,1';
             if( $rdg->text eq '#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 );
+                       my $l_id = 'l' . $rdg->id;
+                       my $l;
+                       if( $c->has_reading( $l_id ) ) {
+                               $l = $c->reading( $l_id );
+                       } else {
+                       $l = $c->add_reading( {
+                                                       'collation' => $c,
+                                                       'id' => $l_id,
+                                                       'is_lacuna' => 1,
+                                                       } );
+                                       }
                     push( @$new_p, $l );
                     $last_rdg = $l;
                 }
@@ -188,11 +197,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 = {
+               'collation' => $collation,
+               'id' => "$index,$ctr",
+               'rank' => $index,
+               'text' => $w,
+               };
+        my $r = $collation->add_reading( $rargs );
         $unique{$w} = $r;
+        $ctr++;
     }
     return \%unique;
 }