remove HACK from tradition build script
[scpubgit/stemmatology.git] / script / make_tradition.pl
1 #!/usr/bin/env perl
2
3 use lib 'lib';
4 use strict;
5 use warnings;
6 use Getopt::Long;
7 use Text::Tradition;
8 use Text::Tradition::Directory;
9 use Text::Tradition::StemmaUtil;
10
11 binmode STDERR, ":utf8";
12 binmode STDOUT, ":utf8";
13 eval { no warnings; binmode $DB::OUT, ":utf8"; };
14
15 my( $informat, $inbase, $outformat, $help, $language, $name, $sep, $stemmafile, 
16         $dsn, $dbuser, $dbpass ) 
17     = ( '', '', '', '', 'Default', 'Tradition', "\t", '',
18         "dbi:SQLite:dbname=stemmaweb/db/traditions.db", undef, undef );
19
20 GetOptions( 'i|in=s'    => \$informat,
21             'b|base=s'  => \$inbase,
22             'o|out=s'   => \$outformat,
23             'l|language=s' => \$language,
24             'n|name=s'  => \$name,
25             'h|help'    => \$help,
26             's|stemma=s' => \$stemmafile,
27             'u|user=s'  => \$dbuser,
28             'p|pass=s'  => \$dbpass,
29             'sep=s'             => \$sep,
30             'dsn=s'             => \$dsn,
31     );
32
33 if( $help ) {
34     help();
35 }
36
37 unless( $informat =~ /^(CSV|CTE|KUL|Self|TEI|CollateX|tab(ular)?)|stone$/i ) {
38     help( "Input format must be one of CollateX, CSV, CTE, Self, TEI" );
39 }
40 $informat = 'CollateX' if $informat =~ /^c(ollate)?x$/i;
41 $informat = 'KUL' if $informat =~ /^kul$/i;
42 $informat = 'CTE' if $informat =~ /^cte$/i;
43 $informat = 'Self' if $informat =~ /^self$/i;
44 $informat = 'TEI' if $informat =~ /^tei$/i;
45 $informat = 'Tabular' if $informat =~ /^tab$/i;
46 $informat = 'CollateText' if $informat =~ /^stone$/i;
47
48 unless( $outformat =~ /^(graphml|svg|dot|stemma|csv|db)$/ ) {
49     help( "Output format must be one of db, graphml, svg, csv, stemma, or dot" );
50 }
51
52 # Do we have a base if we need it?
53 if( $informat =~ /^(KUL|CollateText)$/ && !$inbase ) {
54     help( "$informat input needs a base text" );
55 }
56 $sep = "\t" if $sep eq 'tab';
57
58 my $input = $ARGV[0];
59
60 # First: read the base. Make a graph, but also note which
61 # nodes represent line beginnings.
62 my %args = ( 'input' => $informat,
63              'file' => $input );
64 $args{'base'} = $inbase if $inbase;
65 $args{'language'} = $language if $language;
66 $args{'name'} = $name if $name;
67 $args{'sep_char'} = $sep if $informat eq 'Tabular';
68 ### Custom hacking for Stone
69 if( $informat eq 'CollateText' ) {
70     $args{'sigla'} = [ qw/ S M X V Z Bb B K W L / ];
71 }
72 my $tradition = Text::Tradition->new( %args );
73 if( $stemmafile ) {
74         my $stemma = $tradition->add_stemma( dotfile => $stemmafile );
75         print STDERR "Saved stemma at $stemmafile\n" if $stemma;
76 }
77
78 # Now output what we have been asked to.
79 if( $outformat eq 'stemma' ) {
80     my $cdata = character_input( $tradition->collation->make_alignment_table );
81     my( $result, $tree ) = phylip_pars( $cdata );
82     if( $result ) {
83         print $tree;
84     } else {
85         print STDERR "Bad result: $tree";
86     }
87 } elsif( $outformat eq 'db' ) {
88         my $extra_args = { 'create' => 1 };
89         $extra_args->{'user'} = $dbuser if $dbuser;
90         $extra_args->{'password'} = $dbpass if $dbpass;
91         my $dir = Text::Tradition::Directory->new( 'dsn' => $dsn, 
92                 'extra_args' => $extra_args );
93         my $scope = $dir->new_scope;
94         my $uuid = $dir->store( $tradition );
95         print STDERR "Saved tradition to database with ID $uuid\n";
96 } else {
97     my $output = "as_$outformat";
98     print $tradition->collation->$output();
99 }
100
101 sub help {
102     my( $msg ) = @_;
103     print STDERR << "EOF"
104 Usage: $0 -i [format] -o [format] (--base [filename]) (--(no)linear) [inputfile]
105     i, input: Format of the input file.  Must be one of CollateX, CSV, CTE, Self, TEI.
106     o, output: Format of the output.  Must be one of svg, dot, graphml, csv, stemma.
107     b, base: Filename that contains a base text.  Needed for CSV input.
108     l, linear: Treat transposed readings separately, producing a linear graph.  
109         If nolinear, treat transposed readings as the same node.
110     h, help: Print this message.
111 EOF
112     ;
113     if( $msg ) {
114         print STDERR "$msg\n";
115     }
116     exit ($msg ? 1 : 0 );
117 }