segment the large texts in the relationship mapper
[scpubgit/stemmatology.git] / script / make_tradition.pl
CommitLineData
910a0a6d 1#!/usr/bin/env perl
2
3use lib 'lib';
4use strict;
5use warnings;
6use Getopt::Long;
7use Text::Tradition;
861c3e27 8use Text::Tradition::Directory;
4e5a7b2c 9use Text::Tradition::StemmaUtil;
910a0a6d 10
11binmode STDERR, ":utf8";
12binmode STDOUT, ":utf8";
13eval { no warnings; binmode $DB::OUT, ":utf8"; };
14
6a1c434d 15my( $informat, $inbase, $outformat, $help, $language, $name, $HACK, $sep, $stemmafile,
7d99d254 16 $dsn, $dbuser, $dbpass )
861c3e27 17 = ( '', '', '', '', 1, 'Tradition', 0, "\t", '',
7d99d254 18 "dbi:SQLite:dbname=stemmaweb/db/traditions.db", undef, undef );
910a0a6d 19
408449b7 20GetOptions( 'i|in=s' => \$informat,
21 'b|base=s' => \$inbase,
22 'o|out=s' => \$outformat,
6a1c434d 23 'l|language=s' => \$language,
861c3e27 24 'n|name=s' => \$name,
408449b7 25 'h|help' => \$help,
861c3e27 26 's|stemma=s' => \$stemmafile,
7d99d254 27 'u|user=s' => \$dbuser,
28 'p|pass=s' => \$dbpass,
a7fb3133 29 'sep=s' => \$sep,
408449b7 30 'hack' => \$HACK,
861c3e27 31 'dsn=s' => \$dsn,
910a0a6d 32 );
33
34if( $help ) {
35 help();
36}
37
fa954f4c 38unless( $informat =~ /^(CSV|CTE|KUL|Self|TEI|CollateX|tab(ular)?)|stone$/i ) {
910a0a6d 39 help( "Input format must be one of CollateX, CSV, CTE, Self, TEI" );
40}
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;
d9e873d0 46$informat = 'Tabular' if $informat =~ /^tab$/i;
fa954f4c 47$informat = 'CollateText' if $informat =~ /^stone$/i;
910a0a6d 48
861c3e27 49unless( $outformat =~ /^(graphml|svg|dot|stemma|csv|db)$/ ) {
50 help( "Output format must be one of db, graphml, svg, csv, stemma, or dot" );
910a0a6d 51}
52
53# Do we have a base if we need it?
fa954f4c 54if( $informat =~ /^(KUL|CollateText)$/ && !$inbase ) {
910a0a6d 55 help( "$informat input needs a base text" );
56}
aa71409f 57$sep = "\t" if $sep eq 'tab';
fa954f4c 58
910a0a6d 59my $input = $ARGV[0];
910a0a6d 60
61# First: read the base. Make a graph, but also note which
62# nodes represent line beginnings.
dfc37e38 63my %args = ( 'input' => $informat,
6a1c434d 64 'file' => $input );
910a0a6d 65$args{'base'} = $inbase if $inbase;
6a1c434d 66$args{'language'} = $language if $language;
408449b7 67$args{'name'} = $name if $name;
a7fb3133 68$args{'sep_char'} = $sep if $informat eq 'Tabular';
fa954f4c 69### Custom hacking for Stone
70if( $informat eq 'CollateText' ) {
71 $args{'sigla'} = [ qw/ S M X V Z Bb B K W L / ];
72}
910a0a6d 73my $tradition = Text::Tradition->new( %args );
861c3e27 74if( $stemmafile ) {
173ecc07 75 my $stemma = $tradition->add_stemma( dotfile => $stemmafile );
861c3e27 76 print STDERR "Saved stemma at $stemmafile\n" if $stemma;
77}
910a0a6d 78
79### Custom hacking
80# Remove witnesses C, E, G in the Matthew text
81if( $HACK ) {
5d90cf74 82 # Set the funny name while we're at it
6a1c434d 83 my $oldname = $tradition->name;
84 $oldname =~ s/(\d)/ $1/;
85 my $newname = "\x{17d}amanakagrut\x{2bf}iwn " . ucfirst( $oldname );
86 $tradition->name( $newname );
910a0a6d 87}
88
89# Now output what we have been asked to.
90if( $outformat eq 'stemma' ) {
4e5a7b2c 91 my $cdata = character_input( $tradition->collation->make_alignment_table );
92 my( $result, $tree ) = phylip_pars( $cdata );
910a0a6d 93 if( $result ) {
94 print $tree;
95 } else {
96 print STDERR "Bad result: $tree";
97 }
861c3e27 98} elsif( $outformat eq 'db' ) {
7d99d254 99 my $extra_args = { 'create' => 1 };
100 $extra_args->{'user'} = $dbuser if $dbuser;
101 $extra_args->{'password'} = $dbpass if $dbpass;
861c3e27 102 my $dir = Text::Tradition::Directory->new( 'dsn' => $dsn,
7d99d254 103 'extra_args' => $extra_args );
861c3e27 104 my $scope = $dir->new_scope;
105 my $uuid = $dir->store( $tradition );
106 print STDERR "Saved tradition to database with ID $uuid\n";
910a0a6d 107} else {
108 my $output = "as_$outformat";
109 print $tradition->collation->$output();
110}
111
112sub help {
113 my( $msg ) = @_;
114 print STDERR << "EOF"
115Usage: $0 -i [format] -o [format] (--base [filename]) (--(no)linear) [inputfile]
116 i, input: Format of the input file. Must be one of CollateX, CSV, CTE, Self, TEI.
117 o, output: Format of the output. Must be one of svg, dot, graphml, csv, stemma.
118 b, base: Filename that contains a base text. Needed for CSV input.
119 l, linear: Treat transposed readings separately, producing a linear graph.
120 If nolinear, treat transposed readings as the same node.
121 h, help: Print this message.
122EOF
123 ;
124 if( $msg ) {
125 print STDERR "$msg\n";
126 }
127 exit ($msg ? 1 : 0 );
128}