move main functionality of join_readings into the library
[scpubgit/stemmatology.git] / script / join_readings.pl
1 #!/usr/bin/env perl
2
3 use lib 'lib';
4 use feature 'say';
5 use strict;
6 use warnings;
7 use Getopt::Long;
8 use Lingua::Features::Structure;
9 use Text::Tradition::Directory;
10 use XML::Easy::Syntax qw/ $xml10_name_rx $xml10_namestartchar_rx /;
11 use TryCatch;
12
13 binmode STDOUT, ':utf8';
14 binmode STDERR, ':utf8';
15 eval { no warnings; binmode $DB::OUT, ':utf8'; $DB::deep = 1000 };
16
17 my( $dbuser, $dbpass );
18 my $dsn = 'dbi:SQLite:dbname=stemmaweb/db/traditions.db';
19 my $testrun;
20
21 GetOptions( 
22         'dsn=s'    => \$dsn,
23         'u|user=s' => \$dbuser,
24         'p|pass=s' => \$dbpass,
25         'n|test'   => \$testrun,
26         );
27
28 my $dbopts = { dsn => $dsn };
29 $dbopts->{extra_args}->{user} = $dbuser if $dbuser;
30 $dbopts->{extra_args}->{password} = $dbpass if $dbpass;
31
32 my $dir = Text::Tradition::Directory->new( $dbopts );
33
34 my $scope = $dir->new_scope();
35 my $lookfor = $ARGV[0] || '';
36 foreach my $tinfo ( $dir->traditionlist() ) {
37         next unless $tinfo->{'name'} =~ /$lookfor/ || $tinfo->{'id'} eq $lookfor;
38         my $tradition = $dir->lookup( $tinfo->{'id'} );
39         my $c = $tradition->collation;
40
41         # Anywhere in the graph that there is a reading that joins only to a single
42         # successor, and neither of these have any relationships, just join the two
43         # readings.
44         
45         # Save/update the current path texts
46         foreach my $wit ( $tradition->witnesses ) {
47                 my @pathtext = split( /\s+/, $c->path_text( $wit->sigil ) );
48                 $wit->text( \@pathtext );
49                 if( $wit->is_layered ) {
50                         my @layertext = split( /\s+/, $c->path_text( $wit->sigil.$c->ac_label ) );
51                         $wit->layertext( \@layertext );
52                 }
53         }
54         
55         # Do the deed
56         $c->compress_readings();
57         # ...and save it.
58         $dir->save( $tradition );
59 }