more work on our own graphml format
[scpubgit/stemmatology.git] / lib / Text / Tradition / Parser / Tabular.pm
index 10d0730..199490b 100644 (file)
@@ -30,10 +30,9 @@ sub parse {
     my( $tradition, $tab_str ) = @_;
     # TODO Allow setting of sep_char
     my $c = $tradition->collation; # shorthand
-    my $csv = Text::CSV_XS->new( { binary => 1 } ); # binary for UTF-8
+    my $csv = Text::CSV_XS->new( { binary => 1, # binary for UTF-8
+        sep_char => "\t" } );
     my @lines = split( "\n", $tab_str );
-    # Conveniently, we are basically receiving exactly the sort of alignment table
-    # we might want to produce later.  May as well save it.
     my $alignment_table;
     foreach my $l ( @lines ) {
         my $status = $csv->parse( $l );
@@ -54,7 +53,6 @@ sub parse {
     
     # Now for the next rows, make nodes as necessary, assign their ranks, and 
     # add them to the witness paths.
-    $DB::single = 1;
     foreach my $idx ( 1 .. $#{$alignment_table} ) {
         my $row = $alignment_table->[$idx];
         my $nodes = make_nodes( $c, $row, $idx );
@@ -69,19 +67,36 @@ sub parse {
         }
     }
     
-    # Push the end node onto all paths.
+    
+    # Collapse our lacunae into a single node and
+    # push the end node onto all paths.
     $c->end->rank( scalar @$alignment_table );
     foreach my $wit ( @witnesses ) {
-        push( @{$wit->path}, $c->end );
+        my $p = $wit->path;
+        my $last_rdg = shift @$p;
+        my $new_p = [ $last_rdg ];
+        foreach my $rdg ( @$p ) {
+            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 );
+                    push( @$new_p, $l );
+                    $last_rdg = $l;
+                }
+                $c->del_reading( $rdg );
+            } else {
+                # No lacuna, save the reading.
+                push( @$new_p, $rdg );
+            }
+        }
+        push( @$new_p, $c->end );
+        $wit->path( $new_p );
     }
     
     # Join up the paths.
     $c->make_witness_paths;
-    
-    # Save the alignment table that was so handily provided to us.
-    # TODO if we support other delimiters, we will have to re-export this
-    # rather than saving the original string.
-    $c->_save_csv( $tab_str );
 }
 
 sub make_nodes {