8 use Text::Tradition::Directory;
9 use Text::Tradition::StemmaUtil;
11 binmode STDERR, ":utf8";
12 binmode STDOUT, ":utf8";
13 eval { no warnings; binmode $DB::OUT, ":utf8"; };
15 my( $informat, $inbase, $outformat, $help, $linear, $name, $HACK, $sep, $stemmafile,
16 $dsn, $dbuser, $dbpass )
17 = ( '', '', '', '', 1, 'Tradition', 0, "\t", '',
18 "dbi:SQLite:dbname=stemmaweb/db/traditions.db", undef, undef );
20 GetOptions( 'i|in=s' => \$informat,
21 'b|base=s' => \$inbase,
22 'o|out=s' => \$outformat,
23 'l|linear!' => \$linear,
26 's|stemma=s' => \$stemmafile,
27 'u|user=s' => \$dbuser,
28 'p|pass=s' => \$dbpass,
38 unless( $informat =~ /^(CSV|CTE|KUL|Self|TEI|CollateX|tab(ular)?)|stone$/i ) {
39 help( "Input format must be one of CollateX, CSV, CTE, Self, TEI" );
41 $informat = 'CollateX' if $informat =~ /^c(ollate)?x$/i;
42 $informat = 'KUL' if $informat =~ /^kul$/i;
43 $informat = 'CTE' if $informat =~ /^cte$/i;
44 $informat = 'Self' if $informat =~ /^self$/i;
45 $informat = 'TEI' if $informat =~ /^tei$/i;
46 $informat = 'Tabular' if $informat =~ /^tab$/i;
47 $informat = 'CollateText' if $informat =~ /^stone$/i;
49 unless( $outformat =~ /^(graphml|svg|dot|stemma|csv|db)$/ ) {
50 help( "Output format must be one of db, graphml, svg, csv, stemma, or dot" );
53 # Do we have a base if we need it?
54 if( $informat =~ /^(KUL|CollateText)$/ && !$inbase ) {
55 help( "$informat input needs a base text" );
57 $sep = "\t" if $sep eq 'tab';
61 # First: read the base. Make a graph, but also note which
62 # nodes represent line beginnings.
63 my %args = ( 'input' => $informat,
65 'linear' => $linear );
66 $args{'base'} = $inbase if $inbase;
67 $args{'name'} = $name if $name;
68 $args{'sep_char'} = $sep if $informat eq 'Tabular';
69 ### Custom hacking for Stone
70 if( $informat eq 'CollateText' ) {
71 $args{'sigla'} = [ qw/ S M X V Z Bb B K W L / ];
73 my $tradition = Text::Tradition->new( %args );
75 my $stemma = $tradition->add_stemma( dotfile => $stemmafile );
76 print STDERR "Saved stemma at $stemmafile\n" if $stemma;
80 # Remove witnesses C, E, G in the Matthew text
82 my @togo = qw/ C E G /;
83 $tradition->collation->clear_witness( @togo );
84 $tradition->del_witness( @togo );
87 # Now output what we have been asked to.
88 if( $outformat eq 'stemma' ) {
89 my $cdata = character_input( $tradition->collation->make_alignment_table );
90 my( $result, $tree ) = phylip_pars( $cdata );
94 print STDERR "Bad result: $tree";
96 } elsif( $outformat eq 'db' ) {
97 my $extra_args = { 'create' => 1 };
98 $extra_args->{'user'} = $dbuser if $dbuser;
99 $extra_args->{'password'} = $dbpass if $dbpass;
100 my $dir = Text::Tradition::Directory->new( 'dsn' => $dsn,
101 'extra_args' => $extra_args );
102 my $scope = $dir->new_scope;
103 my $uuid = $dir->store( $tradition );
104 print STDERR "Saved tradition to database with ID $uuid\n";
106 my $output = "as_$outformat";
107 print $tradition->collation->$output();
112 print STDERR << "EOF"
113 Usage: $0 -i [format] -o [format] (--base [filename]) (--(no)linear) [inputfile]
114 i, input: Format of the input file. Must be one of CollateX, CSV, CTE, Self, TEI.
115 o, output: Format of the output. Must be one of svg, dot, graphml, csv, stemma.
116 b, base: Filename that contains a base text. Needed for CSV input.
117 l, linear: Treat transposed readings separately, producing a linear graph.
118 If nolinear, treat transposed readings as the same node.
119 h, help: Print this message.
123 print STDERR "$msg\n";
125 exit ($msg ? 1 : 0 );