| 1 | # ABSTRACT: A trie-like structure for DZ Interset features and their values. | ||
| 2 | # Copyright © 2012, 2014 Dan Zeman <zeman@ufal.mff.cuni.cz> | ||
| 3 | |||
| 4 | package Lingua::Interset::Trie; | ||
| 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; | ||
| 13 | use MooseX::SemiAffordanceAccessor; # attribute x is written using set_x($value) and read using x() | ||
| 14 | use Carp; | ||
| 15 | |||
| 16 | |||
| 17 | |||
| 18 | has 'root_hash' => ( isa => 'HashRef', is => 'rw', default => sub {{}} ); | ||
| 19 | has 'features' => ( isa => 'ArrayRef', is => 'rw', required => 1 ); | ||
| 20 | |||
| 21 | |||
| 22 | |||
| 23 | #------------------------------------------------------------------------------ | ||
| 24 | # Adds a feature value to the trie. | ||
| 25 | #------------------------------------------------------------------------------ | ||
| 26 | sub add_value | ||
| 27 | { | ||
| 28 | my $self = shift; | ||
| 29 | my $pointer = shift; # hash reference | ||
| 30 | my $value = shift; | ||
| 31 | my $tag = shift; # tag example; only for the last feature | ||
| 32 | if(!exists($pointer->{$value})) | ||
| 33 | { | ||
| 34 | # Last feature (last level of the trie) stores tag examples instead of pointers. | ||
| 35 | if(defined($tag) && $tag ne '') | ||
| 36 | { | ||
| 37 | $pointer->{$value} = $tag; | ||
| 38 | } | ||
| 39 | else | ||
| 40 | { | ||
| 41 | my %new_sub_hash; | ||
| 42 | $pointer->{$value} = \%new_sub_hash; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | return $pointer->{$value}; | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | |||
| 50 | #------------------------------------------------------------------------------ | ||
| 51 | # Advances a trie pointer. | ||
| 52 | #------------------------------------------------------------------------------ | ||
| 53 | sub advance_pointer | ||
| 54 | { | ||
| 55 | my $self = shift; | ||
| 56 | my $pointer = shift; | ||
| 57 | my $feature = shift; | ||
| 58 | my $value = shift; | ||
| 59 | if($feature =~ m/^(tagset|other)$/) | ||
| 60 | { | ||
| 61 | my @keys = keys(%{$pointer}); | ||
| 62 | $value = $keys[0]; | ||
| 63 | } | ||
| 64 | else | ||
| 65 | { | ||
| 66 | if(ref($value) eq 'ARRAY') | ||
| 67 | { | ||
| 68 | $value = join('|', @{$value}); | ||
| 69 | } | ||
| 70 | if(!exists($pointer->{$value})) | ||
| 71 | { | ||
| 72 | confess("Dead trie pointer."); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | return $pointer->{$value}; | ||
| 76 | } | ||
| 77 | |||
| 78 | |||
| 79 | |||
| 80 | #------------------------------------------------------------------------------ | ||
| 81 | # Returns permitted feature values in a form suitable for printing. This may be | ||
| 82 | # useful for debugging. | ||
| 83 | #------------------------------------------------------------------------------ | ||
| 84 | sub as_string | ||
| 85 | { | ||
| 86 | my $self = shift; | ||
| 87 | my $fs = new Lingua::Interset::FeatureStructure(); | ||
| 88 | return $self->_get_permitted_combinations_as_text_recursion($fs, 0, $self->root_hash()); | ||
| 89 | } | ||
| 90 | |||
| 91 | |||
| 92 | |||
| 93 | #------------------------------------------------------------------------------ | ||
| 94 | # Recursive part of printing permitted feature value combinations. | ||
| 95 | #------------------------------------------------------------------------------ | ||
| 96 | sub _get_permitted_combinations_as_text_recursion | ||
| 97 | { | ||
| 98 | my $self = shift; | ||
| 99 | my $fs0 = shift; # Lingua::Interset::FeatureStructure | ||
| 100 | my $i = shift; # index of the next feature to process | ||
| 101 | my $pointer = shift; # reference to the current hash in the trie | ||
| 102 | my @features = @{$self->features()}; | ||
| 103 | return if($i>$#features); | ||
| 104 | my $string; | ||
| 105 | # Loop through permitted values of the next feature. | ||
| 106 | my @values = sort(keys(%{$pointer})); | ||
| 107 | foreach my $value (@values) | ||
| 108 | { | ||
| 109 | # Add the value of the next feature to the feature structure. | ||
| 110 | my $fs1 = $fs0->duplicate(); | ||
| 111 | $fs1->set($features[$i], $value); | ||
| 112 | # If this is the last feature, print the feature structure. | ||
| 113 | if($i==$#features) | ||
| 114 | { | ||
| 115 | $string .= $fs1->as_string()."\n"; | ||
| 116 | } | ||
| 117 | # Otherwise, go to the next feature. | ||
| 118 | else | ||
| 119 | { | ||
| 120 | $string .= $self->_get_permitted_combinations_as_text_recursion($fs1, $i+1, $pointer->{$value}); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | return $string; | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | |||
| 128 | #------------------------------------------------------------------------------ | ||
| 129 | # If a feature structure is permitted, returns an example of a known tag that | ||
| 130 | # generates the same feature structure. Otherwise returns an empty string. | ||
| 131 | #------------------------------------------------------------------------------ | ||
| 132 | sub get_tag_example | ||
| 133 | { | ||
| 134 | my $self = shift; | ||
| 135 | my $fs = shift; # Lingua::Interset::FeatureStructure | ||
| 136 | my @features = @{$self->features()}; | ||
| 137 | my $pointer = $self->root_hash(); | ||
| 138 | foreach my $feature (@features) | ||
| 139 | { | ||
| 140 | my $value = $fs->get_joined($feature); | ||
| 141 | # advance_pointer() will die if we supply a forbidden feature value so we must check it here. | ||
| 142 | if(exists($pointer->{$value}) || $feature =~ m/^(tagset|other)$/) | ||
| 143 | { | ||
| 144 | $pointer = $self->advance_pointer($pointer, $feature, $value); | ||
| 145 | } | ||
| 146 | else | ||
| 147 | { | ||
| 148 | return "Forbidden value $value of feature $feature"; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | # The last hash in the trie (the one for the last feature) points to examples of tags. | ||
| 152 | # Thus if we are here, our $pointer is no longer a hash reference but a scalar string. | ||
| 153 | return $pointer; | ||
| 154 | } | ||
| 155 | |||
| 156 | |||
| 157 | |||
| 158 | 1; | ||
| 159 | |||
| 160 | __END__ | ||
| 161 | |||
| 162 | =pod | ||
| 163 | |||
| 164 | =encoding UTF-8 | ||
| 165 | |||
| 166 | =head1 NAME | ||
| 167 | |||
| 168 | Lingua::Interset::Trie - A trie-like structure for DZ Interset features and their values. | ||
| 169 | |||
| 170 | =head1 VERSION | ||
| 171 | |||
| 172 | version 3.015 | ||
| 173 | |||
| 174 | =head1 SYNOPSIS | ||
| 175 | |||
| 176 | use Lingua::Interset::Tagset::EN::Penn; | ||
| 177 | |||
| 178 | my $ts = Lingua::Interset::Tagset::EN::Penn->new(); | ||
| 179 | # Get a Lingua::Interset::Trie object $permitted and print all feature structures | ||
| 180 | # that the tagset en::penn can generate. | ||
| 181 | my $permitted = $ts->permitted_structures(); | ||
| 182 | print($permitted->as_string(), "----------\n"); | ||
| 183 | |||
| 184 | =head1 DESCRIPTION | ||
| 185 | |||
| 186 | The C<Trie> class defines a trie-like data structure for DZ Interset features | ||
| 187 | and their values. It is an auxiliary data structure that an outside user should | ||
| 188 | not need to use directly. | ||
| 189 | |||
| 190 | It is used to describe all feature-value combinations that | ||
| 191 | are permitted under a given tagset. (Example: If the prefix already traversed | ||
| 192 | in the trie indicates that we have a noun, with subtype of proper noun, what | ||
| 193 | are the possible values of the next feature, say, gender?) | ||
| 194 | |||
| 195 | The trie assumes that features are ordered according to their priority. | ||
| 196 | However, the priorities are defined outside the trie, by default in the | ||
| 197 | FeatureStructure class, or they may be overriden in a Tagset subclass. | ||
| 198 | The trie can store features in any order. | ||
| 199 | |||
| 200 | =head1 ATTRIBUTES | ||
| 201 | |||
| 202 | =head2 features | ||
| 203 | |||
| 204 | An array reference. Lists the features in the order in which their values appear | ||
| 205 | in the trie. This may be the default order according to | ||
| 206 | C<< Lingua::Interset::FeatureStructure->priority_features() >>, | ||
| 207 | or a custom order. | ||
| 208 | |||
| 209 | =head2 root_hash | ||
| 210 | |||
| 211 | The trie structure is implemented as a tree of hashes. | ||
| 212 | The root hash corresponds to the first feature. | ||
| 213 | Its keys are values of the feature and each of them leads to a second-level hash. | ||
| 214 | All second-level hashes correspond to the second feature, their keys are values of that feature etc. | ||
| 215 | It is interpreted as a sequence of feature queries: | ||
| 216 | I<If feature F1 has value X, then if feature F2 has value Y, then...> | ||
| 217 | |||
| 218 | We need a pointer when traversing the trie, | ||
| 219 | and the pointer is always a reference to one of the hashes. | ||
| 220 | The C<root_hash> attribute is our entry pointer where we start the traversal. | ||
| 221 | |||
| 222 | =head1 METHODS | ||
| 223 | |||
| 224 | =head2 add_value() | ||
| 225 | |||
| 226 | $trie->add_value ($pointer, $value[, $tag_example]); | ||
| 227 | |||
| 228 | Adds a feature value to the trie. It does not need to know the feature name. | ||
| 229 | It takes the feature value and the pointer to the trie level corresponding to | ||
| 230 | the feature (reference to an existing hash). If the hash already has a key | ||
| 231 | corresponding to the value, the method only advances to the sub-hash | ||
| 232 | referenced by the value, and returns the new pointer. If there is no such key, | ||
| 233 | the method first creates the new sub-hash and then advances the pointer. | ||
| 234 | |||
| 235 | For the last feature we can optionally provide an example of a tag where this | ||
| 236 | combination of feature values occurred. It may be useful for debugging, when | ||
| 237 | we see a permitted feature structure but do not understand how does it come | ||
| 238 | that it is permitted. | ||
| 239 | |||
| 240 | =head2 advance_pointer() | ||
| 241 | |||
| 242 | $trie->advance_pointer ($pointer, $feature, $value); | ||
| 243 | |||
| 244 | Advances a trie pointer. | ||
| 245 | Normally it observes the value of the current feature; | ||
| 246 | however, the features C<tagset> and C<other> get special treatment | ||
| 247 | (any value is permitted). | ||
| 248 | |||
| 249 | =head2 as_string() | ||
| 250 | |||
| 251 | Returns permitted feature values in a form suitable for printing. | ||
| 252 | This may be useful for debugging. | ||
| 253 | |||
| 254 | =head2 get_tag_example() | ||
| 255 | |||
| 256 | $trie->get_tag_example ($feature_structure); | ||
| 257 | |||
| 258 | Takes a L<Lingua::Interset::FeatureStructure> object. | ||
| 259 | If this is a permitted structure according to the trie, | ||
| 260 | the method returns the tag example that has been stored in the last-level hash of the trie. | ||
| 261 | |||
| 262 | Otherwise it returns an error message. | ||
| 263 | This is a debugging method and it will not throw exceptions on forbidden values. | ||
| 264 | |||
| 265 | =head1 SEE ALSO | ||
| 266 | |||
| 267 | L<Lingua::Interset::Tagset>, | ||
| 268 | L<Lingua::Interset::FeatureStructure> | ||
| 269 | |||
| 270 | =head1 AUTHOR | ||
| 271 | |||
| 272 | Dan Zeman <zeman@ufal.mff.cuni.cz> | ||
| 273 | |||
| 274 | =head1 COPYRIGHT AND LICENSE | ||
| 275 | |||
| 276 | This software is copyright (c) 2019 by Univerzita Karlova (Charles University). | ||
| 277 | |||
| 278 | This is free software; you can redistribute it and/or modify it under | ||
| 279 | the same terms as the Perl 5 programming language system itself. | ||
| 280 | |||
| 281 | =cut |