| 1 | # ABSTRACT: A temporary envelope that provides access to the old (Interset 1.0) drivers from Interset 2.0. | ||
| 2 | # Once all the old drivers are ported to Interset 2.0, this module will be removed. | ||
| 3 | # Copyright © 2014 Dan Zeman <zeman@ufal.mff.cuni.cz> | ||
| 4 | |||
| 5 | package Lingua::Interset::OldTagsetDriver; | ||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | our $VERSION = '3.014'; | ||
| 9 | |||
| 10 | use utf8; | ||
| 11 | use open ':utf8'; | ||
| 12 | use namespace::autoclean; | ||
| 13 | use Moose; | ||
| 14 | use Lingua::Interset; | ||
| 15 | use Lingua::Interset::FeatureStructure; | ||
| 16 | extends 'Lingua::Interset::Tagset'; | ||
| 17 | |||
| 18 | |||
| 19 | |||
| 20 | has 'driver' => ( isa => 'Str', is => 'ro', required => 1 ); # e.g. 'cs::pdt'; not 'tagset::cs::pdt' (the 'tagset' part will be inserted automatically) | ||
| 21 | # The following attributes are required but they will be automatically derived from 'driver' in BUILDARGS() below. | ||
| 22 | has 'decode_function' => ( isa => 'CodeRef', is => 'ro', required => 1 ); | ||
| 23 | has 'encode_function' => ( isa => 'CodeRef', is => 'ro', required => 1 ); | ||
| 24 | has 'list_function' => ( isa => 'CodeRef', is => 'ro', required => 1 ); | ||
| 25 | |||
| 26 | |||
| 27 | |||
| 28 | #------------------------------------------------------------------------------ | ||
| 29 | # This block will be called before object construction. It will take the driver | ||
| 30 | # attribute from the user and use it to get the three function attributes. Then | ||
| 31 | # it will pass all the attributes to the constructor. | ||
| 32 | #------------------------------------------------------------------------------ | ||
| 33 | around BUILDARGS => sub | ||
| 34 | { | ||
| 35 | my $orig = shift; | ||
| 36 | my $class = shift; | ||
| 37 | # Call the default BUILDARGS in Moose::Object. It will take care of distinguishing between a hash reference and a plain hash. | ||
| 38 | my $attr = $class->$orig(@_); | ||
| 39 | if($attr->{driver}) | ||
| 40 | { | ||
| 41 | my $driver_hash = Lingua::Interset::get_driver_hash(); | ||
| 42 | if(!exists($driver_hash->{$attr->{driver}})) | ||
| 43 | { | ||
| 44 | confess("Unknown tagset driver '$attr->{driver}'"); | ||
| 45 | } | ||
| 46 | if(!$driver_hash->{$attr->{driver}}{old}) | ||
| 47 | { | ||
| 48 | confess("OldTagsetDriver can be used only for old drivers but '$attr->{driver}' is new"); | ||
| 49 | } | ||
| 50 | my $package = $driver_hash->{$attr->{driver}}{package}; | ||
| 51 | my $eval = <<_end_of_eval_ | ||
| 52 | { | ||
| 53 | use ${package}; | ||
| 54 | my \$decode = \\&${package}::decode; | ||
| 55 | my \$encode = \\&${package}::encode; | ||
| 56 | my \$list = \\&${package}::list; | ||
| 57 | return (\$decode, \$encode, \$list); | ||
| 58 | } | ||
| 59 | _end_of_eval_ | ||
| 60 | ; | ||
| 61 | ###!!! Perlcritic suggests that the following line be | ||
| 62 | ###!!! my ($decode, $encode, $list) = eval { $eval }; | ||
| 63 | ###!!! so that it is not compiled every time it is called. | ||
| 64 | ###!!! But the suggested version does not work on Windows! | ||
| 65 | my ($decode, $encode, $list) = eval $eval; ## no critic | ||
| 66 | if($@) | ||
| 67 | { | ||
| 68 | confess("$@\nEval failed"); | ||
| 69 | } | ||
| 70 | # Now add the references to the driver functions to the attribute hash. | ||
| 71 | $attr->{decode_function} = $decode; | ||
| 72 | $attr->{encode_function} = $encode; | ||
| 73 | $attr->{list_function} = $list; | ||
| 74 | } | ||
| 75 | return $attr; | ||
| 76 | }; | ||
| 77 | |||
| 78 | |||
| 79 | |||
| 80 | #------------------------------------------------------------------------------ | ||
| 81 | # Decodes a physical tag (string) and returns the corresponding feature | ||
| 82 | # structure. | ||
| 83 | #------------------------------------------------------------------------------ | ||
| 84 | sub decode | ||
| 85 | { | ||
| 86 | my $self = shift; | ||
| 87 | my $tag = shift; | ||
| 88 | my $fs = Lingua::Interset::FeatureStructure->new(); | ||
| 89 | my $fs_hash = &{$self->decode_function()}($tag); | ||
| 90 | translate($fs_hash, 12); | ||
| 91 | $fs->set_hash($fs_hash); | ||
| 92 | return $fs; | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | |||
| 97 | #------------------------------------------------------------------------------ | ||
| 98 | # Takes feature structure and returns the corresponding physical tag (string). | ||
| 99 | #------------------------------------------------------------------------------ | ||
| 100 | sub encode | ||
| 101 | { | ||
| 102 | my $self = shift; | ||
| 103 | my $fs = shift; # Lingua::Interset::FeatureStructure | ||
| 104 | my $fs_hash = $fs->get_hash(); | ||
| 105 | translate($fs_hash, 21); | ||
| 106 | # Call non-strict ("1") encoding of the old driver. We use a different method ("encode_strict") for strict encoding now. | ||
| 107 | my $tag = &{$self->encode_function()}($fs_hash, 1); | ||
| 108 | return $tag; | ||
| 109 | } | ||
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | #------------------------------------------------------------------------------ | ||
| 114 | # Returns reference to list of known tags. | ||
| 115 | #------------------------------------------------------------------------------ | ||
| 116 | sub list | ||
| 117 | { | ||
| 118 | my $self = shift; | ||
| 119 | return &{$self->list_function()}(); | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | |||
| 124 | #------------------------------------------------------------------------------ | ||
| 125 | # Some features and values changed between Interset 1.0 and 2.0. The feature | ||
| 126 | # space has been changing during whole history of Interset and tagset drivers | ||
| 127 | # had to be adjusted to each change; however, we are not going to adapt old | ||
| 128 | # drivers to features new in 2.0 (instead, the whole drivers should be ported). | ||
| 129 | # We must thus translate the changed features while this temporary envelope to | ||
| 130 | # old drivers is used. | ||
| 131 | #------------------------------------------------------------------------------ | ||
| 132 | sub translate | ||
| 133 | { | ||
| 134 | my $hash = shift; # hash reference, not Lingua::Interset::FeatureStructure | ||
| 135 | my $direction = shift; # 12 or 21 ... from version 1 to 2 or from 2 to 1 | ||
| 136 | if($direction != 12 && $direction != 21) | ||
| 137 | { | ||
| 138 | confess("Direction is neither 12 nor 21"); | ||
| 139 | } | ||
| 140 | my @translations = | ||
| 141 | ( | ||
| 142 | ['subpos', 'prop' => 'nountype', 'prop'], | ||
| 143 | ['subpos', 'det' => 'adjtype', 'det'], | ||
| 144 | ['subpos', 'coor' => 'conjtype', 'coor'], | ||
| 145 | ['subpos', 'sub' => 'conjtype', 'sub'], | ||
| 146 | ); | ||
| 147 | foreach my $feature (keys(%{$hash})) | ||
| 148 | { | ||
| 149 | foreach my $translation (@translations) | ||
| 150 | { | ||
| 151 | my ($sf, $sv, $tf, $tv); | ||
| 152 | if($direction==12) | ||
| 153 | { | ||
| 154 | ($sf, $sv, $tf, $tv) = @{$translation}; | ||
| 155 | } | ||
| 156 | elsif($direction==21) | ||
| 157 | { | ||
| 158 | ($tf, $tv, $sf, $sv) = @{$translation}; | ||
| 159 | } | ||
| 160 | # Translate. | ||
| 161 | if(defined($hash->{$sf}) && $hash->{$sf} eq $sv) | ||
| 162 | { | ||
| 163 | delete($hash->{$sf}); | ||
| 164 | $hash->{$tf} = $tv; | ||
| 165 | } | ||
| 166 | } | ||
| 167 | } | ||
| 168 | return $hash; | ||
| 169 | } | ||
| 170 | |||
| 171 | |||
| 172 | |||
| 173 | 1; | ||
| 174 | |||
| 175 | __END__ | ||
| 176 | |||
| 177 | =pod | ||
| 178 | |||
| 179 | =encoding UTF-8 | ||
| 180 | |||
| 181 | =head1 NAME | ||
| 182 | |||
| 183 | Lingua::Interset::OldTagsetDriver - A temporary envelope that provides access to the old (Interset 1.0) drivers from Interset 2.0. | ||
| 184 | |||
| 185 | =head1 VERSION | ||
| 186 | |||
| 187 | version 3.014 | ||
| 188 | |||
| 189 | =head1 SYNOPSIS | ||
| 190 | |||
| 191 | # (No need to | ||
| 192 | # use tagset::en::conll; | ||
| 193 | # here. It will be taken care of within OldTagsetDriver.) | ||
| 194 | use Lingua::Interset::OldTagsetDriver; | ||
| 195 | my $driver = Lingua::Interset::OldTagsetDriver->new(driver => 'en::conll'); | ||
| 196 | my $fs = $driver->decode("NN\tNN\t_"); | ||
| 197 | |||
| 198 | =head1 DESCRIPTION | ||
| 199 | |||
| 200 | Provides object envelope for an old, non-object-oriented driver from Interset 1.0. | ||
| 201 | This makes the old drivers at least partially usable until they are fully ported to Interset 2.0. | ||
| 202 | Note however that the old drivers use Interset features and/or values that have been changed in the new version. | ||
| 203 | |||
| 204 | =head1 METHODS | ||
| 205 | |||
| 206 | =head2 decode() | ||
| 207 | |||
| 208 | my $fs = $driver->decode ($tag); | ||
| 209 | |||
| 210 | Takes a tag (string) and returns a L<Lingua::Interset::FeatureStructure> object | ||
| 211 | with corresponding feature values set. | ||
| 212 | |||
| 213 | =head2 encode() | ||
| 214 | |||
| 215 | my $tag = $driver->encode ($fs); | ||
| 216 | |||
| 217 | Takes a L<Lingua::Interset::FeatureStructure> object and | ||
| 218 | returns the tag (string) in the given tagset that corresponds to the feature values. | ||
| 219 | Note that some features may be ignored because they cannot be represented | ||
| 220 | in the given tagset. | ||
| 221 | |||
| 222 | =head2 list() | ||
| 223 | |||
| 224 | my $list_of_tags = $driver->list(); | ||
| 225 | |||
| 226 | Returns the reference to the list of all known tags in this particular tagset. | ||
| 227 | This is not directly needed to decode, encode or convert tags but it is very useful | ||
| 228 | for testing and advanced operations over the tagset. | ||
| 229 | Note however that many tagset drivers contain only an approximate list, | ||
| 230 | created by collecting tag occurrences in some corpus. | ||
| 231 | |||
| 232 | =head1 SEE ALSO | ||
| 233 | |||
| 234 | L<Lingua::Interset::Tagset>, | ||
| 235 | L<Lingua::Interset::FeatureStructure> | ||
| 236 | |||
| 237 | =head1 AUTHOR | ||
| 238 | |||
| 239 | Dan Zeman <zeman@ufal.mff.cuni.cz> | ||
| 240 | |||
| 241 | =head1 COPYRIGHT AND LICENSE | ||
| 242 | |||
| 243 | This software is copyright (c) 2019 by Univerzita Karlova (Charles University). | ||
| 244 | |||
| 245 | This is free software; you can redistribute it and/or modify it under | ||
| 246 | the same terms as the Perl 5 programming language system itself. | ||
| 247 | |||
| 248 | =cut |