simplify Directory and add exceptions;
[scpubgit/stemmatology.git] / script / make_tradition.pl
index f928c33..309033b 100755 (executable)
@@ -5,22 +5,27 @@ use strict;
 use warnings;
 use Getopt::Long;
 use Text::Tradition;
-use Text::Tradition::Stemma;
+use Text::Tradition::Directory;
+use Text::Tradition::StemmaUtil;
 
 binmode STDERR, ":utf8";
 binmode STDOUT, ":utf8";
 eval { no warnings; binmode $DB::OUT, ":utf8"; };
 
-my( $informat, $inbase, $outformat, $help, $linear, $name, $HACK ) 
-    = ( '', '', '', '', 1, 'Tradition', 0 );
+my( $informat, $inbase, $outformat, $help, $linear, $name, $HACK, $sep, $stemmafile, $dsn ) 
+    = ( '', '', '', '', 1, 'Tradition', 0, "\t", '',
+       "dbi:SQLite:dbname=stemmaweb/db/traditions.db" );
 
 GetOptions( 'i|in=s'    => \$informat,
             'b|base=s'  => \$inbase,
             'o|out=s'   => \$outformat,
             'l|linear!' => \$linear,
-            'n|name'    => \$name,
+            'n|name=s'  => \$name,
             'h|help'    => \$help,
+            's|stemma=s' => \$stemmafile,
+            'sep=s'            => \$sep,
             'hack'      => \$HACK,
+            'dsn=s'            => \$dsn,
     );
 
 if( $help ) {
@@ -38,15 +43,15 @@ $informat = 'TEI' if $informat =~ /^tei$/i;
 $informat = 'Tabular' if $informat =~ /^tab$/i;
 $informat = 'CollateText' if $informat =~ /^stone$/i;
 
-unless( $outformat =~ /^(graphml|svg|dot|stemma|csv)$/ ) {
-    help( "Output format must be one of graphml, svg, csv, stemma, or dot" );
+unless( $outformat =~ /^(graphml|svg|dot|stemma|csv|db)$/ ) {
+    help( "Output format must be one of db, graphml, svg, csv, stemma, or dot" );
 }
 
 # Do we have a base if we need it?
 if( $informat =~ /^(KUL|CollateText)$/ && !$inbase ) {
     help( "$informat input needs a base text" );
 }
-
+$sep = "\t" if $sep eq 'tab';
 
 my $input = $ARGV[0];
 
@@ -57,36 +62,40 @@ my %args = ( 'input' => $informat,
              'linear' => $linear );
 $args{'base'} = $inbase if $inbase;
 $args{'name'} = $name if $name;
+$args{'sep_char'} = $sep if $informat eq 'Tabular';
 ### Custom hacking for Stone
 if( $informat eq 'CollateText' ) {
     $args{'sigla'} = [ qw/ S M X V Z Bb B K W L / ];
 }
 my $tradition = Text::Tradition->new( %args );
+if( $stemmafile ) {
+       my $stemma = $tradition->add_stemma( $stemmafile );
+       print STDERR "Saved stemma at $stemmafile\n" if $stemma;
+}
 
 ### Custom hacking
 # Remove witnesses C, E, G in the Matthew text
 if( $HACK ) {
-    foreach( $tradition->collation->paths() ) {
-        $tradition->collation->del_path( $_ ) if $_->label =~ /^[ceg]$/i;
-    }
-    foreach( $tradition->collation->readings() ) {
-        if( !$_->outgoing() && !$_->incoming() ) {
-            print STDERR "Deleting reading " . $_->label . "\n";
-            $tradition->collation->del_reading( $_ );
-        }
-    }
+       my @togo = qw/ C E G /;
+       $tradition->collation->clear_witness( @togo );
+       $tradition->del_witness( @togo );
 }
 
 # Now output what we have been asked to.
 if( $outformat eq 'stemma' ) {
-    my $stemma = Text::Tradition::Stemma->new( 
-        'collation' => $tradition->collation );
-    my( $result, $tree ) = $stemma->run_phylip_pars();
+    my $cdata = character_input( $tradition->collation->make_alignment_table );
+    my( $result, $tree ) = phylip_pars( $cdata );
     if( $result ) {
         print $tree;
     } else {
         print STDERR "Bad result: $tree";
     }
+} elsif( $outformat eq 'db' ) {
+       my $dir = Text::Tradition::Directory->new( 'dsn' => $dsn, 
+               'extra_args' => { 'create' => 1 } );
+       my $scope = $dir->new_scope;
+       my $uuid = $dir->store( $tradition );
+       print STDERR "Saved tradition to database with ID $uuid\n";
 } else {
     my $output = "as_$outformat";
     print $tradition->collation->$output();