X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=scpubgit%2Fstemmatology.git;a=blobdiff_plain;f=script%2Forth_case_links.pl;fp=script%2Forth_case_links.pl;h=948798498ed3c564793e01bba53876b81ff6e19c;hp=efde97cc91c927222120cbfb9c54e98779182d91;hb=378de72495d704d9de710a0b9c6cfec146de7886;hpb=354cc918ae96a100b8bbb332aba3ff1023a16f0a diff --git a/script/orth_case_links.pl b/script/orth_case_links.pl index efde97c..9487984 100755 --- a/script/orth_case_links.pl +++ b/script/orth_case_links.pl @@ -1,27 +1,40 @@ #!/usr/bin/env perl use lib 'lib'; +use feature 'say'; use strict; use warnings; +use Getopt::Long; use Text::Tradition::Directory; +use TryCatch; -binmode STDERR, ':utf8'; binmode STDOUT, ':utf8'; -eval { no warnings; binmode $DB::OUT, ':utf8' }; +binmode STDERR, ':utf8'; +eval { no warnings; binmode $DB::OUT, ':utf8'; $DB::deep = 1000 }; + +my( $dbuser, $dbpass ); +my $dsn = 'dbi:SQLite:dbname=stemmaweb/db/traditions.db'; +my $testrun; + +GetOptions( + 'dsn=s' => \$dsn, + 'u|user=s' => \$dbuser, + 'p|pass=s' => \$dbpass, + 'n|test' => \$testrun, + ); -my( $dsn, $user, $pass ) = @ARGV; +my $dbopts = { dsn => $dsn }; +$dbopts->{extra_args}->{user} = $dbuser if $dbuser; +$dbopts->{extra_args}->{password} = $dbpass if $dbpass; -my $connect_args = { dsn => $dsn }; -$connect_args->{'extra_args'} = { user => $user, password => $pass } - if $user && $pass; -my $dir = Text::Tradition::Directory->new( $connect_args ); +my $dir = Text::Tradition::Directory->new( $dbopts ); -foreach my $text ( $dir->traditionlist ) { - my $id = $text->{'id'}; - next unless $text->{'name'} =~ /Heinrichi/; - my $scope = $dir->new_scope; - my $tradition = $dir->lookup( $id ); - print STDERR "Processing tradition " . $tradition->name . "\n"; +my $scope = $dir->new_scope(); +my $lookfor = $ARGV[0] || ''; +foreach my $tinfo ( $dir->traditionlist() ) { + next unless $tinfo->{'name'} =~ /$lookfor/ || $tinfo->{'id'} eq $lookfor; + my $tradition = $dir->lookup( $tinfo->{id} ); + say STDERR "Processing tradition " . $tradition->name; my $c = $tradition->collation; $c->flatten_ranks(); # just in case foreach my $rank ( 1 .. $c->end->rank - 1 ) { @@ -34,16 +47,19 @@ foreach my $text ( $dir->traditionlist ) { my @orthmatch = grep { lc( $r->text ) eq lc( $_->text ) } @readings; foreach my $om ( @orthmatch ) { if( $r->text eq $om->text ) { - print STDERR "Merging identical readings $r and $om (" - . $r->text . ")\n"; + say STDERR "Merging identical readings $r and $om (" + . $r->text . ")"; $merged{$om->id} = 1; $c->merge_readings( $r, $om ); } else { - print STDERR sprintf( "Adding orthographic link for %s and %s (%s / %s)\n", + say STDERR sprintf( "Adding orthographic link for %s and %s (%s / %s)", $r->id, $om->id, $r->text, $om->text ); - eval { $c->add_relationship( $r, $om, - { 'type' => 'orthographic', 'scope' => 'global' } ); }; - print STDERR $@ if $@; + try { + $c->add_relationship( $r, $om, + { 'type' => 'orthographic', 'scope' => 'global' } ); }; + } catch ( Text::Tradition::Error $e ) { + say STDERR "Relationship skipped: " . $e->message; + } } } }