9 use Text::Tradition::Directory;
10 use Text::Tradition::StemmaUtil qw/ character_input phylip_pars /;
12 binmode STDERR, ":utf8";
13 binmode STDOUT, ":utf8";
14 eval { no warnings; binmode $DB::OUT, ":utf8"; };
16 my( $informat, $inbase, $outformat, $help, $language, $name, $sep, $stemmafile,
17 $dsn, $dbuser, $dbpass, $from, $to, $dbid )
18 = ( '', '', '', '', 'Default', 'Tradition', "\t", '',
19 "dbi:SQLite:dbname=stemmaweb/db/traditions.db", undef, undef, undef, undef, undef );
21 GetOptions( 'i|in=s' => \$informat,
22 'b|base=s' => \$inbase,
23 'o|out=s' => \$outformat,
24 'l|language=s' => \$language,
27 's|stemma=s' => \$stemmafile,
28 'u|user=s' => \$dbuser,
29 'p|pass=s' => \$dbpass,
41 unless( $informat =~ /^(CSV|CTE|KUL|Self|TEI|CollateX|tab(ular)?)|stone$/i ) {
42 help( "Input format must be one of CollateX, CSV, CTE, Self, TEI" );
44 $informat = 'CollateX' if $informat =~ /^c(ollate)?x$/i;
45 $informat = 'KUL' if $informat =~ /^kul$/i;
46 $informat = 'CTE' if $informat =~ /^cte$/i;
47 $informat = 'Self' if $informat =~ /^self$/i;
48 $informat = 'TEI' if $informat =~ /^tei$/i;
49 $informat = 'Tabular' if $informat =~ /^tab$/i;
50 $informat = 'CollateText' if $informat =~ /^stone$/i;
52 unless( $outformat =~ /^(graphml|svg|dot|stemma|csv|db)$/ ) {
53 help( "Output format must be one of db, graphml, svg, csv, stemma, or dot" );
57 help( "Subgraphs only supported in GraphML format" )
58 unless $outformat eq 'graphml';
61 # Do we have a base if we need it?
62 if( $informat =~ /^(KUL|CollateText)$/ && !$inbase ) {
63 help( "$informat input needs a base text" );
65 $sep = "\t" if $sep eq 'tab';
69 # First: read the base. Make a graph, but also note which
70 # nodes represent line beginnings.
71 my %args = ( 'input' => $informat,
73 $args{'base'} = $inbase if $inbase;
74 $args{'language'} = $language if $language;
75 $args{'name'} = $name if $name;
76 $args{'sep_char'} = $sep if $informat eq 'Tabular';
77 ### Custom hacking for Stone
78 if( $informat eq 'CollateText' ) {
79 $args{'sigla'} = [ qw/ S M X V Z Bb B K W L / ];
81 my $tradition = Text::Tradition->new( %args );
83 my $stemma = $tradition->add_stemma( dotfile => $stemmafile );
84 print STDERR "Saved stemma at $stemmafile\n" if $stemma;
87 # Now output what we have been asked to.
88 if( $outformat eq 'stemma' ) {
89 my $cdata = character_input( $tradition->collation->alignment_table );
91 print phylip_pars( $cdata );
92 } catch( Text::Tradition::Error $e ) {
93 print STDERR "Bad result: " . $e->message;
95 } elsif( $outformat eq 'db' ) {
96 my $extra_args = { 'create' => 1 };
97 $extra_args->{'user'} = $dbuser if $dbuser;
98 $extra_args->{'password'} = $dbpass if $dbpass;
99 my $dir = Text::Tradition::Directory->new( 'dsn' => $dsn,
100 'extra_args' => $extra_args );
101 my $scope = $dir->new_scope;
104 $uuid = $dir->store( $dbid => $tradition );
106 $uuid = $dir->store( $tradition );
108 print STDERR "Saved tradition to database with ID $uuid\n";
110 my $output = "as_$outformat";
112 $opts->{'from'} = $from if $from;
113 $opts->{'to'} = $to if $to;
114 print $tradition->collation->$output( $opts );
119 print STDERR << "EOF"
120 Usage: $0 -i [format] -o [format] (--base [filename]) (--(no)linear) [inputfile]
121 i, input: Format of the input file. Must be one of CollateX, CSV, CTE, Self, TEI.
122 o, output: Format of the output. Must be one of svg, dot, graphml, csv, stemma.
123 b, base: Filename that contains a base text. Needed for CSV input.
124 l, linear: Treat transposed readings separately, producing a linear graph.
125 If nolinear, treat transposed readings as the same node.
126 h, help: Print this message.
130 print STDERR "$msg\n";
132 exit ($msg ? 1 : 0 );