add some final statistics; try to color anonymous nodes too
[scpubgit/stemmatology.git] / make_tradition.pl
CommitLineData
910a0a6d 1#!/usr/bin/env perl
2
3use lib 'lib';
4use strict;
5use warnings;
6use Getopt::Long;
7use Text::Tradition;
8use Text::Tradition::Stemma;
9
10binmode STDERR, ":utf8";
11binmode STDOUT, ":utf8";
12eval { no warnings; binmode $DB::OUT, ":utf8"; };
13
14my( $informat, $inbase, $outformat, $help, $linear, $HACK )
15 = ( '', '', '', '', 1, 0 );
16
17GetOptions( '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
25if( $help ) {
26 help();
27}
28
d9e873d0 29unless( $informat =~ /^(CSV|CTE|KUL|Self|TEI|CollateX|tab(ular)?)$/i ) {
910a0a6d 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;
d9e873d0 37$informat = 'Tabular' if $informat =~ /^tab$/i;
910a0a6d 38
39unless( $outformat =~ /^(graphml|svg|dot|stemma|csv)$/ ) {
40 help( "Output format must be one of graphml, svg, csv, stemma, or dot" );
41}
42
43# Do we have a base if we need it?
44if( $informat eq 'KUL' && !$inbase ) {
45 help( "$informat input needs a base text" );
46}
47
910a0a6d 48my $input = $ARGV[0];
910a0a6d 49
50# First: read the base. Make a graph, but also note which
51# nodes represent line beginnings.
dfc37e38 52my %args = ( 'input' => $informat,
53 'file' => $input,
910a0a6d 54 'linear' => $linear );
55$args{'base'} = $inbase if $inbase;
56my $tradition = Text::Tradition->new( %args );
57
58### Custom hacking
59# Remove witnesses C, E, G in the Matthew text
60if( $HACK ) {
61 foreach( $tradition->collation->paths() ) {
62 $tradition->collation->del_path( $_ ) if $_->label =~ /^[ceg]$/i;
63 }
64 foreach( $tradition->collation->readings() ) {
65 if( !$_->outgoing() && !$_->incoming() ) {
66 print STDERR "Deleting reading " . $_->label . "\n";
67 $tradition->collation->del_reading( $_ );
68 }
69 }
70}
71
72# Now output what we have been asked to.
73if( $outformat eq 'stemma' ) {
74 my $stemma = Text::Tradition::Stemma->new(
75 'collation' => $tradition->collation );
40f19742 76 my( $result, $tree ) = $stemma->run_phylip_pars();
910a0a6d 77 if( $result ) {
78 print $tree;
79 } else {
80 print STDERR "Bad result: $tree";
81 }
82} else {
83 my $output = "as_$outformat";
84 print $tradition->collation->$output();
85}
86
87sub help {
88 my( $msg ) = @_;
89 print STDERR << "EOF"
90Usage: $0 -i [format] -o [format] (--base [filename]) (--(no)linear) [inputfile]
91 i, input: Format of the input file. Must be one of CollateX, CSV, CTE, Self, TEI.
92 o, output: Format of the output. Must be one of svg, dot, graphml, csv, stemma.
93 b, base: Filename that contains a base text. Needed for CSV input.
94 l, linear: Treat transposed readings separately, producing a linear graph.
95 If nolinear, treat transposed readings as the same node.
96 h, help: Print this message.
97EOF
98 ;
99 if( $msg ) {
100 print STDERR "$msg\n";
101 }
102 exit ($msg ? 1 : 0 );
103}