CHECKPOINT for laptop migration
[scpubgit/stemmatology.git] / lib / Text / Tradition / Parser / CSV.pm
index a7e3ea4..003936c 100644 (file)
@@ -2,8 +2,8 @@ package Text::Tradition::Parser::CSV;
 
 use strict;
 use warnings;
+use Storable qw /dclone/;
 use Text::CSV::Simple;
-use Text::Tradition::Parser::BaseText qw( merge_base );
 
 =head1 NAME
 
@@ -19,29 +19,16 @@ breaks.
 
 =over
 
-=item B<parse>
+=item B<read>
 
-parse( $graph, 'variants.csv', 'reference.txt' );
+my @apparatus = read( $csv_file );
 
-Takes an initialized Text::Tradition::Graph object and the relevant
-data files; puts the text and its variants onto the graph.
+Takes a CSV file; returns a data structure of apparatus entries to be
+merged with a base text.
 
 =cut
 
-sub parse {
-    my( $graph, $csv_file, $base_text ) = @_;
-
-    # Parse the CSV file into a list of apparatus entries.
-    my @app_list = _read_csv( $csv_file );
-    # Now put the base text onto the graph, and merge in the 
-    # apparatus entries.
-    merge_base( $graph, $base_text, @app_list );
-}
-
-# Takes a CSV file; returns a data structure of apparatus entries to
-# be merged with a base text.
-
-sub _read_csv {
+sub read {
     my( $csv_file ) = @_;
     my $parser = Text::CSV::Simple->new();
     my @fields = qw/ reference text variant type context non_corr non_indep 
@@ -69,33 +56,46 @@ sub _read_csv {
        # apparatus to the list, and clear it out, if we have started a
        # new reading.
        if( $new_lemma ) {
-           push( @app_list, $apparatus ) if keys %$apparatus;
-           $apparatus = { _id => $linehash{reference},
-           };
+           # Was it a doubled-up apparatus entry e.g 'non (1/2)'?
+           if( keys %$apparatus &&
+               $apparatus->{'rdg_0'} =~ /(.*?)\s+\(?([\d\/]+)\)?\s*$/ ) {
+               my( $reading, $istr ) = ( $1, $2 );
+               my @instances = split( /\//, $istr );
+               foreach my $i ( @instances ) {
+                   my $app = dclone( $apparatus );
+                   $app->{'rdg_0'} = $reading . "_$i";
+                   $app->{'_id'} .= chr(97+$i);
+                   push( @app_list, $app );
+               }
+           } elsif( keys %$apparatus ) {
+               push( @app_list, $apparatus );
+           }
+           $apparatus = { _id => $linehash{reference} };
            $rdg_ctr = 0;
        }
        # The apparatus has multiple readings, and multiple witnesses per
        # reading.  So it's a hashref whose values are listrefs.
-       $apparatus->{ 'rdg_0' } = $linehash{ 'text' } 
-        if $linehash{ 'text' };
-       $apparatus->{ 'rdg_' . ++$rdg_ctr } = $linehash{ 'variant' };
+       $apparatus->{'rdg_0'} = $linehash{'text'} if $linehash{'text'};
+       $apparatus->{'rdg_' . ++$rdg_ctr} = $linehash{'variant'};
        foreach my $attr ( @fields[3..8] ) {
-           $apparatus->{"_rdg_${rdg_ctr}_$attr"} = $linehash{ $attr }
-           if $linehash{ $attr };
+           $apparatus->{"_rdg_${rdg_ctr}_$attr"} = $linehash{$attr} if defined $linehash{$attr};
        }
        
        foreach my $k ( @fields[10..$#fields] ) {
            my $variant_rdg = $linehash{$k};
            $k =~ s/\s+\(a\.c\.\)//;
            if( $variant_rdg =~ /^0/ ) {
-               $apparatus->{ $k } = 'rdg_0'
-                   unless exists $apparatus->{ $k };
+               $apparatus->{$k} = 'rdg_0'
+                   unless exists $apparatus->{$k};
            } elsif ( $variant_rdg =~ /^1/ ) {
-               $apparatus->{ $k } = 'rdg_' . $rdg_ctr;
+               warn sprintf( "Already found variant reading %s for %s at %s!",
+                             $apparatus->{$k}, $k, $apparatus->{_id} )
+                   if exists $apparatus->{$k} && $apparatus->{$k} ne 'rdg_0';
+               $apparatus->{$k} = 'rdg_' . $rdg_ctr;
            } else { # else for $, we don't list the MS
                warn "Unparsed variant indicator $variant_rdg for $k in " .
                    $apparatus->{'_id'}
-               unless ( !$variant_rdg or $variant_rdg =~ /^\$$/ );
+                   unless ( !$variant_rdg or $variant_rdg =~ /^\$$/ );
            }
        }
        # See if we have at least one reading for each variant.