get rid of common reading calculation for positional fun
[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
29unless( $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
38unless( $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?
43if( $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.
48my $input = $ARGV[0];
49unless( $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.
59my %args = ( $informat => $input,
60 'linear' => $linear );
61$args{'base'} = $inbase if $inbase;
62my $tradition = Text::Tradition->new( %args );
63
64### Custom hacking
65# Remove witnesses C, E, G in the Matthew text
66if( $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.
79if( $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
93sub help {
94 my( $msg ) = @_;
95 print STDERR << "EOF"
96Usage: $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.
103EOF
104 ;
105 if( $msg ) {
106 print STDERR "$msg\n";
107 }
108 exit ($msg ? 1 : 0 );
109}