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 # Variables with defaults
17 my( $informat, $outformat, $language, $name, $sep, $dsn ) = ( '', '', 'Default',
18 'Tradition', "\t", "dbi:SQLite:dbname=stemmaweb/db/traditions.db" );
19 # Variables with no default
20 my( $inbase, $help, $stemmafile, $dbuser, $dbpass, $from, $to, $dbid, $debug );
22 GetOptions( 'i|in=s' => \$informat,
23 'b|base=s' => \$inbase,
24 'o|out=s' => \$outformat,
25 'l|language=s' => \$language,
28 's|stemma=s' => \$stemmafile,
29 'u|user=s' => \$dbuser,
30 'p|pass=s' => \$dbpass,
43 unless( $informat =~ /^(CSV|CTE|KUL|Self|TEI|CollateX|tab(ular)?)|stone|db$/i ) {
44 help( "Input format must be one of CollateX, CSV, CTE, Self, TEI" );
46 $informat = 'CollateX' if $informat =~ /^c(ollate)?x$/i;
47 $informat = 'KUL' if $informat =~ /^kul$/i;
48 $informat = 'CTE' if $informat =~ /^cte$/i;
49 $informat = 'Self' if $informat =~ /^self$/i;
50 $informat = 'TEI' if $informat =~ /^tei$/i;
51 $informat = 'Tabular' if $informat =~ /^tab$/i;
52 $informat = 'CollateText' if $informat =~ /^stone$/i;
54 unless( $outformat =~ /^(graphml|svg|dot|stemma|csv|db)$/ ) {
55 help( "Output format must be one of db, graphml, svg, csv, stemma, or dot" );
59 help( "Subgraphs only supported in GraphML format" )
60 unless $outformat eq 'graphml';
63 # Do we have a base if we need it?
64 if( $informat =~ /^(KUL|CollateText)$/ && !$inbase ) {
65 help( "$informat input needs a base text" );
67 $sep = "\t" if $sep eq 'tab';
72 if( $informat eq 'db' ) {
73 my $dbargs = { dsn => $dsn };
74 $dbargs->{'extra_args'}->{'user'} = $dbuser if $dbuser;
75 $dbargs->{'extra_args'}->{'password'} = $dbpass if $dbpass;
76 $dir = Text::Tradition::Directory->new( $dbargs );
77 my $scope = $dir->new_scope();
78 $tradition = $dir->lookup( $input );
80 # First: read the base. Make a graph, but also note which
81 # nodes represent line beginnings.
82 my %args = ( 'input' => $informat,
84 $args{'base'} = $inbase if $inbase;
85 $args{'language'} = $language if $language;
86 $args{'name'} = $name if $name;
87 $args{'sep_char'} = $sep if $informat eq 'Tabular';
88 ### Custom hacking for Stone
89 if( $informat eq 'CollateText' ) {
90 $args{'sigla'} = [ qw/ S M X V Z Bb B K W L / ];
92 $tradition = Text::Tradition->new( %args );
95 my $stemma = $tradition->add_stemma( dotfile => $stemmafile );
96 print STDERR "Saved stemma at $stemmafile\n" if $stemma;
99 # Now output what we have been asked to.
100 if( $outformat eq 'stemma' ) {
101 my $cdata = character_input( $tradition->collation->alignment_table );
103 print phylip_pars( $cdata );
104 } catch( Text::Tradition::Error $e ) {
105 print STDERR "Bad result: " . $e->message;
107 } elsif( $outformat eq 'db' ) {
109 my $extra_args = { 'create' => 1 };
110 $extra_args->{'user'} = $dbuser if $dbuser;
111 $extra_args->{'password'} = $dbpass if $dbpass;
112 $dir = Text::Tradition::Directory->new( 'dsn' => $dsn,
113 'extra_args' => $extra_args );
115 my $scope = $dir->new_scope;
118 $uuid = $dir->store( $dbid => $tradition );
120 $uuid = $dir->store( $tradition );
122 print STDERR "Saved tradition to database with ID $uuid\n";
124 my $output = "as_$outformat";
126 $opts->{'from'} = $from if $from;
127 $opts->{'to'} = $to if $to;
128 $opts->{'nocalc'} = 1 if $debug;
129 print $tradition->collation->$output( $opts );
134 print STDERR << "EOF"
135 Usage: $0 -i [format] -o [format] (--base [filename]) (--(no)linear) [inputfile]
136 i, input: Format of the input file. Must be one of CollateX, CSV, CTE, Self, TEI.
137 o, output: Format of the output. Must be one of svg, dot, graphml, csv, stemma.
138 b, base: Filename that contains a base text. Needed for CSV input.
139 l, linear: Treat transposed readings separately, producing a linear graph.
140 If nolinear, treat transposed readings as the same node.
141 h, help: Print this message.
145 print STDERR "$msg\n";
147 exit ($msg ? 1 : 0 );