--- trunk/lib/WebPAC/Input.pm 2006/07/09 15:22:39 593 +++ trunk/lib/WebPAC/Input.pm 2006/09/06 18:08:30 634 @@ -7,8 +7,8 @@ use WebPAC::Common; use base qw/WebPAC::Common/; -use Text::Iconv; use Data::Dumper; +use Encode qw/from_to/; =head1 NAME @@ -16,11 +16,11 @@ =head1 VERSION -Version 0.08 +Version 0.11 =cut -our $VERSION = '0.08'; +our $VERSION = '0.11'; =head1 SYNOPSIS @@ -39,15 +39,14 @@ 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 @@ 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 @@ -160,7 +159,7 @@ $input->open( path => '/path/to/database/file', - code_page => '852', + code_page => 'cp852', limit => 500, offset => 6000, lookup => $lookup_obj, @@ -169,9 +168,13 @@ my ($k,$v) = @_; # store lookup $k => $v }, + modify_records => { + 900 => { '^a' => { ' : ' => '^b' } }, + 901 => { '*' => { '^b' => ' ; ' } }, + }, ); -By default, C is assumed to be C<852>. +By default, C is assumed to be C. C is optional parametar to position at some offset before reading from database. @@ -179,9 +182,13 @@ 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. @@ -198,7 +205,7 @@ if ($arg->{lookup_coderef} && ref($arg->{lookup_coderef}) ne 'CODE'); $log->logcroak("need path") if (! $arg->{'path'}); - my $code_page = $arg->{'code_page'} || '852'; + my $code_page = $arg->{'code_page'} || 'cp852'; # store data in object $self->{'input_code_page'} = $code_page; @@ -206,40 +213,40 @@ $self->{$v} = $arg->{$v} if ($arg->{$v}); } - # create Text::Iconv object - $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); +# from_to($l, $code_page, $self->{'encoding'}); +# $l =~ s/($recode_regex)/$recode_map->{$1}/g if ($recode_regex && $recode_map); +# return $l; +# }, %{ $arg }, ); @@ -279,7 +286,34 @@ $log->debug("position: $pos\n"); - my $rec = $self->{fetch_rec}->($self, $db, $pos ); + my $rec = $self->{fetch_rec}->($self, $db, $pos, sub { + my ($l,$f_nr) = @_; +# return unless defined($l); +# return $l unless ($rec_regex && $f_nr); + + $log->debug("-=> $f_nr ## $l"); + + # codepage conversion and recode_regex + from_to($l, $code_page, $self->{'encoding'}); + $l =~ s/($recode_regex)/$recode_map->{$1}/g if ($recode_regex && $recode_map); + + # 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} }) { + my $old_l = $l; + eval '$l =~ ' . $r; + if ($old_l ne $l) { + $log->debug("REGEX on $f_nr eval \$l =~ $r\n## old l: [$old_l]\n## new l: [$l]"); + } + $log->error("error applying regex: $r") if ($@); + } + } + + $log->debug("<=- $f_nr ## $l"); + return $l; + }); $log->debug(sub { Dumper($rec) }); @@ -301,6 +335,9 @@ # update counters for statistics if ($self->{stats}) { + # fetch clean record with regexpes applied for statistics + my $rec = $self->{fetch_rec}->($self, $db, $pos); + foreach my $fld (keys %{ $rec }) { $self->{_stats}->{fld}->{ $fld }++; @@ -312,6 +349,7 @@ if (ref($row) eq 'HASH') { foreach my $sf (keys %{ $row }) { + next if ($sf eq 'subfields'); $self->{_stats}->{sf}->{ $fld }->{ $sf }->{count}++; $self->{_stats}->{sf}->{ $fld }->{ $sf }->{repeatable}++ if (ref($row->{$sf}) eq 'ARRAY'); @@ -496,6 +534,55 @@ return $out; } +=head2 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; + + my $log = $self->_get_logger(); + + foreach my $f (keys %$modify_record) { + $log->debug("field: $f"); + + foreach my $sf (keys %{ $modify_record->{$f} }) { + $log->debug("subfield: $sf"); + + foreach my $from (keys %{ $modify_record->{$f}->{$sf} }) { + my $to = $modify_record->{$f}->{$sf}->{$from}; + #die "no field?" unless defined($to); + $log->debug("transform: |$from| -> |$to|"); + + if ($sf =~ /^\^/) { + my $regex = + 's/\Q'. $sf .'\E(.*?)\Q'. $from .'\E(.*?)/'. $sf .'$1'. $to .'$2/g'; + push @{ $regexpes->{$f} }, $regex; + $log->debug(">>>>> $regex [sf]"); + } else { + my $regex = + 's/\Q'. $from .'\E/'. $to .'/g'; + push @{ $regexpes->{$f} }, $regex; + $log->debug(">>>>> $regex [global]"); + } + + } + } + } + + return $regexpes; +} + =head1 MEMORY USAGE C options is double-edged sword. If enabled, WebPAC @@ -533,7 +620,7 @@ =head1 COPYRIGHT & LICENSE -Copyright 2005 Dobrica Pavlinusic, All Rights Reserved. +Copyright 2005-2006 Dobrica Pavlinusic, All Rights Reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.