| 1 | # ABSTRACT: DZ Interset is a universal morphosyntactic feature set to which all tagsets of all corpora/languages can be mapped. | ||
| 2 | # Copyright © 2007-2014 Univerzita Karlova v Praze / Dan Zeman <zeman@ufal.mff.cuni.cz> | ||
| 3 | |||
| 4 | package Lingua::Interset; | ||
| 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 | # Allow the user to import the core functions into their namespace by stating | ||
| 15 | # use Lingua::Interset qw(decode encode list); | ||
| 16 | use Exporter::Easy ( OK => [ 'decode', 'encode', 'encode_strict', 'list', 'find_drivers', 'find_tagsets', 'hash_drivers', 'get_driver_object' ] ); | ||
| 17 | |||
| 18 | |||
| 19 | |||
| 20 | ############################################################################### | ||
| 21 | # DRIVER FUNCTIONS WITH PARAMETERIZED DRIVERS | ||
| 22 | ############################################################################### | ||
| 23 | |||
| 24 | |||
| 25 | |||
| 26 | # Static reference to the list of all installed tagset drivers. | ||
| 27 | # The list will be built lazily, see find_drivers(). | ||
| 28 | my $driver_list = undef; | ||
| 29 | my $driver_hash = undef; # indexed by tagset id | ||
| 30 | |||
| 31 | |||
| 32 | |||
| 33 | #------------------------------------------------------------------------------ | ||
| 34 | # Decodes a tag using a particular driver. | ||
| 35 | #------------------------------------------------------------------------------ | ||
| 36 | sub decode | ||
| 37 | { | ||
| 38 | my $tagset = shift; # e.g. "cs::pdt" | ||
| 39 | my $driver = get_driver_object($tagset); | ||
| 40 | return $driver->decode(@_); | ||
| 41 | } | ||
| 42 | |||
| 43 | |||
| 44 | |||
| 45 | #------------------------------------------------------------------------------ | ||
| 46 | # Encodes a tag using a particular driver. | ||
| 47 | #------------------------------------------------------------------------------ | ||
| 48 | sub encode | ||
| 49 | { | ||
| 50 | my $tagset = shift; # e.g. "cs::pdt" | ||
| 51 | my $driver = get_driver_object($tagset); | ||
| 52 | return $driver->encode(@_); | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | #------------------------------------------------------------------------------ | ||
| 58 | # Encodes a tag using a particular driver and strict encoding (only known | ||
| 59 | # tags). | ||
| 60 | #------------------------------------------------------------------------------ | ||
| 61 | sub encode_strict | ||
| 62 | { | ||
| 63 | my $tagset = shift; # e.g. "cs::pdt" | ||
| 64 | my $driver = get_driver_object($tagset); | ||
| 65 | return $driver->encode_strict(@_); | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | |||
| 70 | #------------------------------------------------------------------------------ | ||
| 71 | # Lists all tags of a tag set. | ||
| 72 | #------------------------------------------------------------------------------ | ||
| 73 | sub list | ||
| 74 | { | ||
| 75 | my $tagset = shift; # e.g. "cs::pdt" | ||
| 76 | my $driver = get_driver_object($tagset); | ||
| 77 | return $driver->list(@_); | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | |||
| 82 | #------------------------------------------------------------------------------ | ||
| 83 | # Creates and returns a tagset driver object for a given tagset. | ||
| 84 | #------------------------------------------------------------------------------ | ||
| 85 | sub get_driver_object | ||
| 86 | { | ||
| 87 | my $tagset = shift; # e.g. "cs::pdt" | ||
| 88 | my $driver_hash = get_driver_hash(); | ||
| 89 | if(!exists($driver_hash->{$tagset})) | ||
| 90 | { | ||
| 91 | confess("Unknown tagset driver '$tagset'"); | ||
| 92 | } | ||
| 93 | # We will cache the driver objects for tagsets. We do not want to construct them again and again. | ||
| 94 | if(!defined($driver_hash->{$tagset}{driver})) | ||
| 95 | { | ||
| 96 | my $package = $driver_hash->{$tagset}{package}; | ||
| 97 | my $eval; | ||
| 98 | if($driver_hash->{$tagset}{old}) | ||
| 99 | { | ||
| 100 | $eval = <<_end_of_old_eval_ | ||
| 101 | { | ||
| 102 | use ${package}; | ||
| 103 | use Lingua::Interset::OldTagsetDriver; | ||
| 104 | my \$object = Lingua::Interset::OldTagsetDriver->new(driver => '${tagset}'); | ||
| 105 | return \$object; | ||
| 106 | } | ||
| 107 | _end_of_old_eval_ | ||
| 108 | ; | ||
| 109 | } | ||
| 110 | else # new driver | ||
| 111 | { | ||
| 112 | $eval = <<_end_of_eval_ | ||
| 113 | { | ||
| 114 | use ${package}; | ||
| 115 | my \$object = ${package}->new(); | ||
| 116 | return \$object; | ||
| 117 | } | ||
| 118 | _end_of_eval_ | ||
| 119 | ; | ||
| 120 | } | ||
| 121 | ###!!! Perlcritic suggests that the following line be | ||
| 122 | ###!!! my $object = eval { $eval }; | ||
| 123 | ###!!! so that it is not compiled every time it is called. | ||
| 124 | ###!!! But the suggested version does not work on Windows! | ||
| 125 | my $object = eval $eval; ## no critic | ||
| 126 | if($@) | ||
| 127 | { | ||
| 128 | confess("$@\nEval failed"); | ||
| 129 | } | ||
| 130 | $driver_hash->{$tagset}{driver} = $object; | ||
| 131 | } | ||
| 132 | my $object = $driver_hash->{$tagset}{driver}; | ||
| 133 | if(!defined($object) || ref($object) !~ m/^Lingua::Interset::/) | ||
| 134 | { | ||
| 135 | confess("Did not succeed in creating driver object for '$tagset' (".ref($object)."/$object)"); | ||
| 136 | } | ||
| 137 | return $object; | ||
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | |||
| 142 | #------------------------------------------------------------------------------ | ||
| 143 | # Tries to enumerate existing tagset drivers. Searches for relevant folders in | ||
| 144 | # @INC paths. | ||
| 145 | #------------------------------------------------------------------------------ | ||
| 146 | sub find_drivers | ||
| 147 | { | ||
| 148 | if(!defined($driver_list)) | ||
| 149 | { | ||
| 150 | $driver_list = _find_drivers(); | ||
| 151 | } | ||
| 152 | return $driver_list; | ||
| 153 | } | ||
| 154 | sub _find_drivers | ||
| 155 | { | ||
| 156 | my @drivers; | ||
| 157 | foreach my $path (@INC) | ||
| 158 | { | ||
| 159 | # Old drivers (Interset 1.0) are in the "tagset" folder. | ||
| 160 | # We will continue using them until all have been ported to Interset 2.0. | ||
| 161 | my $tpath = "$path/tagset"; | ||
| 162 | if(-d $tpath) | ||
| 163 | { | ||
| 164 | opendir(DIR, $tpath) or confess("Cannot read folder $tpath: $!\n"); | ||
| 165 | my @subdirs = readdir(DIR); | ||
| 166 | closedir(DIR); | ||
| 167 | foreach my $sd (@subdirs) | ||
| 168 | { | ||
| 169 | my $sdpath = "$tpath/$sd"; | ||
| 170 | if(-d $sdpath && $sd !~ m/^\.\.?$/) | ||
| 171 | { | ||
| 172 | # It is possible that a subfolder of $PERLLIB is not readable. | ||
| 173 | # We cannot complain about it. We will just silently proceed to the next available folder. | ||
| 174 | opendir(DIR, $sdpath) or next; | ||
| 175 | my @files = readdir(DIR); | ||
| 176 | closedir(DIR); | ||
| 177 | foreach my $file (@files) | ||
| 178 | { | ||
| 179 | my $fpath = "$sdpath/$file"; | ||
| 180 | my $driver = $file; | ||
| 181 | if(-f $fpath && $driver =~ s/\.pm$//) | ||
| 182 | { | ||
| 183 | $driver = $sd."::".$driver; | ||
| 184 | my %record = | ||
| 185 | ( | ||
| 186 | 'old' => 1, | ||
| 187 | 'tagset' => $driver, | ||
| 188 | 'package' => "tagset::$driver", | ||
| 189 | 'path' => $fpath | ||
| 190 | ); | ||
| 191 | push(@drivers, \%record); | ||
| 192 | } | ||
| 193 | } | ||
| 194 | } | ||
| 195 | } | ||
| 196 | } | ||
| 197 | # New drivers (Interset 2.0) are in the "Lingua/Interset" folder. | ||
| 198 | # Not everything in this folder is a driver! But subfolders lead to drivers, the additional stuff are files, not folders. | ||
| 199 | my $lipath = "$path/Lingua/Interset/Tagset"; | ||
| 200 | if(-d $lipath) | ||
| 201 | { | ||
| 202 | opendir(DIR, $lipath) or confess("Cannot read folder $lipath: $!\n"); | ||
| 203 | my @subdirs = readdir(DIR); | ||
| 204 | closedir(DIR); | ||
| 205 | foreach my $sd (@subdirs) | ||
| 206 | { | ||
| 207 | my $sdpath = "$lipath/$sd"; | ||
| 208 | if(-d $sdpath && $sd !~ m/^\.\.?$/) | ||
| 209 | { | ||
| 210 | # It is possible that a subfolder of $PERLLIB is not readable. | ||
| 211 | # We cannot complain about it. We will just silently proceed to the next available folder. | ||
| 212 | opendir(DIR, $sdpath) or next; | ||
| 213 | my @files = readdir(DIR); | ||
| 214 | closedir(DIR); | ||
| 215 | foreach my $file (@files) | ||
| 216 | { | ||
| 217 | my $fpath = "$sdpath/$file"; | ||
| 218 | my $driver = $file; | ||
| 219 | if(-f $fpath && $driver =~ s/\.pm$//) | ||
| 220 | { | ||
| 221 | my $driver_uppercased = $sd.'::'.$driver; | ||
| 222 | my $driver_lowercased = lc($driver_uppercased); | ||
| 223 | my %record = | ||
| 224 | ( | ||
| 225 | 'old' => 0, | ||
| 226 | 'tagset' => $driver_lowercased, | ||
| 227 | 'package' => "Lingua::Interset::Tagset::$driver_uppercased", | ||
| 228 | 'path' => $fpath | ||
| 229 | ); | ||
| 230 | push(@drivers, \%record); | ||
| 231 | } | ||
| 232 | } | ||
| 233 | } | ||
| 234 | } | ||
| 235 | } | ||
| 236 | } | ||
| 237 | @drivers = sort {$a->{tagset} cmp $b->{tagset}} (@drivers); | ||
| 238 | return \@drivers; | ||
| 239 | } | ||
| 240 | |||
| 241 | |||
| 242 | |||
| 243 | #------------------------------------------------------------------------------ | ||
| 244 | # Returns the set of all known tagset drivers indexed by tagset id. If there | ||
| 245 | # are two drivers installed for the same tagset, it is not defined which one | ||
| 246 | # will be returned (and something is definitely wrong if they are not | ||
| 247 | # identical implementations). Exception: Interset 2.0 drivers are prefered over | ||
| 248 | # the old ones. | ||
| 249 | # | ||
| 250 | # For backward compatibility, this function is also available under its older | ||
| 251 | # name, get_driver_hash(). I later decided that I wanted to call it | ||
| 252 | # hash_drivers(). It is available for import as hash_drivers(). | ||
| 253 | #------------------------------------------------------------------------------ | ||
| 254 | sub hash_drivers | ||
| 255 | { | ||
| 256 | return get_driver_hash(); | ||
| 257 | } | ||
| 258 | sub get_driver_hash | ||
| 259 | { | ||
| 260 | if(!defined($driver_hash)) | ||
| 261 | { | ||
| 262 | my $driver_list = find_drivers(); | ||
| 263 | # Index the drivers by tagset id. | ||
| 264 | my %hash; | ||
| 265 | foreach my $driver (@{$driver_list}) | ||
| 266 | { | ||
| 267 | # It is possible (though not exactly desirable) that there are several drivers installed for the same tagset. | ||
| 268 | if(exists($hash{$driver->{tagset}})) | ||
| 269 | { | ||
| 270 | # If the previously encountered driver is old and this one is new, prefer the new one. | ||
| 271 | # Otherwise (both are old or both are new) just hope that the two installed modules are identical. | ||
| 272 | if($hash{$driver->{tagset}}{old} && !$driver->{old}) | ||
| 273 | { | ||
| 274 | $hash{$driver->{tagset}} = $driver; | ||
| 275 | } | ||
| 276 | } | ||
| 277 | else # this is the first driver found for this tagset | ||
| 278 | { | ||
| 279 | $hash{$driver->{tagset}} = $driver; | ||
| 280 | } | ||
| 281 | } | ||
| 282 | $driver_hash = \%hash; | ||
| 283 | } | ||
| 284 | return $driver_hash; | ||
| 285 | } | ||
| 286 | |||
| 287 | |||
| 288 | |||
| 289 | #------------------------------------------------------------------------------ | ||
| 290 | # Tries to enumerate known tagsets (for which there is at least one driver). | ||
| 291 | # This function uses find_drivers() but it returns a list of tagset ids | ||
| 292 | # available for the user to ask for creating a driver object. | ||
| 293 | #------------------------------------------------------------------------------ | ||
| 294 | sub find_tagsets | ||
| 295 | { | ||
| 296 | my $hash = get_driver_hash(); | ||
| 297 | my @tagsets = sort(keys(%{$hash})); | ||
| 298 | return @tagsets; | ||
| 299 | } | ||
| 300 | |||
| 301 | |||
| 302 | |||
| 303 | 1; | ||
| 304 | |||
| 305 | __END__ | ||
| 306 | |||
| 307 | =pod | ||
| 308 | |||
| 309 | =encoding UTF-8 | ||
| 310 | |||
| 311 | =head1 NAME | ||
| 312 | |||
| 313 | Lingua::Interset - DZ Interset is a universal morphosyntactic feature set to which all tagsets of all corpora/languages can be mapped. | ||
| 314 | |||
| 315 | =head1 VERSION | ||
| 316 | |||
| 317 | version 3.015 | ||
| 318 | |||
| 319 | =head1 SYNOPSIS | ||
| 320 | |||
| 321 | use Lingua::Interset qw(decode encode); | ||
| 322 | |||
| 323 | my $tag1 = 'NN'; # in the English Penn Treebank, "NN" means "noun" | ||
| 324 | my $feature_structure = decode('en::penn', $tag1); | ||
| 325 | print($feature_structure->as_string(), "\n"); | ||
| 326 | $feature_structure->set_number('plur'); | ||
| 327 | my $tag2 = encode('en::penn', $feature_structure); | ||
| 328 | print("$tag2\n"); | ||
| 329 | |||
| 330 | =head1 DESCRIPTION | ||
| 331 | |||
| 332 | DZ Interset is a universal framework for reading, writing, converting and | ||
| 333 | interpreting part-of-speech and morphosyntactic tags from multiple tagsets | ||
| 334 | of many different natural languages. | ||
| 335 | |||
| 336 | Individual tagsets are mapped to the Interset using specialized modules | ||
| 337 | called I<tagset drivers>. Every driver must implement three methods: | ||
| 338 | C<decode>, C<encode> and C<list>. | ||
| 339 | |||
| 340 | The main module, C<Lingua::Interset>, provides parameterized access to the | ||
| 341 | drivers and their methods. Instead of having to C<use> particular modules | ||
| 342 | (which would mean you know in advance what tagsets your program will be | ||
| 343 | working with) you just specify the tagset giving its I<identifier> as a parameter. | ||
| 344 | Tagset ids are derived from Perl package names but they are always all-lowercase. | ||
| 345 | Most tagsets are taylored for one language and their id has two components (separated by C<::>): | ||
| 346 | the ISO 639 code of the language, and a part to distinguish various tagsets for the language. | ||
| 347 | This second component may be some sort of abbreviated name of the corpus where the tagset is used, | ||
| 348 | for example. | ||
| 349 | |||
| 350 | More information is given at the DZ Interset project page, | ||
| 351 | L<https://wiki.ufal.ms.mff.cuni.cz/user:zeman:interset>. | ||
| 352 | |||
| 353 | =head1 FUNCTIONS | ||
| 354 | |||
| 355 | =head2 decode() | ||
| 356 | |||
| 357 | my $fs = decode ('en::penn', 'NNS'); | ||
| 358 | |||
| 359 | A generic interface to the C<decode()> method of L<Lingua::Interset::Tagset>. | ||
| 360 | Takes tagset id and a tag in that tagset. Returns a L<Lingua::Interset::FeatureStructure> object | ||
| 361 | with corresponding feature values set. | ||
| 362 | |||
| 363 | =head2 encode() | ||
| 364 | |||
| 365 | my $fs = decode ('en::penn', 'NNS'); | ||
| 366 | my $tag = encode ('en::conll', $fs); | ||
| 367 | |||
| 368 | A generic interface to the C<encode()> method of L<Lingua::Interset::Tagset>. | ||
| 369 | Takes tagset id and a L<Lingua::Interset::FeatureStructure> object. | ||
| 370 | Returns the tag in the given tagset that corresponds to the feature values. | ||
| 371 | Note that some features may be ignored because they cannot be represented | ||
| 372 | in the given tagset. | ||
| 373 | |||
| 374 | =head2 encode_strict() | ||
| 375 | |||
| 376 | my $fs = decode ('en::penn', 'NNS'); | ||
| 377 | my $tag = encode_strict ('en::conll', $fs); | ||
| 378 | |||
| 379 | A generic interface to the C<encode_strict()> method of L<Lingua::Interset::Tagset>. | ||
| 380 | Takes tagset id and a feature structure (L<Lingua::Interset::FeatureStructure>). | ||
| 381 | Returns a tag of the identified tagset that matches the contents of the feature | ||
| 382 | structure. | ||
| 383 | |||
| 384 | Unlike C<encode()>, C<encode_strict()> always returns a I<known tag>, i.e. | ||
| 385 | one that is returned by the C<list()> method of the Tagset object. Many tagsets | ||
| 386 | consist of I<structured> tags, i.e. they can be defined as a compact representation | ||
| 387 | of a feature structure (a set of attribute-value pairs). It is in principle possible | ||
| 388 | to encode such combinations of features and values that did not appear in the original | ||
| 389 | tagset. For example, a tagset for Czech is unlikely to contain a tag saying that | ||
| 390 | a word is preposition and at the same time setting non-empty value for gender. | ||
| 391 | Yet it is possible to create such a tag because the tagset encodes part of speech | ||
| 392 | and gender independently. | ||
| 393 | |||
| 394 | If this is undesirable behavior, the application should call C<encode_strict()> | ||
| 395 | instead of C<encode()>. Then it will be guaranteed that the resulting tag is one | ||
| 396 | of those returned by C<list()>. Nevertheless, think twice whether you really need | ||
| 397 | the guarantee, as it does not come for free. The necessity to replace forbidden | ||
| 398 | feature values by permitted ones may sometimes lead to surprising or confusing | ||
| 399 | results. | ||
| 400 | |||
| 401 | =head2 list() | ||
| 402 | |||
| 403 | my $list_of_tags = list ('en::penn'); | ||
| 404 | |||
| 405 | A generic interface to the C<list()> method of L<Lingua::Interset::Tagset>. | ||
| 406 | Takes tagset id and returns the reference to the list of all known tags of that tagset. | ||
| 407 | This is not directly needed to decode, encode or convert tags but it is very useful | ||
| 408 | for testing and advanced operations over the tagset. | ||
| 409 | Note however that many tagset drivers contain only an approximate list, | ||
| 410 | created by collecting tag occurrences in some corpus. | ||
| 411 | |||
| 412 | =head2 get_driver_object() | ||
| 413 | |||
| 414 | my $driver = get_driver_object ('en::penn'); | ||
| 415 | |||
| 416 | A generic accessor to installed Interset drivers of tagsets. | ||
| 417 | Takes tagset id and returns a L<Lingua::Interset::Tagset> object. | ||
| 418 | |||
| 419 | The objects are cached and if you call this function several times for the same | ||
| 420 | tagset, you will always get the reference to the same object. Tagset objects | ||
| 421 | do not have variable state, so it probably does not make sense to have several | ||
| 422 | different driver objects for the same tagset. If you want to get a different | ||
| 423 | object, you must call C<new()>, e.g. C<< Lingua::Interset::Tagset::EN::Penn->new() >>. | ||
| 424 | |||
| 425 | =head2 find_drivers() | ||
| 426 | |||
| 427 | my $list_of_drivers = find_drivers (); | ||
| 428 | |||
| 429 | This function searches relevant folders in C<@INC> for installed Interset | ||
| 430 | drivers for tagsets. | ||
| 431 | It looks both for the new Interset 2 drivers (e.g. C<Lingua::Interset::Tagset::EN::Penn>) | ||
| 432 | and for the old Interset 1 drivers (e.g. C<tagset::en::penn>). | ||
| 433 | It returns a reference to an array of hash references. | ||
| 434 | Every hash in the list contains the following fields | ||
| 435 | (here with example values): | ||
| 436 | |||
| 437 | my %record = | ||
| 438 | ( | ||
| 439 | 'old' => 1, # 1 or 0 ... old or new driver? | ||
| 440 | 'tagset' => 'en::penn', # tagset id | ||
| 441 | 'package' => 'Lingua::Interset::Tagset::EN::Penn', # this is what you 'use' or 'require' in your code | ||
| 442 | 'path' => '/home/zeman/perl5/lib/Lingua/Interset/Tagset/EN/Penn.pm' # path where it is installed | ||
| 443 | ); | ||
| 444 | |||
| 445 | Note that you may find more than one package for the same tagset id. | ||
| 446 | This function will list all of them. | ||
| 447 | When you ask Interset to do something with a tagset (e.g. C<decode ('en::penn', $tag)>), | ||
| 448 | Interset will select one of the available packages for you. | ||
| 449 | It will prefer new drivers over the old ones. | ||
| 450 | If you have two old or two new drivers, their priority will be decided by Perl | ||
| 451 | and it should correspond to the order of your C<$PERL5LIB> environment variable. | ||
| 452 | To avoid confusion, it is recommended that you have each package installed only once. | ||
| 453 | |||
| 454 | =head2 hash_drivers() | ||
| 455 | |||
| 456 | my $hash_of_drivers = hash_drivers (); | ||
| 457 | |||
| 458 | Returns the set of all known tagset drivers indexed by tagset id. The elements | ||
| 459 | are hashes themselves, with the same record structure as returned by | ||
| 460 | C<find_drivers()>. Unlike C<find_drivers()>, here the records are organized | ||
| 461 | in a hash instead of a list, and only one driver per tagset is present. | ||
| 462 | If there are two drivers installed for the same tagset, the one that appears | ||
| 463 | earlier in C<@INC> (or in the C<PERL5LIB> environment variable) is returned. | ||
| 464 | Exception: Interset 2.0 and newer drivers are prefered over the old ones. | ||
| 465 | |||
| 466 | =head2 find_tagsets() | ||
| 467 | |||
| 468 | my @list_of_tagset_ids = find_tagsets (); | ||
| 469 | |||
| 470 | This function uses find_drivers() and further processes its output. It returns | ||
| 471 | the list of tagset ids for which there is a driver installed. The user can then | ||
| 472 | call the C<get_driver_object()> method on these ids. | ||
| 473 | |||
| 474 | =head1 SEE ALSO | ||
| 475 | |||
| 476 | L<Lingua::Interset::FeatureStructure>, | ||
| 477 | L<Lingua::Interset::Tagset> | ||
| 478 | |||
| 479 | =head1 AUTHOR | ||
| 480 | |||
| 481 | Dan Zeman <zeman@ufal.mff.cuni.cz> | ||
| 482 | |||
| 483 | =head1 COPYRIGHT AND LICENSE | ||
| 484 | |||
| 485 | This software is copyright (c) 2019 by Univerzita Karlova (Charles University). | ||
| 486 | |||
| 487 | This is free software; you can redistribute it and/or modify it under | ||
| 488 | the same terms as the Perl 5 programming language system itself. | ||
| 489 | |||
| 490 | =cut |