From f676f0ef203532bb94900a0e2ac5ae5b512316b0 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Thu, 13 Jul 2006 11:54:33 +0000 Subject: [PATCH] r831@llin: dpavlin | 2006-07-13 13:56:19 +0200 first cut in implementing modify_records using automatically generated regexpes git-svn-id: svn+ssh://mjesec/home/dpavlin/svn/webpac2/trunk@597 07558da8-63fa-0310-ba24-9fe276d99e06 --- Makefile.PL | 2 +- lib/WebPAC/Input.pm | 111 ++++++++++++++++++++++++++++++++------- lib/WebPAC/Input/ISIS.pm | 25 ++++++--- 3 files changed, 109 insertions(+), 29 deletions(-) diff --git a/Makefile.PL b/Makefile.PL index b85e1e8..11edce5 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -24,7 +24,7 @@ WriteMakefile( 'Encode' => 0, 'LWP' => 0, 'File::Path' => 0, - 'Biblio::Isis' => 0.20, + 'Biblio::Isis' => 0.22, 'MARC::Fast' => 0.02, 'Search::Estraier' => 0.06, 'List::Util' => 0, diff --git a/lib/WebPAC/Input.pm b/lib/WebPAC/Input.pm index fb1b4ca..3d2687a 100644 --- a/lib/WebPAC/Input.pm +++ b/lib/WebPAC/Input.pm @@ -39,15 +39,14 @@ C and optional C functions. Perhaps a little code snippet. - use WebPAC::Input; + use WebPAC::Input; - my $db = WebPAC::Input->new( - module => 'WebPAC::Input::ISIS', - config => $config, + my $db = WebPAC::Input->new( + module => 'WebPAC::Input::ISIS', low_mem => 1, - ); + ); - $db->open('/path/to/database'); + $db->open( path => '/path/to/database' ); print "database size: ",$db->size,"\n"; while (my $rec = $db->fetch) { # do something with $rec @@ -69,7 +68,7 @@ Create new input database object. no_progress_bar => 1, ); -C is low-level file format module. See L and +C is low-level file format module. See L and L. Optional parametar C specify application code page (which will be @@ -169,6 +168,10 @@ This function will read whole database in memory and produce lookups. my ($k,$v) = @_; # store lookup $k => $v }, + modify_records => { + 900 => { '^a' => { ' : ' => '^b' } }, + 901 => { '*' => { '^b' => ' ; ' } }, + }, ); By default, C is assumed to be C<852>. @@ -179,9 +182,13 @@ C is optional parametar to read just C records from database C create optional report about usage of fields and subfields -C is closure to call when adding key => $value combinations to +C is closure to call when adding C<< key => 'value' >> combinations to lookup. +C specify mapping from subfields to delimiters or from +delimiters to subfields, as well as oprations on fields (if subfield is +defined as C<*>. + Returns size of database, regardless of C and C parametars, see also C. @@ -210,36 +217,55 @@ sub open { $self->{iconv} = Text::Iconv->new($code_page,$self->{'encoding'}); ## FIXME remove! my $filter_ref; + my $recode_regex; + my $recode_map; if ($self->{recode}) { my @r = split(/\s/, $self->{recode}); if ($#r % 2 != 1) { $log->logwarn("recode needs even number of elements (some number of valid pairs)"); } else { - my $recode; while (@r) { my $from = shift @r; my $to = shift @r; - $recode->{$from} = $to; + $recode_map->{$from} = $to; } - my $regex = join '|' => keys %{ $recode }; - - $log->debug("using recode regex: $regex"); - - $filter_ref = sub { - my $t = shift; - $t =~ s/($regex)/$recode->{$1}/g; - return $t; - }; + $recode_regex = join '|' => keys %{ $recode_map }; + $log->debug("using recode regex: $recode_regex"); } } + my $rec_regex = $self->modify_record_regexps(%{ $arg->{modify_records} }); + $log->debug("rec_regex: ", Dumper($rec_regex)); + my ($db, $size) = $self->{open_db}->( $self, path => $arg->{path}, - filter => $filter_ref, + filter => sub { + my ($l,$f_nr) = @_; + return unless defined($l); + + ## FIXME remove iconv! + $l = $self->{iconv}->convert($l) if ($self->{iconv}); + + $l =~ s/($recode_regex)/$recode_map->{$1}/g if ($recode_regex && $recode_map); + + return $l unless ($rec_regex); + + # apply regexps + if ($rec_regex && defined($rec_regex->{$f_nr})) { + $log->logconfess("regexps->{$f_nr} must be ARRAY") if (ref($rec_regex->{$f_nr}) ne 'ARRAY'); + my $c = 0; + foreach my $r (@{ $rec_regex->{$f_nr} }) { + while ( eval '$l =~ ' . $r ) { $c++ }; + } + warn "## field $f_nr triggered $c regexpes\n" if ($c && $self->{debug}); + } + + return $l; + }, %{ $arg }, ); @@ -496,6 +522,51 @@ sub stats { return $out; } +=head1 modify_record_regexps + +Generate hash with regexpes to be applied using L. + + my $regexpes = $input->modify_record_regexps( + 900 => { '^a' => { ' : ' => '^b' } }, + 901 => { '*' => { '^b' => ' ; ' } }, + ); + +=cut + +sub modify_record_regexps { + my $self = shift; + my $modify_record = {@_}; + + my $regexpes; + + foreach my $f (keys %$modify_record) { +warn "--- f: $f\n"; + foreach my $sf (keys %{ $modify_record->{$f} }) { +warn "---- sf: $sf\n"; + foreach my $from (keys %{ $modify_record->{$f}->{$sf} }) { + my $to = $modify_record->{$f}->{$sf}->{$from}; + #die "no field?" unless defined($to); +warn "----- transform: |$from| -> |$to|\n"; + + if ($sf =~ /^\^/) { + my $regex = + 's/\Q'. $sf .'\E([^\^]+)\Q'. $from .'\E([^\^]+)/'. $sf .'$1'. $to .'$2/g'; + push @{ $regexpes->{$f} }, $regex; +warn ">>>>> $regex [sf]\n"; + } else { + my $regex = + 's/\Q'. $from .'\E/'. $to .'/g'; + push @{ $regexpes->{$f} }, $regex; +warn ">>>>> $regex [global]\n"; + } + + } + } + } + + return $regexpes; +} + =head1 MEMORY USAGE C options is double-edged sword. If enabled, WebPAC diff --git a/lib/WebPAC/Input/ISIS.pm b/lib/WebPAC/Input/ISIS.pm index b2109f4..094189c 100644 --- a/lib/WebPAC/Input/ISIS.pm +++ b/lib/WebPAC/Input/ISIS.pm @@ -50,10 +50,25 @@ sub init { Returns handle to database and size in records - my ($db,$size) = $open_db( + my ($db,$size) = $isis->open_db( path => '/path/to/LIBRI' + filter => sub { + my ($l,$field_nr) = @_; + # do something with $l which is line of input file + return $l; + }, } +Options: + +=over 4 + +=item path + +path to CDS/ISIS database + +=back + =cut sub open_db { @@ -77,11 +92,7 @@ sub open_db { $isis_db = new Biblio::Isis( isisdb => $arg->{path}, include_deleted => 1, - hash_filter => sub { - my $l = shift || return; - $l = $self->{iconv}->convert($l) if ($self->{iconv}); - return $l; - }, + hash_filter => $arg->{filter} ? sub { return $arg->{filter}->(@_); } : undef, ) or $log->logdie("can't find database $arg->{path}"); $db_size = $isis_db->count; @@ -99,8 +110,6 @@ Return record with ID C<$mfn> from database my $rec = $self->fetch_rec( $db, $mfn ); -} - =cut sub fetch_rec { -- 2.20.1