Commit | Line | Data |
910a0a6d |
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::Stemma; |
9 | |
10 | binmode STDERR, ":utf8"; |
11 | binmode STDOUT, ":utf8"; |
12 | eval { no warnings; binmode $DB::OUT, ":utf8"; }; |
13 | |
14 | my( $informat, $inbase, $outformat, $help, $linear, $HACK ) |
15 | = ( '', '', '', '', 1, 0 ); |
16 | |
17 | GetOptions( 'i|in=s' => \$informat, |
18 | 'b|base=s' => \$inbase, |
19 | 'o|out=s' => \$outformat, |
20 | 'l|linear!' => \$linear, |
21 | 'h|help' => \$help, |
22 | 'hack' => \$HACK, |
23 | ); |
24 | |
25 | if( $help ) { |
26 | help(); |
27 | } |
28 | |
29 | unless( $informat =~ /^(CSV|CTE|KUL|Self|TEI|CollateX)$/i ) { |
30 | help( "Input format must be one of CollateX, CSV, CTE, Self, TEI" ); |
31 | } |
32 | $informat = 'CollateX' if $informat =~ /^c(ollate)?x$/i; |
33 | $informat = 'KUL' if $informat =~ /^kul$/i; |
34 | $informat = 'CTE' if $informat =~ /^cte$/i; |
35 | $informat = 'Self' if $informat =~ /^self$/i; |
36 | $informat = 'TEI' if $informat =~ /^tei$/i; |
37 | |
38 | unless( $outformat =~ /^(graphml|svg|dot|stemma|csv)$/ ) { |
39 | help( "Output format must be one of graphml, svg, csv, stemma, or dot" ); |
40 | } |
41 | |
42 | # Do we have a base if we need it? |
43 | if( $informat eq 'KUL' && !$inbase ) { |
44 | help( "$informat input needs a base text" ); |
45 | } |
46 | |
47 | # CSV parsing requires a filename; XML parsing requires a string. |
48 | my $input = $ARGV[0]; |
49 | unless( $informat eq 'KUL' || $informat eq 'CSV' ) { |
50 | my @lines; |
51 | open( INFILE, "$input" ) or die "Could not read $input"; |
52 | @lines = <INFILE>; |
53 | close INFILE; |
54 | $input = join( '', @lines ); |
55 | } |
56 | |
57 | # First: read the base. Make a graph, but also note which |
58 | # nodes represent line beginnings. |
59 | my %args = ( $informat => $input, |
60 | 'linear' => $linear ); |
61 | $args{'base'} = $inbase if $inbase; |
62 | my $tradition = Text::Tradition->new( %args ); |
63 | |
64 | ### Custom hacking |
65 | # Remove witnesses C, E, G in the Matthew text |
66 | if( $HACK ) { |
67 | foreach( $tradition->collation->paths() ) { |
68 | $tradition->collation->del_path( $_ ) if $_->label =~ /^[ceg]$/i; |
69 | } |
70 | foreach( $tradition->collation->readings() ) { |
71 | if( !$_->outgoing() && !$_->incoming() ) { |
72 | print STDERR "Deleting reading " . $_->label . "\n"; |
73 | $tradition->collation->del_reading( $_ ); |
74 | } |
75 | } |
76 | } |
77 | |
78 | # Now output what we have been asked to. |
79 | if( $outformat eq 'stemma' ) { |
80 | my $stemma = Text::Tradition::Stemma->new( |
81 | 'collation' => $tradition->collation ); |
82 | my( $result, $tree ) = $stemma->run_pars(); |
83 | if( $result ) { |
84 | print $tree; |
85 | } else { |
86 | print STDERR "Bad result: $tree"; |
87 | } |
88 | } else { |
89 | my $output = "as_$outformat"; |
90 | print $tradition->collation->$output(); |
91 | } |
92 | |
93 | sub help { |
94 | my( $msg ) = @_; |
95 | print STDERR << "EOF" |
96 | Usage: $0 -i [format] -o [format] (--base [filename]) (--(no)linear) [inputfile] |
97 | i, input: Format of the input file. Must be one of CollateX, CSV, CTE, Self, TEI. |
98 | o, output: Format of the output. Must be one of svg, dot, graphml, csv, stemma. |
99 | b, base: Filename that contains a base text. Needed for CSV input. |
100 | l, linear: Treat transposed readings separately, producing a linear graph. |
101 | If nolinear, treat transposed readings as the same node. |
102 | h, help: Print this message. |
103 | EOF |
104 | ; |
105 | if( $msg ) { |
106 | print STDERR "$msg\n"; |
107 | } |
108 | exit ($msg ? 1 : 0 ); |
109 | } |