Merge branch 'master' of github.com:tla/stemmatology
[scpubgit/stemmatology.git] / script / join_readings.pl
CommitLineData
869a1ada 1#!/usr/bin/env perl
2
3use lib 'lib';
4use feature 'say';
5use strict;
6use warnings;
7use Getopt::Long;
8use Lingua::Features::Structure;
9use Text::Tradition::Directory;
10use XML::Easy::Syntax qw/ $xml10_name_rx $xml10_namestartchar_rx /;
11use TryCatch;
12
13binmode STDOUT, ':utf8';
14binmode STDERR, ':utf8';
15eval { no warnings; binmode $DB::OUT, ':utf8'; $DB::deep = 1000 };
16
17my( $dbuser, $dbpass );
18my $dsn = 'dbi:SQLite:dbname=stemmaweb/db/traditions.db';
19my $testrun;
20
21GetOptions(
22 'dsn=s' => \$dsn,
23 'u|user=s' => \$dbuser,
24 'p|pass=s' => \$dbpass,
25 'n|test' => \$testrun,
26 );
27
28my $dbopts = { dsn => $dsn };
29$dbopts->{extra_args}->{user} = $dbuser if $dbuser;
30$dbopts->{extra_args}->{password} = $dbpass if $dbpass;
31
32my $dir = Text::Tradition::Directory->new( $dbopts );
33
34my $scope = $dir->new_scope();
35my $lookfor = $ARGV[0] || '';
36foreach 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.
6771a1b1 44
45 # Save/update the current path texts
869a1ada 46 foreach my $wit ( $tradition->witnesses ) {
6771a1b1 47 my @pathtext = split( /\s+/, $c->path_text( $wit->sigil ) );
48 $wit->text( \@pathtext );
869a1ada 49 if( $wit->is_layered ) {
6771a1b1 50 my @layertext = split( /\s+/, $c->path_text( $wit->sigil.$c->ac_label ) );
51 $wit->layertext( \@layertext );
869a1ada 52 }
53 }
6771a1b1 54
55 # Do the deed
56 $c->compress_readings();
57 # ...and save it.
869a1ada 58 $dir->save( $tradition );
59}