| 1 | # ABSTRACT: Common code for drivers of tagsets from files in CoNLL 2006 format. | ||
| 2 | # This will be the common ancestor of e.g. BG::Conll and DA::Conll. | ||
| 3 | # It will not be used for tagsets that are derived from non-Conll tagsets, e.g. CS::Conll and EN::Conll. | ||
| 4 | # (Most Conll tagsets are derived from non-Conll tagsets but we do not care unless we also have a driver for the non-Conll ancestor.) | ||
| 5 | # Copyright © 2014 Dan Zeman <zeman@ufal.mff.cuni.cz> | ||
| 6 | |||
| 7 | package Lingua::Interset::Tagset::Conll; | ||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | our $VERSION = '3.014'; | ||
| 11 | |||
| 12 | use utf8; | ||
| 13 | use open ':utf8'; | ||
| 14 | use namespace::autoclean; | ||
| 15 | use Moose; | ||
| 16 | extends 'Lingua::Interset::Tagset'; | ||
| 17 | |||
| 18 | |||
| 19 | |||
| 20 | has 'atoms' => ( isa => 'HashRef', is => 'ro', builder => '_create_atoms', lazy => 1 ); | ||
| 21 | has 'features_all' => ( isa => 'ArrayRef', is => 'ro', builder => '_create_features_all', lazy => 1 ); | ||
| 22 | has 'features_pos' => ( isa => 'HashRef', is => 'ro', builder => '_create_features_pos', lazy => 1 ); | ||
| 23 | |||
| 24 | |||
| 25 | |||
| 26 | #------------------------------------------------------------------------------ | ||
| 27 | # These methods must be defined in the derived classes. | ||
| 28 | #------------------------------------------------------------------------------ | ||
| 29 | sub _create_atoms { confess("The _create_atoms() method must be defined in a class derived from Lingua::Interset::Tagset::Conll"); } | ||
| 30 | sub _create_features_all { confess("The _create_features_all() method must be defined in a class derived from Lingua::Interset::Tagset::Conll"); } | ||
| 31 | sub _create_features_pos { confess("The _create_features_pos() method must be defined in a class derived from Lingua::Interset::Tagset::Conll"); } | ||
| 32 | |||
| 33 | |||
| 34 | |||
| 35 | #------------------------------------------------------------------------------ | ||
| 36 | # Returns the list of surface CoNLL features for a given part of speech. | ||
| 37 | #------------------------------------------------------------------------------ | ||
| 38 | sub get_feature_names | ||
| 39 | { | ||
| 40 | my $self = shift; | ||
| 41 | my $pos = shift; | ||
| 42 | my $feature_names_pos = $self->features_pos(); | ||
| 43 | if(exists($feature_names_pos->{$pos})) | ||
| 44 | { | ||
| 45 | return $feature_names_pos->{$pos}; | ||
| 46 | } | ||
| 47 | else | ||
| 48 | { | ||
| 49 | my @dummy = (); | ||
| 50 | return \@dummy; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | #------------------------------------------------------------------------------ | ||
| 57 | # Decodes a physical tag (string) and returns the corresponding feature | ||
| 58 | # structure. | ||
| 59 | #------------------------------------------------------------------------------ | ||
| 60 | sub decode_conll | ||
| 61 | { | ||
| 62 | my $self = shift; | ||
| 63 | my $tag = shift; | ||
| 64 | my $poskey = shift; # pos | subpos | both; subpos is default | ||
| 65 | $poskey = 'subpos' if(!defined($poskey)); | ||
| 66 | my $delimiter = shift; # Feature-value delimiter. Default is '=' but eu::conll has ':' and Hyderabad tagsets have '-'. | ||
| 67 | $delimiter = '=' if(!defined($delimiter)); | ||
| 68 | my $fs = Lingua::Interset::FeatureStructure->new(); | ||
| 69 | $fs->set_tagset($self->get_tagset_id()); | ||
| 70 | my $atoms = $self->atoms(); | ||
| 71 | my $feature_names = $self->features_all(); | ||
| 72 | # Three components: coarse-grained pos, fine-grained pos, features | ||
| 73 | # Only features with non-empty values appear in the tag. | ||
| 74 | # example: N\nNC\ngender=common|number=sing|case=unmarked|def=indef | ||
| 75 | my ($pos, $subpos, $features) = split(/\s+/, $tag); | ||
| 76 | $features = '' if($features eq '_'); | ||
| 77 | my @features_conll = split(/\|/, $features); | ||
| 78 | my %features_conll; | ||
| 79 | foreach my $f (@features_conll) | ||
| 80 | { | ||
| 81 | if($f =~ m/^([-\w]+)$delimiter(.+)$/) | ||
| 82 | { | ||
| 83 | $features_conll{$1} = $2; | ||
| 84 | } | ||
| 85 | else | ||
| 86 | { | ||
| 87 | $features_conll{$f} = $f; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | my $posvalue = $poskey eq 'pos' ? $pos : $poskey eq 'both' ? "$pos\t$subpos" : $subpos; | ||
| 91 | $atoms->{pos}->decode_and_merge_hard($posvalue, $fs); | ||
| 92 | foreach my $name (@{$feature_names}) | ||
| 93 | { | ||
| 94 | if(defined($features_conll{$name}) && $features_conll{$name} ne '') | ||
| 95 | { | ||
| 96 | $atoms->{$name}->decode_and_merge_hard($features_conll{$name}, $fs); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | return $fs; | ||
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | |||
| 104 | #------------------------------------------------------------------------------ | ||
| 105 | # Takes feature structure and returns the corresponding physical tag (string). | ||
| 106 | # Encodes only features while the CPOS and POS must have been encoded in | ||
| 107 | # tagset-specific code. | ||
| 108 | #------------------------------------------------------------------------------ | ||
| 109 | sub encode_conll | ||
| 110 | { | ||
| 111 | my $self = shift; | ||
| 112 | my $fs = shift; # Lingua::Interset::FeatureStructure | ||
| 113 | # Every CoNLL tagset has a different relation between pos and subpos, so we want them already encoded. | ||
| 114 | my $pos = shift; | ||
| 115 | my $subpos = shift; | ||
| 116 | # The list of feature names may differ for different parts of speech so we want to get a reference to the list. | ||
| 117 | my $feature_names = shift; | ||
| 118 | # By default, feature names are included (e.g. "gender=masc"). Some CoNLL tagsets contain only values without feature names (e.g. "masc"). | ||
| 119 | my $value_only = shift; | ||
| 120 | my $delimiter = shift; # Feature-value delimiter. Default is '=' but eu::conll has ':' and Hyderabad tagsets have '-'. | ||
| 121 | $delimiter = '=' if(!defined($delimiter)); | ||
| 122 | my $atoms = $self->atoms(); | ||
| 123 | my @features; | ||
| 124 | foreach my $name (@{$feature_names}) | ||
| 125 | { | ||
| 126 | # Sanity check: did we define atoms for all features? | ||
| 127 | confess("Undefined atom for feature $name") if(!defined($atoms->{$name})); | ||
| 128 | my $value = $atoms->{$name}->encode($fs); | ||
| 129 | if($value ne '') | ||
| 130 | { | ||
| 131 | # Value adjustments tailored for this physical tagset would come here. | ||
| 132 | if($value_only) | ||
| 133 | { | ||
| 134 | push(@features, $value); | ||
| 135 | } | ||
| 136 | else | ||
| 137 | { | ||
| 138 | push(@features, $name.$delimiter.$value); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | } | ||
| 142 | my $features = '_'; | ||
| 143 | if(scalar(@features) > 0) | ||
| 144 | { | ||
| 145 | $features = join('|', @features); | ||
| 146 | } | ||
| 147 | my $tag = "$pos\t$subpos\t$features"; | ||
| 148 | return $tag; | ||
| 149 | } | ||
| 150 | |||
| 151 | |||
| 152 | |||
| 153 | 1; | ||
| 154 | |||
| 155 | __END__ | ||
| 156 | |||
| 157 | =pod | ||
| 158 | |||
| 159 | =encoding UTF-8 | ||
| 160 | |||
| 161 | =head1 NAME | ||
| 162 | |||
| 163 | Lingua::Interset::Tagset::Conll - Common code for drivers of tagsets from files in CoNLL 2006 format. | ||
| 164 | |||
| 165 | =head1 VERSION | ||
| 166 | |||
| 167 | version 3.014 | ||
| 168 | |||
| 169 | =head1 DESCRIPTION | ||
| 170 | |||
| 171 | Common code for drivers of tagsets from files in the CoNLL 2006 format. | ||
| 172 | These tags always consists of three tab-separated parts: | ||
| 173 | C<pos> (from the CoNLL C<CPOS> column), | ||
| 174 | C<subpos> (from the CoNLL C<POS> column), and | ||
| 175 | C<features> (from the CoNLL C<FEATS> column). | ||
| 176 | Features are always separated by a vertical bar. | ||
| 177 | The values of C<CPOS>, C<POS> and features differ across tagsets/treebanks. | ||
| 178 | Nevertheless, there is some minimal code that repeats for every CoNLL tagset. | ||
| 179 | This module provides the code and is thus intended as a common predecessor of | ||
| 180 | the language-specific CoNLL drivers. | ||
| 181 | |||
| 182 | Most CoNLL tagsets are derived from other pre-existing tagsets that use | ||
| 183 | a different format. If we have a driver for such pre-existing tagset, | ||
| 184 | then its CoNLL variant will be probably derived from that driver rather than | ||
| 185 | from this common CoNLL module. | ||
| 186 | |||
| 187 | =head1 SEE ALSO | ||
| 188 | |||
| 189 | L<Lingua::Interset>, | ||
| 190 | L<Lingua::Interset::Tagset>, | ||
| 191 | L<Lingua::Interset::Tagset::AR::Conll>, | ||
| 192 | L<Lingua::Interset::Tagset::AR::Conll2007>, | ||
| 193 | L<Lingua::Interset::Tagset::BG::Conll>, | ||
| 194 | L<Lingua::Interset::Tagset::DA::Conll>, | ||
| 195 | L<Lingua::Interset::FeatureStructure> | ||
| 196 | |||
| 197 | =head1 AUTHOR | ||
| 198 | |||
| 199 | Dan Zeman <zeman@ufal.mff.cuni.cz> | ||
| 200 | |||
| 201 | =head1 COPYRIGHT AND LICENSE | ||
| 202 | |||
| 203 | This software is copyright (c) 2019 by Univerzita Karlova (Charles University). | ||
| 204 | |||
| 205 | This is free software; you can redistribute it and/or modify it under | ||
| 206 | the same terms as the Perl 5 programming language system itself. | ||
| 207 | |||
| 208 | =cut |