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