Problem to access some subfield with MARC::Record
[email protected] ("Emmanuel Di Pretoro")
| Newsgroups | perl.perl4lib |
|---|---|
| Message-ID | <[email protected]> |
Hello, I've created two little scripts to convert from BibTeX to MARC and vice versa. I've attached these scripts with a little BibTeX file. So, I convert a little bibliography from BibTeX to MARC without any problem. Unfortunately, when I try to convert my new MARC file to BibTeX, the 'year' subfield is lost. When I check data with marcdump, I can see all the data I want, included the year. When I use Data::Dumper to see the $entry in marc2bib.pl, I can see the 'year' subfield, unfortunately, I can access it via MARC::Record. Is there something I'm doing wrong ? Thanks in advance, Manu
bib2marc.pl
(text/plain, 1.8 KB)
#!/usr/bin/perl -w
use strict;
use warnings;
use Text::BibTeX;
use MARC::Record;
use Getopt::Long;
my $config = { output => 'export_marc.marc', };
my @bibtex_tag
= qw( author editor title publisher year volume series edition note );
my @marc21_tag = qw( 100$a 100$a 245$a 260$b 260$c 490$v 490$a 250$a 504$a );
my %bib2marc;
@bib2marc{@bibtex_tag} = @marc21_tag;
GetOptions( $config, 'input=s', 'output=s', 'help=s' );
if ( not exists $config->{input} ) {
die "Usage: $0 --input file --output file\n";
}
else {
my $converted = run( $config->{input}, $config->{output} );
}
sub run {
my ( $input, $output ) = @_;
my $cpt = 0;
my $bibfile = Text::BibTeX::File->new($input);
open my $marcfile, '>', $output;
while ( my $entry = Text::BibTeX::Entry->new($bibfile) ) {
next unless $entry->parse_ok;
next unless $entry->type eq 'book';
my $marcrecord = MARC::Record->new();
map { _process_tag( $_, $entry, $marcrecord ) } @bibtex_tag;
print $marcfile $marcrecord->as_usmarc();
$cpt++;
}
close $marcfile;
return $cpt;
}
sub _process_tag {
my ( $bibtag, $entry, $marcrecord ) = @_;
if ( $entry->exists($bibtag) ) {
my ( $tag, $subtag ) = split( '\$', $bib2marc{$bibtag}, 2 );
my $marcfield = MARC::Field->new( $tag, '', '',
$subtag => _clean_tex( $entry->get($bibtag) ) );
$marcrecord->append_fields($marcfield);
}
}
sub _clean_tex {
my $text = shift;
$text =~ s/[\{\}]//g;
$text =~ s/\\//g;
$text =~ s/`e//g;
$text =~ s/'e//g;
$text =~ s/^e//g;
$text =~ s/e//g;
$text =~ s/`a//g;
$text =~ s/^a//g;
$text =~ s/a//g;
$text =~ s/`u//g;
$text =~ s/^u//g;
$text =~ s/u//g;
$text =~ s/`i//g;
$text =~ s/^i//g;
$text =~ s/i//g;
return $text;
}
export_bibtex.bib
(text/plain, 1.9 KB)
@book{,
author = {V{\'e}ronique Mesguich and Armelle Thomas},
title = {Net recherche : Le guide pratique pour mieux trouver l{\'}information utile},
publisher = {ADBS},
series = {Sciences et Techniques de l{\'}Information}
}
@book{,
author = {Langworth, Ian and chromatic},
title = {Perl Testing: A Developer{\'}s Notebook},
publisher = {O{\'}Reilly}
}
@book{,
author = {Thomas A. Limoncelli},
title = {Time Management for System Administrators},
publisher = {O{\'}Reilly}
}
@book{,
author = {Bernard Pochet},
title = {M{\'e}thodologie documentaire : rechercher, consulter, r{\'e}diger {\`a} l{\'}heure d{\'}Internet},
publisher = {De Boeck}
}
@book{,
author = {Yolla Polity and G{\'e}rard Henneron and Rosalba Palermiti},
title = {L{\'}Organisation des connaissances : approches conceptuelles},
publisher = {L{\'}Harmattan}
}
@book{,
author = {Martine Darrobers and Nicole Le Pottier},
title = {La Recherche documentaire},
publisher = {Nathan}
}
@book{,
author = {Daniel Brown},
title = {Mastering Information Retrieval and Probabilistic Decision Intelligence Technology},
publisher = {Chandos Publishing}
}
@book{,
author = {Amanda Spink and Bernard J. Jansen},
title = {Web Search: Public Search of the Web},
publisher = {Kluwer Academic Publishers}
}
@book{,
author = {INRIA},
title = {La Recherche d{\'}information sur les r{\'e}seaux : Cours INRIA 30 sept-4oct.2002, Le Bono (Morbihan)},
publisher = {ADBS}
}
@book{,
author = {Jamie Whyte},
title = {Bad Thoughts: A Guide to Clear Thinking},
publisher = {Corvo Books}
}
@book{,
author = {Fr{\'e}d{\'e}ric Lenoir and Violette Cabesos},
title = {La Promesse de l{\'}ange},
publisher = {Le Livre de Poche}
}
@book{,
author = {Mark Jason Dominus},
title = {Higher-Order Perl},
publisher = {Morgan Kaufmann}
}
@book{,
author = {chromatic and Damian Conway and Curtis {"}Ovid{"} Poe},
title = {Perl Hacks},
publisher = {O{\'}Reilly}
}
marc2bib.pl
(text/plain, 1.9 KB)
#!/usr/bin/perl -w
use strict;
use warnings;
use Text::BibTeX;
use MARC::Record;
use MARC::File::USMARC;
use Getopt::Long;
my $config = { output => 'export_bibtex.bib', };
my @bibtex_tag = qw( author title publisher year volume series edition note );
my @marc21_tag = qw( 100$a 245$a 260$b 260$c 490$v 490$a 250$a 504$a );
my %marc2bib;
@marc2bib{@marc21_tag} = @bibtex_tag;
GetOptions( $config, 'input=s', 'output=s', 'help=s' );
if ( not exists $config->{input} ) {
die "Usage: $0 --input file --output file\n";
}
else {
my $converted = run( $config->{input}, $config->{output} );
}
sub run {
my ( $input, $output ) = @_;
my $cpt = 0;
my $marcfile = MARC::File::USMARC->in($input);
my $bibfile = Text::BibTeX::File->new( $output, '>' );
while ( my $entry = $marcfile->next() ) {
my $bibentry = Text::BibTeX::Entry->new();
$bibentry->set_type('book');
$bibentry->set_metatype(BTE_REGULAR);
$bibentry->set_key('');
map { _process_tag( $_, $entry, $bibentry ) } @marc21_tag;
$bibentry->write($bibfile);
$cpt++;
}
return $cpt;
}
sub _process_tag {
my ( $marctag, $marcrecord, $bibentry ) = @_;
my ( $tag, $subtag ) = split( '\$', $marctag, 2 );
my $data = $marcrecord->subfield( $tag, $subtag );
$bibentry->set( $marc2bib{$marctag}, _clean_tex($data) ) if $data;
}
sub _clean_tex {
my $text = shift;
$text =~ s/"/\{"\}/g;
$text =~ s/'/\{\\'\}/g;
$text =~ s//\{\\`e\}/g;
$text =~ s//\{\\'e\}/g;
$text =~ s//\{\\^e\}/g;
$text =~ s//\{\\e\}/g;
$text =~ s//\{\\`a\}/g;
$text =~ s//\{\\^a\}/g;
$text =~ s//\{\\a\}/g;
$text =~ s//\{\\`u\}/g;
$text =~ s//\{\\^u\}/g;
$text =~ s//\{\\u\}/g;
$text =~ s//\{\\`i\}/g;
$text =~ s//\{\\^i\}/g;
$text =~ s//\{\\i\}/g;
return $text;
}