Aard
view release on metacpan or search on metacpan
lib/Aard.pm view on Meta::CPAN
}
$self->{key}{$index}
}
sub article {
my ($self, $index) = @_;
unless (exists $self->{article}{$index}) {
my $part = $self->read_at($self->{article_offset} + $self->index1($index)->[1], 4);
my $len = unpack 'L>', $part;
read $self->{fh}, $part, $len;
$self->{article}{$index} = decompress $part
}
$self->{article}{$index}
}
sub new {
my ($self, $file) = @_;
open my $fh, '<', $file or die $!;
binmode $fh;
my %header;
for (@{HEADER_SPEC()}) {
read $fh, my $part, $_->[2];
$header{$_->[0]} = unpack $_->[1], $part;
}
die 'Not a recognized aarddict dictionary file' if $header{signature} ne 'aard';
die 'Unknown file format version' if $header{version} != 1;
read $fh, my $meta, $header{meta_length};
$meta = decode_json decompress $meta;
my %obj = (
%header,
fh => $fh,
meta => $meta,
index_format => ($header{index1_item_format} eq '>LL' ? 'L>L>' : 'L>Q>'),
index_length => ($header{index1_item_format} eq '>LL' ? 8 : 12),
);
$obj{index1_offset} = $header_length + $obj{meta_length};
$obj{index2_offset} = $obj{index1_offset} + $obj{index_count} * $obj{index_length};
bless \%obj, $self
}
1;
__END__
=head1 NAME
Aard - Read aarddict dictionaries
=head1 SYNOPSIS
use Aard;
my $dict = Aard->new('something.aar');
printf "This dictionary (volume %d of %d) has %d entries\n", $dict->volume, $dict->total_volumes, $dict->count;
printf "The tenth entry's key: %s\n", $dict->key(9);
printf "The tenth entry's value: %s\n", $dict->article(9);
=head1 DESCRIPTION
Aard is a module for reading files in the Aard Dictionary format (.aar). A dictionary is an array of I<(key, article)> pairs, with some associated metadata.
=over
=item B<new>(I<filename>)
Creates a new Aard object for the given file.
=item B<fh>
Returns the open filehandle to the dictionary.
=item B<count>
Returns the number of entries in this dictionary.
=item B<key>(I<index>)
Returns the key of the I<index>th element. This method caches the keys.
=item B<article>(I<index>)
Returns the article of the I<index>th element. This method caches the articles.
=item B<uuid>
Returns the UUID of this dictionary as a binary string. This is a value shared by all volumes of the same dictionary.
=item B<uuid_string>
Returns the UUID of this dictionary as a human-readable string. This is a value shared by all volumes of the same dictionary.
=item B<volume>
Returns the volume number of this file.
=item B<total_volumes>
Returns the total number of volumes for this dictionary.
=item B<meta>
Returns the raw metadata as a hashref.
=item B<article_count>
Returns the number of unique articles in this volume (if B<article_count_is_volume_total> is true) or in this dictionary (otherwise).
=item B<article_count_is_volume_total>
Returns true if B<article_count> means number of articles in this volume. This is always true since aardtools 0.9.0.
=item B<index_language>
Returns the dictionary's "from" language (two or three letter ISO code)
=item B<article_language>
Returns the dictionary's "to" language (two or three letter ISO code)
=item B<title>
Returns the dictionary title
=item B<version>
Returns the dictionary version
=item B<description>
Returns the dictionary description
=item B<copyright>
Returns the copyright notice
=item B<license>
Returns the full license text
=item B<source>
Returns the dictionary data source
=back
=head1 SEE ALSO
L<http://aarddict.org>, L<http://aarddict.org/aardtools/doc/aardformat.html>
=head1 AUTHOR
Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2014 by Marius Gavrilescu
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.18.2 or,
at your option, any later version of Perl 5 you may have available.
=cut
( run in 0.632 second using v1.01-cache-2.11-cpan-5a3173703d6 )