| 1 | # ABSTRACT: Implements a converter between two physical tagsets via Interset. | ||
| 2 | # Copyright © 2015 Univerzita Karlova v Praze / Dan Zeman <zeman@ufal.mff.cuni.cz> | ||
| 3 | |||
| 4 | package Lingua::Interset::Converter; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | our $VERSION = '3.015'; | ||
| 8 | |||
| 9 | use utf8; | ||
| 10 | use open ':utf8'; | ||
| 11 | use namespace::autoclean; | ||
| 12 | use Moose 2; | ||
| 13 | use MooseX::SemiAffordanceAccessor; # attribute x is written using set_x($value) and read using x() | ||
| 14 | use Lingua::Interset qw(decode encode list); | ||
| 15 | |||
| 16 | has 'from' => ( isa => 'Str', is => 'ro', required => 1, documentation => 'Source tagset identifier, e.g. cs::multext' ); | ||
| 17 | has 'to' => ( isa => 'Str', is => 'ro', required => 1, documentation => 'Target tagset identifier, e.g. cs::pdt' ); | ||
| 18 | has '_cache' => ( isa => 'HashRef', is => 'ro', default => sub { return {} } ); | ||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | #------------------------------------------------------------------------------ | ||
| 23 | # Converts tag from tagset A to tagset B via Interset. Caches tags converted | ||
| 24 | # previously. | ||
| 25 | #------------------------------------------------------------------------------ | ||
| 26 | sub convert | ||
| 27 | { | ||
| 28 | my $self = shift; | ||
| 29 | my $stag = shift; | ||
| 30 | my $cache = $self->_cache(); | ||
| 31 | my $ttag = $cache->{$stag}; | ||
| 32 | if(!defined($ttag)) | ||
| 33 | { | ||
| 34 | my $stagset = $self->from(); | ||
| 35 | my $ttagset = $self->to(); | ||
| 36 | $ttag = encode($ttagset, decode($stagset, $stag)); | ||
| 37 | $cache->{$stag} = $ttag; | ||
| 38 | } | ||
| 39 | return $ttag; | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | |||
| 44 | 1; | ||
| 45 | |||
| 46 | __END__ | ||
| 47 | |||
| 48 | =pod | ||
| 49 | |||
| 50 | =encoding UTF-8 | ||
| 51 | |||
| 52 | =head1 NAME | ||
| 53 | |||
| 54 | Lingua::Interset::Converter - Implements a converter between two physical tagsets via Interset. | ||
| 55 | |||
| 56 | =head1 VERSION | ||
| 57 | |||
| 58 | version 3.015 | ||
| 59 | |||
| 60 | =head1 SYNOPSIS | ||
| 61 | |||
| 62 | use Lingua::Interset::Converter; | ||
| 63 | |||
| 64 | my $c = new Lingua::Interset::Converter ('from' => 'cs::multext', 'to' => 'cs::pdt'); | ||
| 65 | while (<CONLL_IN>) | ||
| 66 | { | ||
| 67 | chomp (); | ||
| 68 | my @fields = split (/\t/, $_); | ||
| 69 | my $source_tag = $fields[4]; | ||
| 70 | $fields[4] = $c->convert ($source_tag); | ||
| 71 | print (join("\t", @fields), "\n"); | ||
| 72 | } | ||
| 73 | |||
| 74 | =head1 DESCRIPTION | ||
| 75 | |||
| 76 | C<Converter> is a simple class that implements Interset-based conversion of tags | ||
| 77 | between two physical tagsets. It includes caching, which will improve performance | ||
| 78 | when converting tags in a large corpus. | ||
| 79 | |||
| 80 | =head1 ATTRIBUTES | ||
| 81 | |||
| 82 | =head2 from | ||
| 83 | |||
| 84 | Identifier of the source tagset (composed of language code and tagset id, all | ||
| 85 | lowercase, for example C<cs::multext>). It must be provided upon construction. | ||
| 86 | |||
| 87 | =head2 from | ||
| 88 | |||
| 89 | Identifier of the target tagset (composed of language code and tagset id, all | ||
| 90 | lowercase, for example C<cs::pdt>). It must be provided upon construction. | ||
| 91 | |||
| 92 | =head1 METHODS | ||
| 93 | |||
| 94 | =head2 convert() | ||
| 95 | |||
| 96 | my $tag1 = convert ($tag0); | ||
| 97 | |||
| 98 | Converts tag from the source tagset to the target tagset via Interset. | ||
| 99 | Tags once converted are cached so the (potentially costly) Interset decoding-encoding | ||
| 100 | methods are called only once per source tag. | ||
| 101 | |||
| 102 | =head1 SEE ALSO | ||
| 103 | |||
| 104 | L<Lingua::Interset> | ||
| 105 | |||
| 106 | =head1 AUTHOR | ||
| 107 | |||
| 108 | Dan Zeman <zeman@ufal.mff.cuni.cz> | ||
| 109 | |||
| 110 | =head1 COPYRIGHT AND LICENSE | ||
| 111 | |||
| 112 | This software is copyright (c) 2019 by Univerzita Karlova (Charles University). | ||
| 113 | |||
| 114 | This is free software; you can redistribute it and/or modify it under | ||
| 115 | the same terms as the Perl 5 programming language system itself. | ||
| 116 | |||
| 117 | =cut |